This problem is really frustrating because I can see what's causing it but can't find a solution.
When attempting to achieve a facebook access token first have my url:
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&code=".$code;
I next have tried to both use url_decode
and curl like so:
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$response = curl_exec($ch);
curl_close($ch);
With curl, the response is empty (even with returntransfer set to true).
So next I try with file_get_contents like so:
$response = file_get_contents($token_url);
echo $response;
This then produces an error which suggests that it is not parsing the url correctly.
Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?client_id=XXXXXXXX&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2F&client_secret=XXXXXXXXXXXXXXXXXX&code=AQAw8Y3fehv2IWxhmQs9M28IsP4hAzwL5pnvSJkb2v_9H61Gpt659IgMHLgvgRH46tXl8BAL0SRyilKqVay4b4Su68nzT2OmbVK88eNHdGQqIaR_N7X8noIkQTeq_HuEImLlxQwbFcg_PJ5EGh9l392KL2OFqi8qplgOE5m21qlKOvVvUE3cZrcfk_mmr9FUm5eQXIvEp8sUYHxHAXvvyNmV [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:\Users\Mark\workspace\Never\aqueous-sands-5942\index.php on line 65
By removing the "amp;"
the url works and you can see the correct response by opening the url directly in browser.
How do I prevent the "amp;"
becoming a part of the url? I can't see any extra spaces anywhere and I have encoded my url (http://localhost:8000/)
What am I doing wrong?
UPDATE:
Larger code sample:
require 'sdk/src/facebook.php';
$app_id = "123456778";
$app_secret = "XXXXXXXXXXXXX";
$my_url = "http://localhost:8000/";
session_start();
@$code = $_REQUEST["code"];
if (!isset($_REQUEST["code"])) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . $my_url . "&state=" . $_SESSION['state'] . "&scope=email";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
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=".$code;
echo "value of token:";
echo $token_url;
/* curl section (commenting out) */ /*
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$response = curl_exec($ch);
curl_close($ch); */
echo "command:";
echo $token_url;
echo "value:";
$response = file_get_contents($token_url);
echo $response;
$params = null;
parse_str($response, $params);