2016-09-15 112 views
1

我正在为此工作2个小时,只是无法弄清楚什么是错的。我只是通过URL(带参数)发出cURL get请求,并且响应预期会是JSON格式的某种access_token,但我不断收到错误:curl_exec()函数返回false。该URL是正确的,因为直接将准备好的URL粘贴到浏览器地址栏会给出适当的access_token。您可能需要知道我正在制作图表API(Facebook)请求。下面是一些代码:在简单的GET请求中返回false的cURL函数

private function getAccessToken() { 
      $uri = $this->prepareTokenUri(); // getting the uri 
      echo "<strong>$uri</strong><br/>"; // printing the uri for debugging purpose 
      $this->setCurlToGet($uri); // explained below 
      $response = curl_exec($this->curl); 
      echo "<b>Here Goes Response</b>"; 
      var_dump($response); // boolean false 
      $response = json_decode($response, true); 
      $this->token_expires = $response['expires_in']; 
      $this->token_type = $response['type']; 
      return $response['access_token']; 
     } 

功能setCurlToGet()只是执行以下操作:

// call this function only when making a GET request 
private function setCurlToGet($url) { 
      $this->unsetCurl(); 
      $this->curl = curl_init(); 
      curl_setopt($this->curl, CURLOPT_URL, $url); 
      curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); 
      return $this->curl; 
     } 

unsetCurl()方法如下:

private function unsetCurl() { 

      if(!is_null($this->curl)) { 
       curl_close($this->curl); 
       $this->curl = null; 
      } 
     } 

我已经var_dumped()应有尽有, cURL资源变量($this->curl),它实际上是一个卷曲资源变量。致电curl_exec()返回false,我只是不明白为什么。再一次,我想重申一下,URL没有什么不对,因为当打印的URL(在行echo "<strong>$uri</strong><br/>";)被复制并粘贴到浏览器的地址栏中时,结果就是我需要的access_token。

如果你想看到URI的图案prepareTokenUri()准备:

https://graph.facebook.com/v2.3/oauth/access_token?client_id={my-appid}&redirect_uri={response-handler-script-uri}&client_secret={app-secret-code}&code={a-long-code}

+3

是否'curl_error()'说明了什么? – AbraCadaver

+0

应该理想地工作。做私人公开只是为了检查 –

+0

@AbraCadaver刚刚检查过。它说'SSL证书问题:无法获得本地签发者证书'。现在该如何解决这件事? – Fresher

回答

2

快速修复:

这些行添加到您的setCurlToGet功能在同一地区的其他curl_setopt方法。

curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); 

与此唯一的问题是,如果有人能够使DNS去他们的服务器,而不是Facebook的从URL预期,你是不是验证它确实是Facebook的。

所以,如果你担心的是,正确的解决办法:

1)https://curl.haxx.se/ca/cacert.pem

2下载cacert.pem)添加以下行为php.ini,与在那里你把正确的路径上述文件

curl.cainfo=/path/to/cacert.pem 

3)刷新apache服务