Solved

Why is fopen so slllooowww?

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now