Note that there are some explanatory texts on larger screens.

plurals
  1. POGet my facebook public events and show in the my site with Facebook API - SDK
    text
    copied!<p>I'd like to show my public events on my site. I use the code below, but it returns an empty object.</p> <p>My app has permissions on: user_events; rsvp_event, read_stream I would not use "offline_access" it will be deprecated permission.</p> <p>I read extensively the api documentation, but it lacks practical examples...</p> <p>As I use the "static token / $token_offline" it returns the events correctly.</p> <pre><code>// 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' =&gt; APP_ID, 'secret' =&gt; APP_SECRET, 'cookie' =&gt; 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 . '&amp;client_secret=' . APP_SECRET . '&amp;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"]=&gt; array(0) { } } */ </code></pre> <p>I am using this code as a temporary solution and would like to know the community opnion on this method.</p> <pre><code>// 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 . '&amp;access_token=' . $access_token)); // if response returned is error get new token based in old token database if (isset($decoded_response-&gt;error) &amp;&amp; $decoded_response-&gt;error-&gt;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 . '&amp;client_secret=' . $app_secret . '&amp;grant_type=fb_exchange_token&amp;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 . '&amp;access_token=' . $access_token)); } // events returned based in ancient token saved in database return $decoded_response; } // if no token saved in database return false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload