Batch calls with Facebook Graph API & PHP

前端 未结 5 1225
遇见更好的自我
遇见更好的自我 2020-12-13 07:55

Designing my first app against the Graph API, using version 2.1.2 of the Facebook supplied PHP library. Trying to maximize performance, etc out of the box and want to batch

5条回答
  •  青春惊慌失措
    2020-12-13 08:33

    Just an update for the new graph Batch API: You can also execute that as follows:

    // Save your method calls into an array
    $queries = array(
        array('method' => 'GET', 'relative_url' => '/me'),
        array('method' => 'GET', 'relative_url' => '/me/groups')
    );
    // POST your queries to the batch endpoint on the graph.
    $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
    
    // Return values are indexed in order of the original array, content is in ['body'] as a JSON
    // string. Decode for use as a PHP array.
    
    $user_profile = json_decode($batchResponse[0]['body'], true);
    $user_groups = json_decode($batchResponse[1]['body'], true);
    

    That should do the trick.

提交回复
热议问题