Wednesday, July 25, 2012

How to fix facebook connect error "CurlException: 7"


Many websites with facebook applications, has lately been facing problems with CurlException 7

The problems range from:

1- CurlException: 7: couldn't connect to host

2- Uncaught CurlException: 7: couldn't connect to host

3- CurlException: 7: Failed to connect to 2a03:2880:10:1f02:face:b00c:0:26: Network is unreachable

The first and second problem are usually more general, and can occur due to many reasons, for example your host might have port 80 or port 443 blocked, and your facebook api will fail to connect.

Another reason might be, your cURL php extension is not installed, you can check if you have cURL installed from phpinfo();

<?php
  phpinfo();
 ?>


If you have access to your server through SSH, you can always install or compile curl  (php-curl).

Also, make sure to check your firewall settings, sometimes your firewall might be blocking some ports or applications, and your script will fail to connect to facebook.


In the 2nd reason (Uncaught CurlException: 7: couldn't connect to host), notice how it says "Uncaught", how dangerous is that?

First it means that your code is not using exception handling by surrounding the code with try and catch.
Secondly when the error occurs, your website will either not load and show this error, or it will load and show the error, and the facebook action will remain incomplete.
Thirdly, it will most probably show the file from where the error occurred, which is insecure and can render the website vunlerable to attacks.

So basically, always make sure to surround your code with try and catch, for example:

<?php
            $facebook = new Facebook(array(
                  'appId'  => 'myFbID'),
                  'secret' => 'myFBSecret"),
                  'cookie' => true,
            ));

            try
            {
                $me = $facebook->api('/me');
            }
            catch (FacebookApiException $e)
            {
                error_log($e);
            }
 ?>

In the catch part, it's always a good idea to write the error to the log file, so that you can keep track of the errors generated and eventually fix them.

In the third option (CurlException: 7: Failed to connect to 2a03:2880:10:1f02:face:b00c:0:26: Network is unreachable), the error is usually generated from cURL being unable to connect to an IPv6.

Lately, facebook has enabled IPv6 on their opengraph, so when your script is calling the opengraph through the api, your server will attempt to connect to an IPv6, and since some servers doesn't have IPv6 enabled or supported yet and doesn't route IPv6 to IPv4, cURL will not be able to connect to the open graph and the error will be thrown.

Some solutions can be made to prevent this:

If you're using a new php version, 5.3+, you can force cURL to resolve to an IPv4 instead of IPv6.
Note that cURL will always attempt to use IPv6.
To force cURL to use IPv4, you can add the following line of code
curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

This should tell cURL to use only IPv4 instead of IPv6 when available.


However this solution doesn't work all the time, so another solution could be, even though it's not a good solution in the long term, is by editing your hosts file.
For servers with linux, you can simply login with SSH and add the following line in your hosts:
69.171.224.54 graph.facebook.com
To edit hosts file, enter the following command
vi /etc/hosts

then append the following line:
69.171.224.38 graph.facebook.com

Although this will temporarily solve the problem, however if Facebook decides one day to stop using this IP for opengraph, your application will stop working, and it might be hard to track the reason of why your app is not working.


For windows, you will have to edit the file (same as above) located in:
windows\system32\drivers\etc\hosts


The third solution could be to disable your IPv6 from the server, some servers have IPv6 installed when the server can't even resolve or route IPv6.

Disabling IPv6 depends on your server, and can be easily found on google.


Hopefully this post will help solving your fb connect problem :)