Link to home
Start Free TrialLog in
Avatar of stuayre
stuayre

asked on

Why is fopen so slllooowww?

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
Avatar of mail-router
mail-router

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
Avatar of stuayre

ASKER

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 );
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
Also, the speed differences could be related to the speed/distance of the internetwork connection between the server and the requested files.
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial