Facebook graph api PHP comments issue

对着背影说爱祢 提交于 2020-01-17 12:44:48

问题


Any one familiar with Facebook graph api? I have a little bug.. I have coded an app Which reply to users comments with a greating comment but it reply with multiple comments instead a single reply.. Any help with this issue?

Here is my webhook code

<?php

  $verify_token = "***************";
  if ($_REQUEST['hub_verify_token'] == $verify_token) {
    echo $_REQUEST['hub_challenge'];
  }

  require_once 'facebook_sdk/src/Facebook/autoload.php';
  $input = json_decode(file_get_contents('php://input'), true);
  if( $input['entry'][0]['changes'][0]['value']['item'] == "comment" &&
      !empty($input['entry'][0]['changes'][0]['value']['message']) ) {
    $sender = $input['entry'][0]['changes'][0]['value']['from']['id'];
    $page_id = $input['entry'][0]['id'];
    if($sender != $page_id):
      $accesstoken = '**************';
      $post_id = $input['entry'][0]['changes'][0]['value']['post_id'];
      $parent_id = $input['entry'][0]['changes'][0]['value']['parent_id'];
      if($parent_id == $post_id):
        $cid = $input['entry'][0]['changes'][0]['value']['comment_id'];
        $message = "-";
        $fb = new Facebook\Facebook([
          'app_id' => '*************',
          'app_secret' => '**************',
          'default_graph_version' => 'v5.0',
        ]);

        if( !empty($input['entry'][0]['changes'][0]['value']['message'])):
          try {
            // Returns a `Facebook\FacebookResponse` object
            $response = $fb->post(
              '/'.$cid.'/comments',
              array (
                'message' => '@['.$sender.'] welcome in our page'
              ),
              $accesstoken
            );
          } catch(Facebook\Exceptions\FacebookResponseException $e) {
            echo 'Graph returned an error: ' . $e->getMessage();
          } catch(Facebook\Exceptions\FacebookSDKException $e) {
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
          }
        endif;
        if(!empty($message)):
          //$response = $fb->get('/'.$post_id.'/full_picture',$accesstoken);
          //$pic =$response->getGraphNode();
          $jsonData = '{
            "recipient":{
              "comment_id":"'.$cid.'"
            },
            "message": 'thank you for commenting'
          }';
          $url = 'https://graph.facebook.com/v5.0/me/messages? access_token='.$accesstoken;
          $ch = curl_init($url);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
          if($r==true) {
            $plan->UpdateRepliesCount($post_id,$count);
            $result = curl_exec($ch);
          }
          curl_close($ch);
        endif;
      endif;
    endif;
?>

来源:https://stackoverflow.com/questions/59733502/facebook-graph-api-php-comments-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!