4
votes

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

1
Do you have https stream open for file_get_content in your php.ini? Also, What are you trying to do with this call?Ashwini Dhekane
How can I open those? I am trying to get the access token for the user. ThanksTilalHusain
Has the user authenticated your app?Ashwini Dhekane
This is a network problem on your side, check your network settings and particularly check if both your IPV4 and IPV6 interfaces are both configuredIgy
This is almost certainly not a code problem, it's your code not being able to reach facebook's servers, it's a problem with your server configuration, firewall, hoster, ISP or other networkIgy

1 Answers

2
votes

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);