PHP file_get_contents ($ url) slow performance

My webpage uses Google charts to create five charts. If the charts are not cached yet, I have the following line of code that retrieves the image.

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

When this code executes in a loop, it takes 10 seconds to get each image. But if I change the code to use one of the Google IPs instead of the URL:

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

Images take less than one second. So, my initial thought was that DNS does not resolve the URL, and the delay is due to the cyclical use of the designated DNS servers? So I tried to execute ping chart.apis.google.com from the server and immediately answered a reasonable answer.

So my question is: is there any PHP (or Apache2) configuration parameter that might be missed that might cause this delay, or does this sound like a server configuration problem?

+6
php
source share
4 answers

Your DNS resolution is slow (the DNS used by your server can be a problem, then most other domains can be slow), or your server has problems using the DNS cache.

In any case, if you don’t have specific reasons for manipulating the image obtained from Google charts, why not just print it as an img tag? If you want, you can overlay texts or transparent png-s with css.

+7
source share

We have the same problem. It could be a DNS problem ... maybe an Apache server that uses too slow DNS servers.

I tried different ways: CURL, WGET (exec shell) ... and still getting the same performance issue.

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

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

+4
source share

This is a problem with the IP address of your hosting provider located in /etc/resolv.conf . You cannot fix it. This is the problem of your hosting provider.

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

 nameserver 8.8.8.8 

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

+2
source share

Why not allow ip before uploading images?

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

All Articles