简体   繁体   中英

Php curl is active but curl_exec always returns false

I'm trying to get data from external website using cURL in PHP but it's not working.

CURL is enabled in phpinfo() .

My test code is not working in CentOS 8/nginx server where the $result=FALSE , but works fine in Debian9/nginx .

$ch=curl_init ("http://somogyivakok.hu/");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec ($ch);
curl_close ($ch);

Do you have any idea what is wrong?

cURL Version is 7.61.

The resource you're looking for is not at somogyivakok.hu but at www.somogyivakok.hu . So you can use the correct uri or instruct the script to follow redirects using the CURLOPT_FOLLOWLOCATION option as per the following example:

$ch = curl_init ("http://somogyivakok.hu/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);

Update:

Investigating further, despite the issue with CURLOPT_FOLLOWLOCATION , as curl_exec returned false there is also an issue with the server configuration and more precisely with httpd_can_network_connect . Using CURLOPT_VERBOSE to debug the following is logged:

Immediate connect fail for 1.1.1.1: Permission denied

On this thread we've found the solution is to run the following command on the server shell:

sudo setsebool httpd_can_network_connect 1

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