Vincent
Vincent

Reputation: 1760

PHP curl does not resolve local host names

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.

Upvotes: 1

Views: 2022

Answers (1)

hanshenrik
hanshenrik

Reputation: 21653

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');

Upvotes: 1

Related Questions