Search code examples
phpfacebookfacebook-business-sdk

How to get Response Headers in Facebook Business SDK


For a Project I need to check the X-App-Usage response Headers to throttle the amount of requests we are doing without running into the rate limiting.

How can I get the response headers? I can't find any documentation for this and no simple solution be diving into the source. https://github.com/facebook/facebook-php-business-sdk

<?php

use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\Post;
use FacebookAds\Object\Fields\CommentFields;

Api::init($app_id, $app_secret, $access_token, false);
$api = Api::instance();
$api->setLogger(new CurlLogger());

$post = new Post($postId);

$commentsRequest = $post->getComments(
    array(
        CommentFields::ID,
        CommentFields::FROM,
        CommentFields::MESSAGE
    ),
    array(
        'limit' => 500
    ),
    true
);        // returns FacebookAds\ApiRequest

$cursor = $commentsRequest->execute(); // returns FacebookAds\Cursor
$cursor->setUseImplicitFetch(true);

foreach ($cursor as $comment) {
    // returns an instance of FacebookAds\Object\Comment
    // doing some stuff with the comments
}

Solution

  • I just found the solution to my problem. I can get the response and headers from the Cursor object via $cursor->getLastResponse()->getHeaders()).