Solved

Why is fopen so slllooowww?

Posted on 2004-09-13
8
655 Views
Last Modified: 2011-09-20
Why does this script take 5 seconds to complete on one server and only 1.2 seconds on another??

is there any alternative to fopen?

what should I ask my host to do to speed things up?


<?php
// Get Current Time
$mtime = microtime();
// Split Seconds and Microseconds
$mtime = explode (" ", $mtime);
// Create a single value for start time
$mtime = $mtime[1] + $mtime[0];
// Write Start Time Into A Variable
$tstart = $mtime;

$file="http://news.bbc.co.uk";

list($protocol, $uri) = split('//', $file);
$data = file_get_contents($protocol . '//' . urlencode($uri));

//echo $data;

// Get current time (Like above) to get end time
$mtime = microtime();
$mtime = explode (" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
// Store end time in a variable
$tend = $mtime;
// Calculate Difference
$totaltime = ($tend - $tstart);
// Output the result
printf ("Page loaded in %f seconds!", $totaltime);
?>

thanks

Stu
0
Comment
Question by:stuayre
8 Comments
 

Expert Comment

by:mail-router
ID: 12050315
Hi

This could be caused by many different things! (1) the DNS server that holds the true IP address for the requested domain could be on different network or country than the server you are requesting! Bottle necks mostly will always be caused by some type of networking trouble! Also just so you know 'file_get_contents()' is not the same thing as 'fopen()' or 'fsockopen()'!



MR
0
 
LVL 1

Author Comment

by:stuayre
ID: 12050530
ahh I forgot I changed that line it should be:

$fp = fopen( $file, "r" ) or   die("Couldn't open    $file");
while( ! feof($fp ))
$data.= fgets( $fp, 1024 );
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 12051691
You might take a look at the curl library.  However, I don't know if it would actually be faster.

http://www.php.net/manual/en/ref.curl.php
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 12051695
Also, the speed differences could be related to the speed/distance of the internetwork connection between the server and the requested files.
0
 
LVL 11

Accepted Solution

by:
shmert earned 85 total points
ID: 12055010
If what you need is the contents of the remote site as a string, I think file_get_contents will actually be slightly faster than appending chunks to the data string using fopen() and fgets().  This is because the file_get_contents is a native function.

That said, the slowness is not because of the PHP code, it's from getting the data from the remote URL.  Maybe you can periodically run a script that caches the info on the local machine, instead of fetching it with the PHP request.  Then it would be nice and quick.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question