1

I am using multiple local hostnames defined in the hosts file of my Windows machine.

For example: localhost, admin_a, admin_b, etc.

I would like to execute curl through PHP on such local hostnames, however the execution hangs and no result is returned.

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, 'http://localhost');
curl_setopt($ch, CURLOPT_RESOLVE, [ 'localhost:80:127.0.0.1']);
$output = curl_exec($ch);

I tried with the option CURLOPT_RESOLVE (not documented on php.net) and it does not seem to help.

On the other hand, the curl command line in a bash console works instantly:

curl http://localhost

What should I do to get PHP curl returns something on locally defined hostnames?

Server: nginx

UPDATE

The php console returns this:

Rebuilt URL to : http://localhost/
Trying ::1...
TCP_NODEDELAY set
Trying 127.0.0.1...
TCP_NODEDELAY set
Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
Host: localhost
Accept: */*

Then nothing happens, the page is just hanging returning nothing.

3
  • default protocol for the CURLOPT_URL isn't documented Commented Dec 28, 2017 at 2:32
  • You try http://admin_a in command-line and admin_a in PHP thus getting different results is not a surprise. Curl is not a consumer grade browser. You should not expect it to auto-correct or auto-complete anything. Commented Dec 28, 2017 at 9:35
  • curl cli sends User-Agent header so try it with CURLOPT_USERAGENT defined. Commented Dec 28, 2017 at 18:43

1 Answer 1

1

by default, curl try to guess the protocol based on the url, and my best guess is, it fails to guess the protocol for admin_a - you can probably get the same problem if you change your curl command to curl admin_a, anyway, change your setopt to curl_setopt($ch, CURLOPT_URL, 'https://admin_a'); - or alternatively, explicitly tell curl what to use with CURLOPT_DEFAULT_PROTOCOL, eg curl_setopt($ch,CURLOPT_DEFAULT_PROTOCOL,'https');

Sign up to request clarification or add additional context in comments.

6 Comments

I get no result using curl_setopt($ch, CURLOPT_URL, 'http://admin_a'); The script hangs and nothing is returned
@Vincent strange, enable CURLOPT_VERBOSE and run it again, and make sure to log what goes to stderr (by default in nginx+php-fpm, stderr goes to nginx's error logs)
I did that and I have updated my answer with the message returned in the PHP console.
@Vincent that verbose log indicates that your localhost webserver is not responding.. does it work if you load localhost in your web browser?
That is what exactly what I do not understand: if I access localhost in a separate tab, it works fine and return the proper message (a json string). I have 2 instances of php-cgi running, maybe it is an upstream issue with nginx. I don't where to look for anymore.
|

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.