我正在为此工作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}
是否'curl_error()'说明了什么? – AbraCadaver
应该理想地工作。做私人公开只是为了检查 –
@AbraCadaver刚刚检查过。它说'SSL证书问题:无法获得本地签发者证书'。现在该如何解决这件事? – Fresher