6

My web page uses Google charts to produce five graphs. If the graphs are not already cached, I have the following line of code that retrieves the image.

$image = file_get_contents("http://chart.apis.google.com/chart?".$query);

When that code is executed in a loop, it takes 10 seconds to get each image. But, if I change the code as to use one of Google's ip addresses instead of the url:

$image = file_get_contents("http://74.125.93.100/chart?".$query);

It takes less than one second to retrieve the images. So my initial thought was that DNS is not resolving the URL and the delay is from cycling through the assigned DNS servers? So I tried to ping chart.apis.google.com from the server and it immediately returned a reasonable response.

So my question is: Is there any PHP (or Apache2) configuration setting that I may be overlooking that may cause this delay, or does this sound like a server configuration issue?

Kevin
  • 133
  • 1
  • 1
  • 6
  • What if you use another method for retrieving the image such as cURL? – Matt Apr 09 '09 at 17:14
  • Which version of PHP? Any chance you can upgrade to the latest to see if that fixes the problem? – Nick Presta Apr 09 '09 at 19:41
  • I'd do some testing to see if DNS is actually the issue. You can wrap your API calls in a timing block. – Travis Leleu Apr 10 '09 at 00:40
  • Thanks to all who have replied. The issue is certainly DNS related and I am on a mission to figure out if its tied to PHP or Apache. I've tested the server using different DNS servers to no avail. Will update if I find anything further. – Kevin Apr 10 '09 at 19:14
  • Please look here http://stackoverflow.com/questions/3629504/php-file-get-contents-very-slow-when-using-full-url –  May 17 '13 at 06:07

4 Answers4

7

Your DNS resolving is slow (the DNS your server is using can be a broblem, then most of the other domains could be slow) or your server has problems using the DNS cache.

In any case, if you don't have some specific reasons to manipulate the image received from google charts, why don't you just print out it as an img tag? You can overlay texts or transparent png-s with css if you want.

Csaba Kétszeri
  • 674
  • 3
  • 6
4

Got the same problem here. It might be a DNS issue... maybe the apache server which use DNS servers that are too slow.

i have tried differents ways: CURL, WGET (shell exec)... and still got the same performance issue.

It takes about 15 seconds on my production server. But on my local server (which uses IP) takes less than 1.5 seconds with my script.

try /etc/resolv.conf or /etc/named.conf? maybe. I am trying to find a solution.

  • 5
    I had the same exact problem as you, it seems. My solution turned out to be editing my /etc/resolv.conf to use Google's nameservers (i.e. `nameserver 8.8.8.8`). – Steven Oxley Jun 16 '10 at 22:26
  • @Steven Oxley THANK YOU VERY MUCH!!!! I spent 2 hours to find solution of this problem, and only your advice was useful. – OZ_ Jun 22 '11 at 02:15
2

It is a problem related to the IP your hosting provider put in /etc/resolv.conf. You can not repair it. It is a problem of your hosting provider.

But you may use the google public dns: 8.8.8.8. Open /etc/resolv.conf, then delete all data and write:

nameserver 8.8.8.8

Then save it. Restart dns and apache. Then try again.

Cosmin
  • 21,216
  • 5
  • 45
  • 60
wap360.net
  • 21
  • 1
1

Why not resolve the ip before start to load the images?

$ip = gethostbyname($name);
$image = file_get_contents($ip."/chart?".$query);
TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • Yes, that would work but would still cost ~10 seconds which is unacceptable. I'm convinced its a config issue, just not sure if it sounds like a PHP, Apache, or server config issue. – Kevin Apr 09 '09 at 17:20
  • It would work only if on that server is hosted only one site. In multisite environment server would'n know which site are you calling – Alekc Apr 09 '09 at 19:41