I am using Graph API https://developers.facebook.com/docs/reference/api/#publishing with PHP SDK and I would like to send some data via HTTP POST method, as it's mentioned in documentation (e.g. add comment).
At https://developers.facebook.com/docs/reference/api/batch/ they say I should encode the body of HTTP POST request as ...should be formatted as a raw HTTP POST body string, similar to a URL query string. I can't get the combination of PHP functions to get this to work. In the example they claim following should work:
"body": "message=Test status update"
Well, that works. But what if I need to add other params? And how should be this string encoded? E.g. I have this:
$data = array('name' => 'Gargamel', 'occupation' => 'Freelancing Smurf Hunter');
How should I process it to get required format? Following does NOT work:
$batch = array();
$q开发者_如何转开发uery = array(
'method' => 'POST',
'relative_url' => '/forrest/full/of/smurfs',
'body' => urldecode(http_build_query($data)),
);
$batch[] = $query;
$responses = $this->api('/?batch=' . json_encode($batch, JSON_HEX_AMP), 'POST');
I explored half of the Internet, but I can't find any more specific information about the format than the one mentioned above (raw HTTP POST similar to a URL query string).
Thanks for any suggestions!
using this: http://forum.developers.facebook.net/viewtopic.php?pid=331343#p331343
$batch_array[] = array(
'method' => 'POST',
'relative_url' => 'Relative url',
'body' => 'message=' . 'Your message' . '%26data=' . 'Your data' ,
);
This is my batch example code:
$graph_url = "https://graph.facebook.com/me/friends?access_token=" . $params['access_token'];
$friends = json_decode(file_get_contents($graph_url));
$batched_request = '[{"method":"GET","relative_url":"'.$friends->data[0]->id.'/likes"}';
for ($i = 1; $i < 20; $i++) {
$batched_request .= ',{"method":"GET","relative_url":"'.$friends->data[$i]->id.'/likes"}';
}
$batched_request .= ']';
$post_url = 'https://graph.facebook.com/?batch='
. $batched_request . '&access_token=' . $params['access_token'] . '&method=post';
$posts = file_get_contents($post_url);
for ($i = 0; $i < 20; $i++) {
$post = json_decode($posts[$i]->body);
echo($friends->data[$i]->id.' '.$friends->data[$i]->name);
//print_r($post);
}
Batch query limit 20 requests. You need to decode each part of batch query, see code above.
精彩评论