TilalHusain
TilalHusain

Reputation: 1016

Facebook Graph API Unreachable

I have been working on a facebook application which will be using Graph API for authentication, The code was working just fine tomorrow but now all of a sudden I have started receving Host is unreachable errors. The code I am using is:

$token_url = "https://graph.facebook.com/oauth/access_token?".
                "client_id=[client_id]".
                "&redirect_uri=http://www.next_big_website.com".
                "&client_secret=[client_secret]".
                "&code=" . $_GET['code'].
                "&scope=manage_pages,publish_stream,publish_actions,read_mailbox,email".
                "&response_type=token";
    $response = file_get_contents($token_url);

And the error I receive is:

Warning (2): file_get_contents(https://graph.facebook.com/oauth/access_token?client_id=[client_id]&redirect_uri=http://www.next_big_website.com&client_secret=[client_secret]&code=somelong_and_ugly_code&scope=manage_pages,publish_stream,publish_actions,read_mailbox,email&response_type=token): failed to open stream: Network is unreachable [temp.php, line 112]

Please help me with this as I have no idea what might have caused this.

Ok, I digged a bit further and found the solution, this happens because facebook is trying to force IPv6, because whenever my server tries to connect using IPv4 Facebook rejects my request, tracert traces a path to facebook API servers and then request gets dropped.

Thanks

Upvotes: 4

Views: 2910

Answers (1)

TilalHusain
TilalHusain

Reputation: 1016

Fixed it by using

 $url = ("https://graph.facebook.com/me/access_token?token");
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);

Upvotes: 2

Related Questions