简体   繁体   中英

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. The CURLOPT_RETURNTRANSFER has been set to true but i do not get a response all I get is a boolean. From what i have read the CURLOPT_RETURNTRANSFER to true which i have done so below. 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.

As an extra option you could set 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.

Is your Boolean false?

If its false, you must have an error.

Add the folowing after your curl_exec line:

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

Are you calling the getResponse() method after you call new ClassName('http://someurl.com')?

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)); right after curl_exec($this->handler); to figure what the problem is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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