I am working with a script (that I did not create originally) that generates a pdf file from an HTML page. The problem is that it is now taking a very long time, like 1-2 minutes, to process. Supposedly this was working fine originally, but has slowed down within the past couple of weeks.
The script calls file_get_contents on a php script, which then outputs the result into an HTML file on the server, and runs the pdf generator app on that file.
I seem to have narrowed down the problem to the file_get_contents call on a full url, rather than a local path.
When I use
$content = file_get_contents('test.txt');
it processes almost instantaneously. However, if I use the full url
$content = file_get_contents('http://example.com/test.txt');
it takes anywhere from 30-90 seconds to process.
It's not limited to our server, it is slow when accessing any external url, such as http://www.google.com. I believe the script calls the full url because there are query string variables that are necessary that don't work if you call the file locally.
I also tried fopen, readfile, and curl, and they were all similarly slow. Any ideas on where to look to fix this?
Note: This has been fixed in PHP 5.6.14. A Connection: close header will now automatically be sent even for HTTP/1.0 requests. See commit 4b1dff6.
I had a hard time figuring out the cause of the slowness of file_get_contents scripts.
By analyzing it with Wireshark, the issue (in my case and probably yours too) was that the remote web server DIDN'T CLOSE THE TCP CONNECTION UNTIL 15 SECONDS (i.e. "keep-alive").
Indeed, file_get_contents doesn't send a "connection" HTTP header, so the remote web server considers by default that's it's a keep-alive connection and doesn't close the TCP stream until 15 seconds (It might not be a standard value - depends on the server conf).
A normal browser would consider the page is fully loaded if the HTTP payload length reaches the length specified in the response Content-Length HTTP header. File_get_contents doesn't do this and that's a shame.
SOLUTION
SO, if you want to know the solution, here it is:
$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
file_get_contents("http://www.something.com/somepage.html",false,$context);
The thing is just to tell the remote web server to close the connection when the download is complete, as file_get_contents isn't intelligent enough to do it by itself using the response Content-Length HTTP header.
I would use curl() to fetch external content, as this is much quicker than the file_get_contents method. Not sure if this will solve the issue, but worth a shot.
Also note that your servers speed will effect the time it takes to retrieve the file.
Here is an example of usage:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
Sometimes, it's because the DNS is too slow on your server, try this:
replace
echo file_get_contents('http://www.google.com');
as
$context=stream_context_create(array('http' => array('header'=>"Host: www.google.com\r\n")));
echo file_get_contents('http://74.125.71.103', false, $context);
I had the same issue,
The only thing that worked for me is setting timeout in $options array.
$options = array(
'http' => array(
'header' => implode($headers, "\r\n"),
'method' => 'POST',
'content' => '',
'timeout' => .5
),
);
$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
$string = file_get_contents("http://localhost/testcall/request.php",false,$context);
Time: 50976 ms (avaerage time in total 5 attempts)
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, "http://localhost/testcall/request.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
echo $data = curl_exec($ch);
curl_close($ch);
Time: 46679 ms (avaerage time in total 5 attempts)
Note: request.php is used to fetch some data from mysql database.
Can you try fetching that url, on the server, from the command line? curl or wget come to mind. If those retrieve the URL at a normal speed, then it's not a network problem and most likely something in the apache/php setup.
I have a huge data passed by API, I'm using file_get_contents to read the data, but it took around 60 seconds. However, using KrisWebDev's solution it took around 25 seconds.
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
file_get_contents($url,false,$context);
What I would also consider with Curl is that you can "thread" the requests. This has helped me immensely as I do not have access to a version of PHP that allows threading at the moment .
For example, I was getting 7 images from a remote server using file_get_contents and it was taking 2-5 seconds per request. This process alone was adding 30seconds or something to the process, while the user waited for the PDF to be generated.
This literally reduced the time to about 1 image. Another example, I verify 36 urls in the time it took before to do one. I think you get the point. :-)
$timeout = 30;
$retTxfr = 1;
$user = '';
$pass = '';
$master = curl_multi_init();
$node_count = count($curlList);
$keys = array("url");
for ($i = 0; $i < $node_count; $i++) {
foreach ($keys as $key) {
if (empty($curlList[$i][$key])) continue;
$ch[$i][$key] = curl_init($curlList[$i][$key]);
curl_setopt($ch[$i][$key], CURLOPT_TIMEOUT, $timeout); // -- timeout after X seconds
curl_setopt($ch[$i][$key], CURLOPT_RETURNTRANSFER, $retTxfr);
curl_setopt($ch[$i][$key], CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch[$i][$key], CURLOPT_USERPWD, "{$user}:{$pass}");
curl_setopt($ch[$i][$key], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($master, $ch[$i][$key]);
}
}
// -- get all requests at once, finish when done or timeout met --
do { curl_multi_exec($master, $running); }
while ($running > 0);
Then check over the results:
if ((int)curl_getinfo($ch[$i][$key], CURLINFO_HTTP_CODE) > 399 || empty($results[$i][$key])) {
unset($results[$i][$key]);
} else {
$results[$i]["options"] = $curlList[$i]["options"];
}
curl_multi_remove_handle($master, $ch[$i][$key]);
curl_close($ch[$i][$key]);
then close file:
curl_multi_close($master);
I know that is old question but I found it today and answers didn't work for me. I didn't see anyone saying that max connections per IP may be set to 1. That way you are doing API request and API is doing another request because you use full url. That's why loading directly from disc works. For me that fixed a problem:
if (strpos($file->url, env('APP_URL')) === 0) {
$url = substr($file->url, strlen(env('APP_URL')));
} else {
$url = $file->url;
}
return file_get_contents($url);
I use curl to retrieve web content from another site but there is two problems;
first, it takes an average 4 sec period to retrieve the contents and I don't know if there is any way to reduce it; second, some times remote server returns a short error message instead of full content.
I was thinking it would very efficient to send 3 request simultaneously, then check the first received response to see if it's an error or not and check the second respond if the first one was an error. by this method it would not be need to wait a full 4 secs to get retry response if the first one was error. but I don't know if it's possible to doing it.
It would be very appreciated if any one write a simple code to send multiple request simultaneously.
Here is my initial code:
<?php
$then = microtime(true);
$url='http://www.example.com/StockInformationHandler.ashx?{%22Type%22:%22getstockprice%22,%22la%22:%22En%22,%22arr%22:%22IRO1LKGH0001,IRO1MADN0001,IRO1KAVR0001,IRO1BHMN0001,IRO1PNBA0001,IRO3ASPZ0001,IRO1IKCO0001,IRO1BANK0001,IRO1IKHR0001,IRO1BDAN0001,IRO1SHND0001,IRO1NBEH0001,IRO1PIAZ0001,IRO1RSAP0001,IRO3DTDZ0001,IRO1TSBE0001,IRO1NAFT0001,IRO1PKHA0001,IRO1AZAB0001,IRO3BMAZ0001,IRO1BANS0001,IRO1BAFG0001,IRO1TAYD0001,IRO1GDIR0001,IRO1SPDZ0001,IRO1NALM0001,IRO1TOSA0001,IRO1BSDR0001,IRO1ZMYD0001,IRO1SBEH0001,IRO3SMBZ0001,IRO1PASN0001,IRO1SPAH0001,IRO1PNES0001,IRO1ALIR0001,IRO1MKBT0001,IRO1FKHZ0001,IRO1RENA0001,IRO1SSHR0001,IRO1PKER0001,IRO1SAHD0001,IRO1BTEJ0001,IRO1DADE0001,IRO1PARK0001,IRO1SKRN0001,IRO1FOLD0001,IRO3KHMZ0001,IRO1NASI0001,IRO3FAYZ0001,IRO1ALMR0001,IRO1NSTH0001,IRO1BMLT0001,IRO1TKSM0001,IRO1AYEG0001,IRO1GTSH0001,IRO1COMB0001,IRO3IMFZ0001,IRO1INDM0001,IRO1DSOB0001,IRO3NPSZ0001,IRO1BPAS0001,IRO1PKOD0001,IRO1OFST0001,IRO3NOLZ0001,IRO1RIIR0001,IRO1ATDM0001,IRO1SNMA0001,IRO1VSIN0001,IRO1MNGZ0001,IRO3PSKZ0001,IRO3KRMZ0001,IRO1AMIN0001,IRO1PRDZ0001,IRO1GLOR0001,IRO1SAJN0001,IRO3BGHZ0001,IRO1PAKS0001,IRO1SIPA0001,IRO1PLKK0001,IRO1KSHJ0001,IRO3BDYZ0001,IRO1LEAB0001,IRO1KHSH0001,IRO1KRIR0001,IRO1PKLJ0001,IRO1HTOK0001,IRO1BPST0001,IRO1TAMI0001,IRO1DTIP0001,IRO1SAND0001,IRO1SHPZ0001,IRO1SHKR0001,IRO1CHAR0001,IRO1ALBZ0001,IRO1LMIR0001,IRO1TRIR0001,IRO3ETLZ0001,IRO1CRBN0001,IRO1SAKH0001,IRO3MSZ93011,IRO1NSAZ0001,IRO3BLSZ0001,IRO1MSMI0001,IRO1TMEL0001,IRO1BALB0001,IRO1LZIN0001,IRO3RFNZ0001,IRO1ABAD0001,IRO1LSMD0001,IRO1DPAK0001,IRO1PLAK0001,IRO1MAVA0001,IRO1GSBE0001,IRO3BIPZ0001,IRO1PSIR0001,IRO1BALI0001,IRO1LIRZ0001,IRO3PKSH0001,IRO1NOVN0001,IRO3GRDZ0001,IRO3PMRZ0001,IRO1ROZD0001,IRO1PABD0001,IRO1SSEP0001,IRO1SGRB0001,IRO1NSPS0001,IRO3ARFZ0001,IRO1GNBO0001,IRO1KHAZ0001,IRO1KIMI0001,IRO1SSIN0001,IRO1PETR0001,IRO1BSTE0001,IRO3DSOZ0001,IRO1TSHE0001,IRO1NMOH0001,IRO1SBAH0001,IRO1TMKH0001,IRO3MNOZ0001,IRO1MAPN0001,IRO1SGAZ0001,IRO1MAGS0001,IRO3PRZZ0001,IRO1HSHM0001,IRO3MSZ93021,IRO1KGND0001,IRO1SGOS0001,IRO1SISH0001,IRO1SKER0001,IRO1KCHI0001,IRO3MRJZ0001,IRO1FRVR0001,IRO1GHEG0001,IRO1KRTI0001,IRO1RINM0001,IRO1GHAT0001,IRO1PSHZ0001,IRO1PNTB0001,IRO1KNRZ0001,IRO1BOTA0001,IRO1GOLG0001,IRT3SATF0001,IRO3AFRZ0001,IRR3ASPZ0101,IRO7ARMP0001,IRO3ZF090001,IRT3SSAF0001,IRT3CASF0001,IRO1ASIA0001,IRO1CONT0001,IRO7BHEP0001,IRO1BAKH0001,IRO1KALZ0001,IRO1KBLI0001,IRO1TRNS0001,IRO1KTAK0001,IRO3BSMZ0001,IRR3BSMZ0101,IRO1SWIC0001,IRO1LAPS0001,IRO1JOSH0001,IRO1MOTJ0001,IRR1MOTJ0101,IRO7MILP0001,IRO1NIRO0001,IRO7BHPP0001,IRO3ZF180001,IRO1ARTA0001,IRO1IPAR0001,IRO1YASA0001,IRO1PASH0001,IRO1TAIR0001,IRO3ZF340001,IRO3ZF140001,IRO7IPTP0001,IRR7IPTP0101,IRO3ZF280001,IRO1DRKH0001,IRO3ZF040001,IRO7SHIP0001,IRO1BARZ0001,IRO1PLST0001,IRO1GAZL0001,IRO1PTAP0001,IRO7HPKP0001,IRO1PIRN0001,IRO7GSIP0001,IRO1MOZI0001,IRO3MSZ93031,IRO3MSZ93041,IRO3MSZ93051,IRO3MSZ93061,IRO3MSZ93071,IRO3MSZ93081,IRO3MSZ93091,IRO3MSZ93101,IRO3MSZ93111,IRO3MSZ93121,IRO3MSZ94011,IRO3MSZ94021,IRO3MSZ94031,IRO3MSZ94041,IRO3MSZ94051,IRO1FROZ0001,IRO1GSKE0001,IRO7NARP0001,IRO1TKNO0001,IRO1MNMH0001,IRO3TORZ0001,IRO1SESF0001,IRO3PMTZ0001,IRO1PMSZ0001,IRO3OSHZ0001,IRO3SGDZ0001,IRO3CGRZ0001,IRO1OFRS0001,IRO1MSKN0001,IRO3PJMZ0001,IRO7BPRP0001,IRO1FIBR0001,IRO1KMSH0001,IRO1KSKA0001,IRO3ZF160001,IRO1NEOP0001,IRO1HJPT0001,IRO3KHZZ0001,IRO7RAHP0001,IRO1HFRS0001,IRO1KFJR0001,IRO7BHKP0001,IRO1AZIN0001,IRO1ATIR0001,IRO1SZPO0001,IRO1RTIR0001,IRO1RADI0001,IRR1CHAR0101,IRO3ZF030001,IRO1FNAR0001,IRO7SDRP0001,IRO1IDOC0001,IRO1KFAN0001,IRO1GOST0001,IRO1LENT0001,IRO1MHKM0001,IRO1MSTI0001,IRO1MNSR0001,IRR1IKCO0101,IRO1MESI0001,IRO1DABO0001,IRO1DOSE0001,IRO1DALZ0001,IRO1PDRO0001,IRO1TMVD0001,IRO1THDR0001,IRO1DJBR0001,IRO7DHVP0001,IRO1DAML0001,IRO1DRZK0001,IRO1DZAH0001,IRO1DSIN0001,IRO1DDPK0001,IRO1ABDI0001,IRO1DFRB0001,IRO1FTIR0001,IRO1DKSR0001,IRO1EXIR0001,IRO1DLGM0001,IRO7PDHP0001,IRO1IRDR0001,IRO3ZOBZ0001,IRO1INFO0001,IRO1EPRS0001,IRO1TKIN0001,IRO1RKSH0001,IRO3ZF120001,IRO3ZF240001,IRO3PZGZ0001,IRO7ZNJP0001,IRO3ZAGZ0001,IRO1AZRT0001,IRO1SDAB0001,IRO1SADB0001,IRO1SURO0001,IRO7IRNP0001,IRO7CBBP0001,IRO1SBOJ0001,IRO3SBZZ0001,IRO1SBHN0001,IRO1PRMT0001,IRO1STEH0001,IRO7SASP0001,IRO1SKHS0001,IRO1SKAZ0001,IRO7SEKP0001,IRO3DBRZ0001,IRO1SDST0001,IRO1SDOR0001,IRO1SROD0001,IRR1SSHR0101,IRO1SIMS0001,IRO1SEFH0001,IRO1SSOF0001,IRB3SSHZ9241,IRO1SFRS0001,IRO1SFKZ0001,IRO1FRDO0001,IRO1SFAS0001,IRO1SFNO0001,IRO1SGEN0001,IRO3SKBZ0001,IRO1SKOR0001,IRO1SMAZ0001,IRO3IBKZ0001,IRO7IENP0001,IRO1SSNR0001,IRO1SHZG0001,IRO1SHGN0001,IRO3SYSZ0001,IRO1SEIL0001,IRO1AMLH0001,IRO3PNLZ0001,IRO1BMPS0001,IRO1PPAM0001,IRO3PTRZ0001,IRO1THSH0001,IRO1TOPI0001,IRO1DODE0001,IRO1SHRG0001,IRO1ZNGN0001,IRO1SEPP0001,IRO7STNP0001,IRO1TSAL0001,IRO1SHSI0001,IRO1PESF0001,IRO1PFRB0001,IRO1SHFS0001,IRR1SHFS0101,IRO1PFAN0001,IRO7PKBP0001,IRO1KAFF0001,IRO1NKOL0001,IRO7SHLP0001,IRO1NPRS0001,IRO1VASH0001,IRT1CSNF0001,IRO1KLBR0001,IRO1BENN0001,IRO1LPAK0001,IRO1MINO0001,IRO1CHCH0001,IRO1KDPS0001,IRO1DMOR0001,IRO1SLMN0001,IRO1SPKH0001,IRO1SPPE0001,IRO1SHAD0001,IRO7MINP0001,IRO1GORJ0001,IRO1GCOZ0001,IRO1MRGN0001,IRO1MRAM0001,IRO1RNAB0001,IRO1NOSH0001,IRO1KIVN0001,IRO1MARK0001,IRO1KSIM0001,IRO1ALTK0001,IRO1SAMA0001,IRO1BAHN0001,IRO1BMAS0001,IRO1BIRI0001,IRO1SPTA0001,IRO1JAMD0001,IRO1FAJR0001,IRO1JSHO0001,IRO1FKAS0001,IRO1FRIS0001,IRO1TFKR0001,IRO3ZF100001,IRO3KZIZ0001,IRO1SEPA0001,IRO1LSDD0001,IRO1SORB0001,IRO1SOLI0001,IRO1LAMI0001,IRO7FANP0001,IRO1NGFO0001,IRO1FVAN0001,IRO1FAIR0001,IRO1GPRS0001,IRO1GPSH0001,IRO3CHRZ0001,IRO1GGAZ0001,IRO1GSHI0001,IRO1GHND0001,IRO3GHSZ0001,IRO1GESF0001,IRO1GMRO0001,IRO1GNJN0001,IRO1ABGN0001,IRO3ZF200001,IRT3SSKF0001,IRO7PKZP0001,IRO1KESF0001,IRO1BAMA0001,IRO1ITAL0001,IRO1IRGC0001,IRO1KPRS0001,IRO1CHML0001,IRO1CHIR0001,IRO1KHFZ0001,IRO1DMVN0001,IRO1TSRZ0001,IRO1ROOI0001,IRO1SINA0001,IRR1SINA0101,IRO1ARDK0001,IRO1PSER0001,IRO1KSAD0001,IRO3KSGZ0001,IRO1TBAS0001,IRO1SHQZ0001,IRO1ALVN0001,IRO1NILO0001,IRO1BHSM0001,IRO1SHMD0001,IRO1VARZ0001,IRO3KBCZ0001,IRO1ASAL0001,IRO1AZMA0001,IRO1PELC0001,IRO1PYAM0001,IRO1JJNM0001,IRO1LKPS0001,IRO1SRMA0001,IRO1KMOA0001,IRO1IAGM0001,IRO3KARZ0001,IRO1BMEL0001,IRO7PMMP0001,IRO3ZF220001,IRR3KHMZ0101,IRO3MIHZ0001,IRO1BROJ0001,IRO1PTOS0001,IRO3ZF060001,IRO1MRIN0001,IRO7BNOP0001,IRO7NIRP0001,IRO3ZF080001,IRO1HMRZ0001,IRO7VHYP0001,IRR1ALBZ0101,IRO1OIMC0001,IRO1TAZB0001,IRO7KARP0001,IRO1BIME0001,IRO1BPAR0001,IRO1DARO0001,IRO1TGOS0001,IRO1TOKA0001,IRO3BKHZ0001,IRO3BMDZ0001,IRO3ZMNZ0001,IRO1SSAP0001,IRO1SDID0001,IRO1SKBV0001,IRR1SKBV0101,IRO7SNAP0001,IRO7SHOP0001,IRO1GBEH0001,IRO7TKDP0001,IRO1KRAF0001,IRO7KOSP0001,IRO3IRNZ0001,IRO7BVMP0001,IRO1MELT0001,IRO1GMEL0001,IRO1SNRO0001,IRO1NIKI0001,IRR3ETLZ0101,IRR1BARZ0101,IRO1KVRZ0001,IRR1TRIR0101,IRR3ZNDZ0101,IRR3ZNDZ0101,IRR1KSKA0101,IRR1DALZ0101,IRO3BLKZ0001,IRR1TKIN0101,IRO1KHOC0001,IRO1CIDC0001,IRR1PNTB0101,IRR1PRDZ0101,IRR3PTRZ0101,IRO3LIAZ0001,IRR1GNJN0101,IRO3TBSZ0001,IRR1PKER0101,IRR1SGAZ0101,IRO1BVMA0001,IRO1MOBN0001,IRR1BMEL0101,IRO3FOHZ0001,IRR1BPAS0101,IRO3BHLZ0001,,%22}';
$ch=curl_init();
$timeout=15;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Get URL content
$lines_string=curl_exec($ch);
curl_close($ch);
$now = microtime(true);
echo sprintf("Elapsed: %f", $now-$then);
$lines_string;
echo "\r\n";
?>