cURL函数在简单GET请求中返回false

时间:2016-09-15 17:30:11

标签: php curl libcurl

我正在研究这个问题2个小时,却无法弄清楚出了什么问题。我正在使用URL(带参数)进行cURL get请求,并且响应应该是JSON格式的某种access_token但我不断收到错误:curl_exec()函数正在返回{{1} }。 URL是正常的,因为直接将准备好的URL粘贴到浏览器地址栏会给出相应的access_token。您可能需要知道我正在制作图形API(Facebook)请求。这是一些代码:

false

函数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()

我有var_dumped()所有东西,cURL资源变量(private function unsetCurl() { if(!is_null($this->curl)) { curl_close($this->curl); $this->curl = null; } } ),它实际上是一个curl资源变量。对$this->curl的调用返回false,我无法弄清楚原因。同样,我想重复一遍,URL没有任何问题,因为当打印的网址(在行curl_exec()中)被复制并粘贴到浏览器的地址栏中时,结果是我需要的access_token。

如果您想查看echo "<strong>$uri</strong><br/>";正在准备的URI模式:

prepareTokenUri()

1 个答案:

答案 0 :(得分:2)

快速修复:

将这些行添加到与其他curl_setopt方法相同的区域中的setCurlToGet函数。

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

唯一的问题是,如果有人能够将dns转到他们的服务器,而不是像网址预期的那样,那么你并没有确认它确实是facebook。

所以,如果您对此感到担心,那么正确的解决方法:

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

下载cacert.pem

2)将以下行添加到php.ini中,其中包含放置上述文件的正确路径

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

3)重新加载apache服务

相关问题