Kevin
Kevin

Reputation: 133

PHP file_get_contents($url) slow performance

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?

Upvotes: 6

Views: 11713

Answers (4)

wap360.net
wap360.net

Reputation: 21

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.

Upvotes: 2

david
david

Reputation:

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.

Upvotes: 4

Csaba Kétszeri
Csaba Kétszeri

Reputation: 694

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.

Upvotes: 7

TheHippo
TheHippo

Reputation: 63179

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

$ip = gethostbyname($name);
$image = file_get_contents($ip."/chart?".$query);

Upvotes: 1

Related Questions