azurinko
azurinko

Reputation: 271

Correct Facebook Graph Api Video Insitghts Request PHP SDK

 foreach( $videos_edge_array as $key => $video ) {
     $requests[$key] = $fb->request('GET', '/'.$video['id'].'/video_insights', [ 'metrics' => 'total_video_views', 'period' => 'lifetime' ] );
    }

  try {
    $batchResponse = $fb->sendBatchRequest($requests, $config['FACEBOOK']['ACCESS_TOKEN']);
  } 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;
  }

  foreach ($batchResponse as $key => $response) {
    if ($response->isError()) {
      $error = $response->getThrownException();
      echo $key . ' error: ' . $error->getMessage();
    } else {
      $videos[$key]['insights'] = $response->getGraphEdge()->asArray();
    }
  }

I think code is self descriptive, but this returns me just empty response...

$videos[$key]['insights']

Should have total_views metrics in it, but somehow doesnt resolve from my request... I have no Idea what to do, I already spend a lot of time on this.

Upvotes: 0

Views: 340

Answers (1)

Kaushal Kumar Panday
Kaushal Kumar Panday

Reputation: 2467

I dont understand PHP much. But I can add my 2 cents here.

In your scenario I can think of the following issues:

  • the URL that is being built is incorrect. Test this using GraphAPI Explorer.
  • the generated token doesn't have the required permissions to read the video content

A user's videos can be read if the owner has granted the user_videos or user_posts permission.

In the video_insights documentation there is no key called insights. So this line $videos[$key]['insights'] seems dicey to me.

Refer the following documentation: https://developers.facebook.com/docs/graph-api/reference/video/ https://developers.facebook.com/docs/graph-api/reference/video/video_insights/

Try using the Facebook Graph API Explorer to test how the URL should look like. If you haven't used it, then it is a must. See these screenshots for reference.Querying **video_insights** Retrieving **total_video_views**

Upvotes: 1

Related Questions