Fatal error: Uncaught CurlException: 7: couldn't connect to host thrown in …src/base_facebook.php on line 887

杀马特。学长 韩版系。学妹 提交于 2019-12-08 15:55:21

问题


I'm trying to develop a sample facebook php login example following the example here

I've hosted my app here, but I'm getting the error message in the question whenever I try to access the link. Here's the code segment that throws the error

try {
  $e = new FacebookApiException(array(// LINE 887
             'error_code' => curl_errno($ch),
             'error' => array(
             'message' => curl_error($ch),
             'type' => 'CurlException',
        ),
  ));
  curl_close($ch);
}

// edit suggested by Kneel-before ZOD
catch(FacebookApiException $e) {
  $result = $e->getResult();
  echo 'Got an : ', $e->getType(),' while posting';
  echo(json_encode($result));
}

catch(Exception $e){
  echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Im quite sure Ive setup the APP ID and secret correctly in index.php.

Here's a screenshot of my app setup on facebook

Any help would be appreciated. Thanks!


回答1:


You may need to give curl a certificate file for Facebook which you do like this:

Download the certificate files from https://github.com/facebookarchive/facebook-php-sdk/blob/master/src/fb_ca_chain_bundle.crt

and add this before attempting any Facebook calls

facebook::$CURL_OPTS[CURLOPT_CAINFO] = 'path/to/fb_ca_chain_bundle.crt';

You could also try

facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;



回答2:


why are you creating the exception on the try block?

my current working login function goes something like this:

public function login()
{               
    $this->load->library('facebook/facebook',$config);
    $uid = $this->facebook->getUser();

    if ($uid) 
    {           
      try 
      {             
        $user_profile = $this->facebook->api('/' . $uid);  

        //then redirect to whatever you need..
      } 
      catch (FacebookApiException $e) 
      { error_log($e);
        $uid = null;
        $this->facebook->destroySession();
      }
    }
    else 
    {
        $params = array(
            'canvas'    => 1,
            'fbconnect' => 0,
            'scope' => 'email',
        );
        echo "<script>top.location.href='" . $this->facebook->getLoginUrl($params)."'</script>";            
        exit();
    }
}



回答3:


I had the same problem like you.

I add this option before curl_exec() in base_facebook.php

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);



回答4:


If you are using free hosting , I think it may not going to work, mostly because they usually disabled outgoing connection. I also have faced this problem with the code that worked smoothly on paid hosting(or outgoing connection enabled).




回答5:


You are creating an exception in try ... catch block. You don't need to do such things. try-catch block designed to handle code segments which can throw an exception in your applications. You need to put inside this block code segments that can throw an exception. In your case it may be a cURL request to the Facebook API.




回答6:


The link is https you may have to supply the certificate info or curl will usually throw an error if you don't have access to the certificate then adding

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

will allow access to any certificate.




回答7:


Set Proxy To Connect Facebook.com

## Find Code In base_facebook.php ##

public static $CURL_OPTS = array(
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 60,
        CURLOPT_USERAGENT      => 'facebook-php-3.2',
      );

And Add option

  public static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_USERAGENT      => 'facebook-php-3.2',
    CURLOPT_PROXY      => '199.200.120.140:8089',
  );



回答8:


As suggested by Ashutosh Bajpay, it's likely that your web server is unable to connect based on the error message. If you're using a shared hosting provider, or have SELinux enabled, it's highly plausible that your code will not be able to initiate a new connection out.

You can write a quick and dirty script to test this theory:

$page = file_get_contents('http://google.com');
(!empty($page)) ? var_dump($page) : print_r(error_get_last());

If the script can connect, you'll get a dump of the page, otherwise you should see an error with it attempting to get to the host.

If you do have control of the box, and it happens to be running SELinux there are some useful tips here.




回答9:


Check your php.ini allows curl function.

For ubantu try to this code in your terminal

sudo apt-get install php5-curl

After it restart your apache server using

sudo service apache2 restart




回答10:


You are catching two exception types, neither is the exception that is "thrown". Try catching the exception that is thrown and within that exception block you'll be able to further debug your problem (such as showing the variables in play during the exception ...) then you can build a simple page to test each piece of the puzzle until you fix it.




回答11:


It has nothing to do with your code. Curl simply can't connect to the facebook graph API. This could either be because your host is blocking outgoing connections to other hosts or facebook is blocking your host.

If you have ssh access you could try to reach http://graph.facebook.com and/ or check your php logs. Otherwise you should ask your host.



来源:https://stackoverflow.com/questions/18865077/fatal-error-uncaught-curlexception-7-couldnt-connect-to-host-thrown-in-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!