简体   繁体   中英

curl_exec always returns 0?

I have this function:

function Connect($url, $post = 0, $postfields = '') 
{
    $ch = curl_init();
    if($post > 0) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    } 
    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/joomla-cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/joomla-cookie.txt');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $exec = curl_exec($ch);
    if($exec) { return $exec; } else { return 0; }
}

I call it like that Connect($host)

And it always return 0...

Try setting CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false. that should do the trick.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Check if there was an error with the request after curl_exec:

if(curl_errno($ch)){
    echo 'Curl error: ' . curl_error($ch);
}

That will provide you with enough info to know if there was a error with the request. If there was no error, you can check the request sent after curl_exec so you can double check that everything sent is in place:

print_r(curl_getinfo($ch));

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