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.
CURLOPT_URLisn't documentedhttp://admin_ain command-line andadmin_ain 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.User-Agentheader so try it withCURLOPT_USERAGENTdefined.