1

I have thousands of html pages which are handled as php.

inside each page, is a line:

<? file_get_contents("http://www.something.com/get_html.php?id=something"); ?>

for some reason, suddenly this line has been slowing down the server. When the page loads, it waits around 15 seconds at this line before proceeding.

The answer here works, namely,

$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
file_get_contents("http://www.something.com/somepage.html",false,$context);

which "tells the remote web server to close the connection when the download is complete".

However, this would require rewriting all the thousands of files. Is there a way to do the same thing from the get_html.php script?

this would be alot easier than rewriting all the pages. I tried sending

header("Connection: close"); in that script but no cigar.

To summarize, I am looking for the answer here but adapted to remote server side solution

Community
  • 1
  • 1
Ray S.
  • 1,192
  • 3
  • 15
  • 27

4 Answers4

0

You could easily do a find/replace in files in a certain directory with most editors. However, I would suggest you started caching results instead of poking your own or foreign servers subsequently for each request.

Elias
  • 1,532
  • 8
  • 19
  • it would be a big project, as not all files in the directories should be searched. cannot cache since the remote file also has url parameters passed to it, as above id=something – Ray S. Feb 10 '14 at 18:33
  • Then set up filters for the directories you want to have the replacements occur in. I don't see why you wouldn't be able to cache the results - unless the data is real-time. Just save the relevant data you are using hooked to the unique URL as a key in whatever data storage you desire. – Elias Feb 10 '14 at 18:40
0

Is the remote server outside of your local network? If not you could query the database or something else directly over your scripts without a http call. Else you could cache your search results in Memcache or files for a couple of time. It depends on the size and varity of your data how much memory is required for caching.

This are only two examples how to get faster response times. There are many approaches to do this.

agassner
  • 689
  • 8
  • 25
  • it's actually on the same server. but when i try using "include ABSOLUTE_PATH_TO_FILE" it gives an error of file not found, even though the path is actually correct. very strange. – Ray S. Feb 10 '14 at 19:15
0

you may try the following:

http://www.php.net/manual/en/function.override-function.php

don't know if you can change your server configuration

laxertu
  • 131
  • 3
0

Here are a couple of things for you to try. Try using cURL to make the request and see if it is still hanging up. Also, try fetching a different page on your site to see if it is also slow. These tests will help determine if it's that particular page or the connection that's hanging up. If another page is slow also, then modifying the 'get_html.php' page probably won't be much help.

To elaborate on Elias' answer, if the connection can easily be fixed by doing a find replace, you can use something like this from the command line in *nix:

perl -pi -w -e 's/search/replace/g;' *.php

-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

You'd have to test this out on a few files before doing all of them, but more specifically, you can use this to very quickly do a find/replace for all of your files:

perl -pi -w -e 's/(file_get_contents\("http:\/\/www.something.com\/somepage.html",false,\$context\)\;)/\$context = stream_context_create(array("http" => array("header" => "Connection: close\\r\\n")));\n$1/g;' *.php
Quixrick
  • 3,190
  • 1
  • 14
  • 17
  • even if i make the php file zero length, it still hangs for 15 seconds so it seems to be a connection problem. I could edit all the files, but would much prefer a single fix in the php script. – Ray S. Feb 10 '14 at 19:20
  • Is 'get_html.php' outputting anything? I'm wondering if the file has been moved/renamed since you can't call it locally either. This seems to match with this post here where the asker was getting a 15-second delay because no data was being returned from the server when he loaded the file. http://stackoverflow.com/questions/11060476/php-connection-close – Quixrick Feb 10 '14 at 19:53
  • get_html.php exists and returns data after 15 seconds. i even tried making it zero length, but still has the same problem. – Ray S. Feb 11 '14 at 18:30