Pawn Posted October 19, 2009 Share Posted October 19, 2009 Hiya. I'd like to grab the load time of an external PHP document. That is, to have a script load the page contents as if it were just another user behind a browser and return in seconds how long that took. Right now I can determine that the server is up and get a ping-like response time, but that isn't much use to me. I don't know where to start, or the difficulty of what I'm asking. I'd very much appreciate any pointers! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/ Share on other sites More sharing options...
MadTechie Posted October 19, 2009 Share Posted October 19, 2009 When you say "page contents" do you mean just the page or including all of its elements (JS, CSS, images) the reason I ask is if you just want the html then you can do something like <?php $start = microtime(true); $homepage = file_get_contents('http://www.apple.com/'); echo microtime(true)-$start; ?> which for me is 0.67515182495117 seconds BUT if i load http://www.apple.com/ from my browser it needs to download all the images as well, this takes about 5-7 seconds. of course the speed also depends on the clients PC and your host's PC Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-940116 Share on other sites More sharing options...
zmoerf Posted October 20, 2009 Share Posted October 20, 2009 you can using <div> element to load external page like iframe Load external page into <DIV> Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-940146 Share on other sites More sharing options...
dreamwest Posted October 20, 2009 Share Posted October 20, 2009 A external page time on ROIDS! $url = "http://youtube.com"; $ch = curl_init($url); $info = curl_getinfo($ch); curl_close($ch); echo 'Took ' . $info['total_time'] . ' seconds to send a request"; //Refrences/// //* "filetime" //* "total_time" //* "namelookup_time" //* "connect_time" //* "pretransfer_time" //* "speed_download" //* "speed_upload" //* "starttransfer_time" //* "redirect_time" Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-940156 Share on other sites More sharing options...
Pawn Posted October 21, 2009 Author Share Posted October 21, 2009 When you say "page contents" do you mean just the page or including all of its elements (JS, CSS, images) Ideally I'd like to get as close as possible to the actual load time, including media the page references. Perhaps I could parse the source for image tags and load them with some function...? Your snippet is certainly a much better indicator than ping though - thanks. Works great. Of course I don't want to run that every time my page loads, as whatever laggy site I'm checking will slow down my own page load. Is there a better way to avoid this than running it periodically and storing the output? you can using <div> element to load external page like iframe Load external page into <DIV> You lost me! A external page time on ROIDS! <?php $url = $_GET['url']; $ch = curl_init($url); $info = curl_getinfo($ch); curl_close($ch); echo $url.' took '.$info['total_time'].' seconds to send a request'; ?> Seems to return 0 seconds for any URL. E.g. Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-941149 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 I guess you could do something like this (replace sessions with a database) <?php session_start(); $URL = 'http://www.apple.com/'; if(!isset($_SESSION[$URL])) { $total = 0; $start = microtime(true); $homepage = file_get_contents($URL); $total += microtime(true)-$start; preg_match_all('/src=(["\']?)([^\1]+?)\1/m', $homepage, $result, PREG_PATTERN_ORDER); $result = $result[2]; foreach($result as $src) { $start = microtime(true); @file_get_contents($src); $total += microtime(true)-$start; } $_SESSION[$URL] = $total; } echo $_SESSION[$URL]; ?> You could even store the scr's with times in a database, add a timestamp and update when the timestamp is older than X Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-941191 Share on other sites More sharing options...
Pawn Posted October 21, 2009 Author Share Posted October 21, 2009 Exactly the kind of thing I had in mind - I'll give it a shot now. Thanks for your time! Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-941198 Share on other sites More sharing options...
Pawn Posted October 21, 2009 Author Share Posted October 21, 2009 For future Googlers: -- -- Table structure for table `sources` -- CREATE TABLE `sources` ( `source_id` int(11) NOT NULL auto_increment, `source_url` varchar(64) NOT NULL, `source_loadtime` mediumint(9) NOT NULL, `source_last_updated` datetime NOT NULL, PRIMARY KEY (`source_id`) ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; <?php include 'db.php'; function getLoad($source_url,$interval=NULL) { #time between updates in hours if(!$interval) { $interval=1; } #check if an update is due $sql = "SELECT DATE_ADD(source_last_updated, INTERVAL $interval HOUR) as update_due FROM sources WHERE source_url = '$source_url' LIMIT 1"; $query = mysql_query($sql); if(!$query) { return "Oops! (1) ".mysql_error(); } $row = mysql_fetch_row($query); $now = time(); $update_due = strtotime($row['0']); if($now>$update_due) { #check loadtime $total = 0; $start = microtime(true); $page = file_get_contents($source_url); $total += microtime(true)-$start; preg_match_all('/src=(["\']?)([^\1]+?)\1/m', $page, $result, PREG_PATTERN_ORDER); $result = $result[2]; foreach($result as $src) { $start = microtime(true); @file_get_contents($src); $total += microtime(true)-$start; } #store it $loadtime = $total*10000; $updated = date("Y-m-d H:i:s"); $sql = "UPDATE sources SET source_loadtime = $loadtime, source_last_updated = '$updated' WHERE source_url = '$source_url'"; $query = mysql_query($sql); if(!$query) { return "Oops! (2) ".mysql_error(); } if(mysql_affected_rows()==0) { #we need a new record $sql = "INSERT INTO sources (source_url, source_loadtime, source_last_updated) VALUES ('$source_url', $loadtime, '$updated')"; $query = mysql_query($sql); if(!$query) { return "Oops! (3) ".mysql_error(); } } $out = substr($total, 0, 6); } else { #get stored loadtime $sql = "SELECT source_loadtime FROM sources WHERE source_url = '$source_url'"; $query = mysql_query($sql); if(!$query) { return "Oops! (4) ".mysql_error(); } $row = mysql_fetch_row($query); $out = $row['0']/10000; } return $out; } $url = $_GET['url']; echo $url." loaded in <strong>".getLoad($url)."</strong> seconds"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-941286 Share on other sites More sharing options...
MadTechie Posted October 21, 2009 Share Posted October 21, 2009 Pawn, I would like to say thank you, for posting back your work, its nice to see people share their work instead of just taking (I have only skimmed it but it looks fine) quick notes: 1. you may want to update if to protected against SQL injection 2. In the foreach($result as $src) loop you could also add the same sort of SQL routine, (but really isn't required) Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-941302 Share on other sites More sharing options...
Pawn Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks, MT. In real-world usage I'm setting the sources server-side, so input validation isn't a problem. Quote Link to comment https://forums.phpfreaks.com/topic/178292-solved-external-page-load-time/#findComment-941451 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.