The fboauth_graph_query() function defined in the fboauth module supports only GET requests. A lot of features that I need to implement for the site I am working on require me to make requests that the fb api documentation clearly says can only be done using POST requests.
I came up with a replacement to the function that I use and it works well. I thought I'd share it here so it may be included in the fboauth module.
The function can be called exactly as fboauth_graph_query(). One need only specify the type of request if POST type is required.
/*
* Copy of fboauth_graph_query with ability to select type of request POST or GET.
* Because updating fb pages etc. can only be done using POST requests and the fboauth_graph_query func doesn't support POST.
*/
function mymodule_graph_query($id, $access_token, $parameters = array(), $method = 'GET') {
$parameters['access_token'] = $access_token;
if ( $method == 'GET') {
$graph_url = url('https://graph.facebook.com/' . $id, array('absolute' => TRUE, 'query' => $parameters));
$graph_result = drupal_http_request($graph_url, array('headers' => array(), 'method' => $method));
}
elseif ( $method == 'POST') {
$graph_url = 'https://graph.facebook.com/' . $id;
$post_data = http_build_query($parameters, '', '&');
$graph_result = drupal_http_request($graph_url, array('headers' => array(), 'method' => $method, 'data' => $post_data));
}
else {
drupal_set_message('error', 'Invalid request type provided for mymodule_graph_query()');
}
return json_decode($graph_result->data);
}
Comments
Comment #1
v-a-1 commentedSorry. The drupal_set_message line is incorrect. It should be:
drupal_set_message('Invalid request type provided for mymodule_graph_query()', 'error');Comment #2
quicksketchThanks for your suggestion! If you can post this as a patch I'll look at it next time I'm working on FBOAuth.
Comment #3
quicksketchI've committed this patch to both branches of FBOAuth.
Comment #5
v-a-1 commentedI've recently discovered that some Facebook requests use the HTTP 'DELETE' verb.
e.g. https://developers.facebook.com/docs/reference/api/page/ (Look under sections titled Delete).
And so have updated the fboauth_graph_query() to reflect the same.
Find attached a patch.
Note: I haven't come across the PUT method anywhere in the Facebook API, and I don't think it will ever be used, considering POST is generally used in its place. So I don't think support for PUT is required.
Comment #6
v-a-1 commentedComment #7
quicksketchThanks! Committed after fixing a minor error text. Here a backport for D6 too.