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.

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
Batyuvitez
  • 11
  • 3

1 Answers1

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
Alessandro Benoit
  • 903
  • 10
  • 19
  • No, that's not the case. – Batyuvitez Nov 29 '19 at 10:11
  • 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). – Alessandro Benoit Nov 29 '19 at 10:14
  • 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](https://www.php.net/manual/en/function.curl-error.php))? – Alessandro Benoit Nov 29 '19 at 10:20
  • `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` – Batyuvitez Nov 29 '19 at 10:50
  • The return value is missing on your last comment... also other than `curl_error` could you return also `curl_errno` output (more info [here](https://www.php.net/manual/en/function.curl-errno.php))? – Alessandro Benoit Nov 29 '19 at 10:53
  • Alright, this seems a different problem alltogether. Have you already checked [this thread](https://stackoverflow.com/questions/46672070/php-curl-to-localhost-returns-permission-denied-on-an-open-port/46678301) ? – Alessandro Benoit Nov 29 '19 at 10:56
  • Thanks MiCc83 your last thread is solved the problem. How can I indicate that your answer solved my issue? – Batyuvitez Nov 29 '19 at 11:04
  • I've updated the answer to include the actual solution to the problem for those who'd come after. Not sure if you're supposed to mark the question solved by my answer so I'll leave it to you. Cheers! – Alessandro Benoit Nov 29 '19 at 11:17