如何每页而不是一个应用程序使用Facebook API

时间:2019-07-02 09:43:39

标签: php wordpress facebook

我正在构建一个应用程序,该应用程序基于数据库中具有Facebook页面ID的人员,使用Facebook帖子填充数据库。如果找到了Facebook页面ID,则会提取其帖子,并将使用特定主题标签的帖子保存到数据库中。

我的问题是,Facebook API一直处于超时状态,目前我只列出了4到5个人。我发现每个用户可以收到250个请求,但是我不确定我是否以正确的方式使用API​​来获取每页的帖子。

我是否必须先登录并亲自批准才能这样做?对于我想做的事情,那真的不起作用。

我一直在使用Github上的PHP SDK,下面是我的代码。我一直在使用Graph Explorer跟踪它,但是我不确定的一件事是如何在每页上生成令牌。

我目前正在使用手动令牌,因为我不知道如何每页生成它们,我尝试查看Git PHP SDK和Facebook API的文档-但这只是没有用在我的大脑中,所以我希望这里的某个人可能能够更好地向我说明。

谢谢您的帮助,我是Facebook API的新手。

$apitoken = 'EAAF4azHPHh0BABSb7ZCPVv2U4pbBsZAfOMjA0tiTcKJoKKIsgfe0ZBZBf8ZAo5lZCHVh51WQzd1lvdOreA8Q4vU9F3ZCP9B1si1NWKZA7tiXxX1V06hNmQhT2tTZA8N27kinhiUDG0gDbYqLLZBZBRkGlAGuGdNZC3hdI6yJXLqd8QoPGWoi2LiLVmlqex2znHlbuqPzx0aq7YCVBQZDZD';


if( have_rows('social_networks') ): 
while( have_rows('social_networks') ): the_row(); 
if( have_rows('facebook_group') ): 
while( have_rows('facebook_group') ): the_row();
if(get_sub_field('facebook_username')){

require_once __DIR__ . '/vendor/autoload.php'; // change path as needed
require_once 'src/Facebook/autoload.php';
$fb = new \Facebook\Facebook([
'app_id' => '413876769201693',
'app_secret' => '1517da72a5c8ccaf5524d95a02d8481a',
'default_graph_version' => 'v3.3',
//'default_access_token' => '{access-token}', // optional
]);


try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$username = $facebook_username;
$response = $fb->get("/$username?fields=fan_count,id,name,instagram_business_account,feed", "$apitoken");
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
 // When validation fails or other local issues
 echo 'Facebook SDK returned an error: ' . $e->getMessage();
 exit;
 }


$me = $response->getGraphUser();
//print_r($me);

////////////////////////////////////////////////////////
//////////////////// FACEBOOK///////////////////
//////////////////////////////////////////////////////

$facebook_likes = $me['fan_count'];
$facebook_posts = $me['feed'];
} endwhile; endif; endwhile; endif;

////////////////////////////////////////////////////////
//////////////////// FACEBOOK POSTS ///////////////////
//////////////////////////////////////////////////////



foreach($facebook_posts as $facebook_post){
$facebook_post_id = $facebook_post['id'];

try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get("/$facebook_post_id?fields=likes.limit(0).summary(true),comments.limit(0).summary(true),shares,reactions.limit(0).summary(true),message", "$apitoken");
 } catch(\Facebook\Exceptions\FacebookResponseException $e) {
 // When Graph returns an error
 echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}


$facebook_post_details = $response->getGraphUser();

$facebook_post_message = $facebook_post_details['message'];
$facebook_post_likes = $facebook_post_details['likes']['summary']['total_count'];
$facebook_post_comments = $facebook_post_details['comments'];
$facebook_post_shares = $facebook_post_details['shares'];

$post_id_fb = get_the_ID();

 $args_fb = array( 
'post_type' => 'campaigns',
);
$the_query_fb = new WP_Query( $args_fb );

if ( $the_query_fb->have_posts() ) : while ( $the_query_fb->have_posts() ) : $the_query_fb->the_post();
$campaign_id = get_the_ID();
$hashtag_fb = get_field('hashtag');
$post_title_fb = get_the_title(get_the_ID());

  if (strpos($facebook_post_message, $hashtag_fb) !== false) {
    if (get_page_by_title($facebook_post_message, OBJECT, 'campaign-posts')) {

  wp_update_post(array(
    'post_title'=> ' '.$facebook_post_message.' :facebook', 
    'post_type'=>'campaign-posts', 
    'post_content'=> $facebook_post_message,
    'status' => 'published',
    'meta_input' => array(
    'likes' => $facebook_post_likes,
     // 'shares' => $facebook_post_shares,
    //  'reactions' => $facebook_post_reactions,
      'influencer' => $post_id_fb,
      'hashtag' => $hashtag_fb,
      'platform'=> 'facebook',
      'link' => $facebook_username,            
    )

));

  wp_update_post(array(
    'post_title'=> $post_title_fb, 
    'post_type'=> 'campaigns', 
    'post_content'=> get_the_content($campaign_id),
    'status' => 'published',
    'meta_input' => array(
      'hashtag' => $hashtag_fb,
      )

    ));

    }else{
      wp_insert_post(array(
        'post_title'=> ' '.$facebook_post_message.' :facebook', 
        'post_type'=>'campaign-posts', 
        'post_content'=> $facebook_post_message,
        'status' => 'published',
        'meta_input' => array(
        'likes' => $facebook_post_likes,
         // 'shares' => $facebook_post_shares,
        //  'reactions' => $facebook_post_reactions,
          'influencer' => $post_id_fb,
          'hashtag' => $hashtag_fb,
          'platform'=> 'facebook',
          'link' => $facebook_username,            
        )

    ));

    wp_update_post(array(
      'post_title'=> $post_title_fb, 
      'post_type'=> 'campaigns', 
      'post_content'=> get_the_content($campaign_id),
      'status' => 'published',
      'meta_input' => array(
        'hashtag' => $hashtag_fb,
        )

      ));

    }

} 

endwhile;
endif;
}

0 个答案:

没有答案