自分の公開イベントを自分のサイトに表示したいのですが。以下のコードを使用しますが、空のオブジェクトが返されます。
私のアプリには次の権限があります:user_events; rsvp_event、read_stream「offline_access」は使用しません。非推奨の権限になります。
APIドキュメントをよく読んでいますが、実用的な例がありません...
「静的トークン/$token_offline」を使用すると、イベントが正しく返されます。
// include SDK
if (!class_exists('Facebook'))
require_once (THEME_FACEBOOK_SDK . '/src/facebook.php');
// params config
define('PROFILE_ID', 'XXXXXXXXXXX'); // my profile id
define('APP_ID', 'XXXXXXXXXXX'); // my application APP_ID in facebook
define('APP_SECRET', 'XXXXXXXXXXX'); // my application APP_SECRET in facebook
// create application instance
$facebook = new Facebook(array('appId' => APP_ID, 'secret' => APP_SECRET, 'cookie' => true));
// get graph API
function fetchUrl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$retData = curl_exec($ch);
curl_close($ch);
return $retData;
}
/*--- I do not want to use it (Generated by Graph API Explorer)
$token_offline = 'AAAD5V3tZBKEQBAAmAfxTb8ZAObX3h6LfSTZCilFneZCkc5YEF5T0SjLt6ZB9i7wQUuvaqhIandqzkiHivZAjYyzMsOiajPsNYZD';*/
// get app token
$graph_url = 'https://graph.facebook.com/oauth/access_token?client_id=' . APP_ID . '&client_secret=' . APP_SECRET . '&grant_type=client_credentials';
// app token
$app_token = fetchUrl($graph_url);
// get events
$graph_url = 'https://graph.facebook.com/' . PROFILE_ID . '/events?' . $app_token; //$token_offline
var_dump(json_decode(fetchUrl($graph_url)));
/*
object(stdClass)#3947 (1) {
["data"]=>
array(0) {
}
}
*/
私はこのコードを一時的な解決策として使用しており、この方法に関するコミュニティの意見を知りたいと思います。
// Get response Graph API. Note this wrapper function exists in order to circumvent PHP’s strict obeying of HTTP error codes. In this case, Facebook returns error code 400 which PHP obeys and wipes out the response.
function fetchUrl($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents)
return $contents;
else
return false;
}
// In the start config only. Save in database the token generated by tools Graph API Explorer
//update_option('access_token_fb', 'AAAD5V3tZBKEQBAGTScYxCZAt3bWXJvzimsfWYAG0SWF40ffngLF03dU8T63gXWHJuiXLZClSoxzuC5UZAsXNDK5yQkQqBPXgAuJPZA3ZBjlgZDZD');
function get_events_facebook(){
$profile_id = 'XXXXXXXXXXX'; // user id my profile facebook
$app_id = 'XXXXXXXXXXX'; // app id
$app_secret = 'XXXXXXXXXXX'; // app secret
// events user select
$fql = "SELECT eid, name, pic, start_time, end_time, location, description FROM event WHERE creator={$profile_id} AND eid IN ( SELECT eid FROM event_member WHERE uid={$profile_id} ) ORDER BY start_time asc";
// link graph API get events
$api_url = 'https://graph.facebook.com/fql?q=' . urlencode($fql);
// get token saved in the database
if($access_token = get_option('access_token_fb')) {
// get events based in token saved in database
$decoded_response = json_decode(fetchUrl($api_url . '&access_token=' . $access_token));
// if response returned is error get new token based in old token database
if (isset($decoded_response->error) && $decoded_response->error->type == "OAuthException") {
// Client-side OAuth and Extending Access_Token Expiration Time through New Endpoint
$refresh_token = 'https://graph.facebook.com/oauth/access_token?client_id=' . $app_id . '&client_secret=' . $app_secret . '&grant_type=fb_exchange_token&fb_exchange_token=' . $access_token;
// create variable $token and set token value
parse_str(fetchUrl($refresh_token));
// save new token in database
update_option('access_token_fb', $access_token);
// get events based in the new token
return json_decode(fetchUrl($api_url . '&access_token=' . $access_token));
}
// events returned based in ancient token saved in database
return $decoded_response;
}
// if no token saved in database
return false;
}