If someone is sitting in the same vexation:
file_get_contents does not seem to handle the token request. The following code works.
<?php
function curlRequest($url) {
$ch = curl_init();
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$timeout = 5;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$app_id = '................';
$app_secret = '..................................';
$my_url = 'http://www.memyself.andi/me.php';
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. urlencode($_SESSION['state']) . "&scope=user_birthday,read_stream";
header("Location: " . $dialog_url);
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id=' .
$app_id . '&redirect_uri=' . urlencode($my_url) .
'&client_secret=' . $app_secret . '&code=' . urlencode($code);
$response = curlRequest($token_url);
$params = null;
parse_str($response, $params);
$_SESSION['access_token'] = $params['access_token'];
$graph_url = "https://graph.facebook.com/_THE_PAGE_ID/feed?access_token="
. $params['access_token'];
$json = json_decode(file_get_contents($graph_url));
echo serialize($json);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
source
share