Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP CURL is too slow for localhost

Tags:

php

curl

I have setup two apps in my WAMP environment. Performance of both the sites is OK. They return data in less than a second. The problem is that when I curl one site from another the curl takes around 20 seconds to respond. I have done profiling. Actually CURL request is taking all the time.

This should not be the case as both sites are setup on my machine and I am accessing using localhost/127.0.0.1 . Is it some sort of DNS resolve issue?

like image 391
Saqib Avatar asked Oct 11 '13 08:10

Saqib


4 Answers

I had the same problem and finally discover that it was coming from two facts :

  1. I use Mac OS X Mavericks
  2. I accessed my project via the URL http://myproject.local/ because I put a line 127.0.0.1 myproject.local in /etc/hosts

It is because the .local tld is reserved for Bonjour service, and this since Mac OS X Lion (10.7).

Changing the tld for something else fixed the problem.

like image 98
lepix Avatar answered Nov 09 '22 16:11

lepix


Yes, is a DNS lookup problem.

On Windows 7 x64 i managed to overcome this issue

(curl call between applications on different ports in localhost) by editing the host file ( C:\Windows\System32\drivers\etc ).

Un-comment on the line that says: 127.0.0.1 localhost , reboot and you're done.

like image 23
Leonardo Atalla Avatar answered Nov 09 '22 16:11

Leonardo Atalla


I solved this problem by setting up different virtual hosts such as sitea.dev and siteb.dev If you use the same host for both sites on your localhost Windows machine, then you get a long delay (the same as if there is a Fatal PHP error). It's not a problem with Curl since file_get_contents(url) gave me the same issue.

like image 24
Andy Avatar answered Nov 09 '22 16:11

Andy


If you are using *.local TLD, you can solve this and enjoy fast loading times by adding both, IPv4 and IPv6 entries in the hosts file

127.0.0.1   test.local
::1         test.local

Best

like image 38
richterd Avatar answered Nov 09 '22 16:11

richterd