[英]CURLOPT_RETURNTRANSFER not returning response
I have trying to query an Instagram API but the CURLOPT_RETURNTRANSFER does not seems to work the way it is suppose to. 我试图查询Instagram API,但CURLOPT_RETURNTRANSFER似乎没有像它想象的那样工作。 The CURLOPT_RETURNTRANSFER has been set to true but i do not get a response all I get is a boolean. CURLOPT_RETURNTRANSFER已设置为true但我没有得到响应我得到的是一个布尔值。 From what i have read the CURLOPT_RETURNTRANSFER to true which i have done so below. 从我已经读过的CURLOPT_RETURNTRANSFER变为true,我已经在下面这样做了。 Please advice. 请指教。 Thanks 谢谢
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;
}
You might solve it by writing your function as follows: 您可以通过编写函数来解决它,如下所示:
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);
}
this way you avoid to check the ssl protocol. 这样你就可以避免检查ssl协议。
As an extra option you could set CURLOPT_SSL_VERIFYHOST : 作为额外选项,您可以设置CURLOPT_SSL_VERIFYHOST :
curl_setopt($this->handler, CURLOPT_SSL_VERIFYHOST, 0);
It is recommended to always work under secure SSL conditions in production environments, as it is mentioned in the docs , CURLOPT_SSL_VERIFYHOST should be set to 2 to ensure a secure SSL connection. 建议始终在生产环境中的安全SSL条件下工作,如文档中所述,CURLOPT_SSL_VERIFYHOST应设置为2以确保安全的SSL连接。
Is your Boolean false? 你的布尔值是假的吗?
If its false, you must have an error. 如果错误,则必须出错。
Add the folowing after your curl_exec line: 在curl_exec行之后添加以下内容:
if(curl_errno($this->handler))
{
echo curl_error($this->handler);
}
Are you calling the getResponse() method after you call new ClassName('http://someurl.com')? 在调用新的ClassName('http://someurl.com')后调用getResponse()方法吗?
try this 尝试这个
$x = new ClassName('http://www.google.com');
var_export($x->getResponse();
EDIT: you can also call add an error output method to see if some curl error happened 编辑:您还可以调用添加错误输出方法来查看是否发生了一些卷曲错误
public function last_error() {
echo curl_error($this->handler);
}
Use print_r(curl_getinfo($this->handler));
使用print_r(curl_getinfo($this->handler));
right after curl_exec($this->handler);
就在curl_exec($this->handler);
之后curl_exec($this->handler);
to figure what the problem is. 找出问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.