0

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.

2
  • 2
    Possible duplicate of PHP - Debugging Curl Commented Nov 27, 2019 at 15:05
  • I'm having the exact same issue and i'd love to know how you solved it Commented Jul 13, 2020 at 19:38

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

8 Comments

No, that's not the case.
Definitely the case as I actually tested my code :) Without CURLOPT_FOLLOWLOCATION I was getting an empty string indeed. http://somogyivakok.hu/ is redirecting with a 301 to http://www.somogyivakok.hu/ (Ps. If you're using Chrome the www part is hidden by the browser).
Btw, I've just realized that if curl_exec returns false there might be some other error. Could you use curl_error and provide the returned error (check the first example here)?
Curl_error returns an empty string. I also debugged with CURLOPT_VERBOSE returns the following: Trying 94.199.183.214... * TCP_NODELAY set * Immediate connect fail for 94.199.183.214: Permission denied * Closing connection 0
The return value is missing on your last comment... also other than curl_error could you return also curl_errno output (more info here)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.