Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook login integration uncaught exception "could not connect to host"

I am trying to learn facebook integration using the PacketCode tutorial available on YouTube. The code works fine on my localhost (even when tested by logging in to facebook). But when I tried to run the code on Hostinger.in free host, it shows following error:

Fatal error: Uncaught exception 'Facebook\FacebookSDKException' with message 'couldn't connect to host' in /home/u315632576/public_html/fblogin-basic-master/lib/Facebook/HttpClients/FacebookCurlHttpClient.php:142 Stack trace: #0 /home/u315632576/public_html/fblogin-basic-master/lib/Facebook/FacebookRequest.php(248): Facebook\HttpClients\FacebookCurlHttpClient->send('https://graph.f...', 'GET', Array) #1 /home/u315632576/public_html/fblogin-basic-master/lib/Facebook/FacebookRedirectLoginHelper.php(146): Facebook\FacebookRequest->execute() #2 /home/u315632576/public_html/fblogin-basic-master/index.php(54): Facebook\FacebookRedirectLoginHelper->getSessionFromRedirect() #3 {main} thrown in /home/u315632576/public_html/fblogin-basic-master/lib/Facebook/HttpClients/FacebookCurlHttpClient.php on line 142

After this I tried running this code by requesting a paid host owner on his paid hosted server an the code still works fine on it. So what can be the problem with the Hostinger.in free host server?

The code that is:

/* INCLUSION OF LIBRARY FILEs*/
    require_once( 'lib/Facebook/FacebookSession.php');
    require_once( 'lib/Facebook/FacebookRequest.php' );
    require_once( 'lib/Facebook/FacebookResponse.php' );
    require_once( 'lib/Facebook/FacebookSDKException.php' );
    require_once( 'lib/Facebook/FacebookRequestException.php' );
    require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
    require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
    require_once( 'lib/Facebook/GraphObject.php' );
    require_once( 'lib/Facebook/GraphUser.php' );
    require_once( 'lib/Facebook/GraphSessionInfo.php' );
    require_once( 'lib/Facebook/Entities/AccessToken.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
    require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;
    use Facebook\FacebookHttpable;
    use Facebook\FacebookCurlHttpClient;
    use Facebook\FacebookCurl;

/*PROCESS*/

    //1.Stat Session
     session_start();
    //2.Use app id,secret and redirect url
     $app_id = 'xyz';
     $app_secret = 'abcd';
     $redirect_url='http://www.rohansanap.com/fblogin-basic-master/';

     //3.Initialize application, create helper object and get fb sess
     FacebookSession::setDefaultApplication($app_id,$app_secret);
     $helper = new FacebookRedirectLoginHelper($redirect_url);
     $sess = $helper->getSessionFromRedirect();

    //4. if fb sess exists echo name 
        if(isset($sess)){
            //create request object,execute and capture response
        $request = new FacebookRequest($sess, 'GET', '/me');
        // from response get graph object
        $response = $request->execute();
        $graph = $response->getGraphObject(GraphUser::className());
        // use graph object methods to get user details
        $name= $graph->getName();
        echo "hi $name";
    }else{
        //else echo login
        echo '<a href='.$helper->getLoginUrl().'>Login with facebook</a>';
    }

Edit 1: I have mentioned above that same code works fine on localhost. I just realized that it works fine when run for first time and if I refresh page after that, same error is displayed!

like image 419
Rohan Sanap Avatar asked Nov 09 '22 17:11

Rohan Sanap


1 Answers

Your server probably blocks outgoing CURL requests, contact your provider and ask him if he could open access to the Facebook API server (graph.facebook.com). see this.

like image 194
mkm Avatar answered Nov 15 '22 06:11

mkm