CURLOPT_RETURNTRANSFER没有返回响应

时间:2012-12-11 06:44:45

标签: php apache curl instagram

我试图查询Instagram API,但CURLOPT_RETURNTRANSFER似乎没有像它想象的那样工作。 CURLOPT_RETURNTRANSFER已设置为true但我没有得到响应我得到的是一个布尔值。从我已经读过的CURLOPT_RETURNTRANSFER变为true,我已经在下面这样做了。请指教。谢谢

public function __construct($uri) {
    $this->handler = curl_init($uri);
           $this->_setOptions();
}

protected function _setOptions() {
    curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($this->handler, CURLOPT_USERAGENT, self::DEFAULT_USER_AGENT);

}

public function getResponse() {
    $response = curl_exec($this->handler);
    curl_close($this->handler);
    return $response;
}

4 个答案:

答案 0 :(得分:2)

您可以通过编写函数来解决它:

protected function _setOptions() {
    curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($this->handler, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->handler, CURLOPT_USERAGENT, self::DEFAULT_USER_AGENT);
}

这样你就可以避免检查ssl协议。

作为额外选项,您可以设置 CURLOPT_SSL_VERIFYHOST

curl_setopt($this->handler, CURLOPT_SSL_VERIFYHOST, 0);

建议始终在生产环境中的安全SSL条件下工作,如docs中所述,CURLOPT_SSL_VERIFYHOST应设置为2以确保安全的SSL连接。

答案 1 :(得分:0)

你的布尔值是假吗?

如果错误,则必须出错。

在curl_exec行之后添加以下内容:

if(curl_errno($this->handler)) 
{
   echo curl_error($this->handler);
}

答案 2 :(得分:0)

在调用新的ClassName('http://someurl.com')后调用getResponse()方法吗?

试试这个

$x = new ClassName('http://www.google.com');
var_export($x->getResponse();

编辑:你也可以调用添加错误输出方法来查看是否发生了一些卷曲错误

public function last_error() {
    echo curl_error($this->handler);
}

答案 3 :(得分:0)

print_r(curl_getinfo($this->handler));之后立即使用curl_exec($this->handler);来确定问题所在。