Solved

Why is fopen so slllooowww?

Posted on 2004-09-13
8
653 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

808 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