繁体   English   中英

CURLOPT_RETURNTRANSFER没有返回响应

[英]CURLOPT_RETURNTRANSFER not returning response

我试图查询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;
}

您可以通过编写函数来解决它,如下所示:

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条件下工作,如文档中所述,CURLOPT_SSL_VERIFYHOST应设置为2以确保安全的SSL连接。

你的布尔值是假的吗?

如果错误,则必须出错。

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

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

在调用新的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);
}

使用print_r(curl_getinfo($this->handler)); 就在curl_exec($this->handler);之后curl_exec($this->handler); 找出问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM