I'm trying to follow the example to create a server side login to Facebook from here, but with no luck. In step 7, when I try to exchange the code with a token and store it in a session for later use, I always get this error :

  file_get_contents(): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

I know this already asked many times, but I still unable to solve this, as per this question, I tried using cURL, but it still does not work, it just returned false.

Other question told me to use the PHP SDK, but I don't know which method to use. So I'm completely lost in here.


Here's my code for calling the login form :

<a href="<?php echo LOGIN_URL; ?> ?>" class="btn btn-primary">Login with Facebook</a>

where LOGIN_URL is defined as :

define("LOGIN_URL", $facebook->getLoginUrl(array("redirect_uri" => APP_URL . "fb_login")));

in fb_login, I have this code :

$app->get("/fb_login", function() use($app, $facebook)
{
    $code = $_REQUEST["code"];

    $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . APP_ID . "&redirect_uri=" . APP_URL . $_SESSION["request_uri"] // Previous URL
       . "&client_secret=" . APP_SECRET . "&code=" . $code;

    $response = file_get_contents($token_url); // Doesn't work
    $params = null;
    parse_str($response, $params);

    $_SESSION["access_token"] = $params["access_token"];

    // Let's try with cURL...
    // $ch = curl_init();
    // 
Edit
Report