Inconsistent error with Facebook Graph API in PHP - Failed to connect to graph.facebook.com port 443: Connection timed out Inconsistent error with Facebook Graph API in PHP - Failed to connect to graph.facebook.com port 443: Connection timed out curl curl

Inconsistent error with Facebook Graph API in PHP - Failed to connect to graph.facebook.com port 443: Connection timed out


As it turns out, a hardcoded CURLOPT_CONNECTTIMEOUT is set in Facebook's SDK in addition to the regular timeout - that seemed to be what was causing the issue. Whenever the connection between my server and the Facebook API was too slow (10s+), it would time out because 10s is the default value in the SDK.

I changed this value to cURL's default value for this option, which is 300, and it started working again. This value is set in Facebook's SDK in the FacebookCurlHttpClient class, in the openConnection method.

Now, for what causes the connection to be this slow at some times, that remains unknown, but at least it doesn't crash anymore when that happens.

You can redefine the methods of the SDK's cURL wrapper, as shown here: https://www.sammyk.me/how-to-inject-your-own-http-client-in-the-facebook-php-sdk-v5#customizing-the-curl-predefined-constants

For example, here's my redefined methods to allow much longer time periods before timeouts:

<?phpnamespace AppBundle\Lib\Facebook;class CustomCurlOptsHttpClient extends \Facebook\HttpClients\FacebookCurlHttpClient{    public function send($url, $method, $body, array $headers, $timeOut)    {        $timeOut *= 5;        return parent::send($url, $method, $body, $headers, $timeOut);    }    public function openConnection($url, $method, $body, array $headers, $timeOut)    {        $timeOut *= 5;        parent::openConnection($url, $method, $body, $headers, $timeOut);        $options = [            CURLOPT_CONNECTTIMEOUT => 300,        ];        $this->facebookCurl->setoptArray($options);    }}

You can also use batch requests in the Facebook SDK if you have multiple requests to make one after the other. That will help speed up the process and prevent you from hitting to timeouts.

Hope that helps anyone else out there who's facing the same issue!


I can't comment, but can you provide some code? Will be happy to help and debug the issue.

Also, Facebook Graph API does return this error when something is blocking your request. For example,file_get_contents is blocked on a lot of shared hosting servers.

You should also handle the error.


Here is a sample snippet i am using and working fine for me.

<?php    include 'configs.php';    include_once "Facebook/autoload.php";    $swipe=1;    $goto=null;    try {        if (!isset($_SESSION['FACEBOOK_SESSION_TOKEN'])) {            $fb = new Facebook\Facebook([                'app_id' => APP_ID,                'app_secret' => APP_SECRET,                'default_graph_version' => 'v2.5',            ]);            $helper = $fb->getRedirectLoginHelper();            $permissions = ["user_about_me","publish_actions" , "user_photos"];            $loginUrl = $helper->getLoginUrl( CALLBACK_URL ,  $permissions);            $swipe=0;        }    }    catch(Facebook\Exceptions\FacebookResponseException $e) {        echo 'Graph returned an error: ' . $e->getMessage();        exit;    } catch(Facebook\Exceptions\FacebookSDKException $e) {        echo 'Facebook SDK returned an error: ' . $e->getMessage();        exit;    }?>

Config.php

<?php    define("CALLBACK_URL", "http://{domain}/facebookredirect.php");    define("RESULT_PAGE", "http://{domain}//profile.php");    define("LOGIN_URI" , "http://{domain}//index.html");    define("APP_ID" , "########");    define("APP_SECRET" ,"#####");   ?>

Also, The Graph API doesn't have 100% uptime. Check your server as curl working properly.