Solved

Why is fopen so slllooowww?

Posted on 2004-09-13
8
657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 count occurrences of each item in an array.

617 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