LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Get Remote File Or URL Content, Size & Load Time Using PHP?

How To Get Remote File Or URL Content, Size & Load Time Using PHP?

How-To-Get-Remote-File-Or-URL-Content,-Size-&-Load-Time-Using-PHP
Downloading content at a specific URL is a common practice on the internet, especially due to increased usage of web services and APIs offered by Amazon, Alexa, Digg, etc. PHP’s cURL library, which often comes with default shared hosting configurations, allows web developers to complete this task.

After our previous article about How To Get Remote File Size Or URL Size In B, KB, MB, GB Format Using PHP?, here we are adding some more features to our code snippet to get a remote file or URL content, size and loading time in one hit or request of cURL to save your processing power and execution time.

One thing to know that this execution time will depend on yupon your servers where you hosting this script and what you are trying to access because it will download the full page then will execute the required process to share the info with you.

Alternatively, you can use the file_get_contents() instead of cURL function remotely, but many hosts don’t allow this.

There are many code snippets available online or on many other blogs and website but everyone is not able to optimize your blog or website so you need some optimized code snippet. So now checkout out code snippet for your blog and website that will give you all features for your desired code. Now grab the ready to use code and paste it where you want.

Table of Contents

Recommended For You:
Simple And Clean FAQ Accordion JQuery Widget

How To Get Remote File Or URL Content, Size & Load Time Using PHP?

Here is the awesome code snippet that will return all things in one request and run. Move ahead to just copy-paste the function and know how to use it.

/****************************************************************************/
// Get File/URL Content, Size And Load TIme
/****************************************************************************/
function curl_get_file_size_time( $incomingURL )
{
	$ch = curl_init();
	$timeout = 5;	//5 is seconds
	$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
	curl_setopt($ch, CURLOPT_URL, $incomingURL);
	curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	$beforeTime = microtime(true);
	$beforeTime = (float) $beforeTime;
	$webPageContent = curl_exec($ch);
	$aftertime = microtime(true);
	$aftertime = (float) $aftertime;
	if($webPageContent === false) {
		curl_close($ch);
		return array($incomingURL,0,0);	// URL, Loading Time, Size In MB
	}
	else {
		$contentLoadTime = $aftertime - $beforeTime;
		$contentLoadTime = round($contentLoadTime,1);	//Make It To 1 Decimal
		$contentSize = mb_strlen($webPageContent);
		$contentSize = round(($contentSize / 1024),2);	//Convert Bytes into KB		
		curl_close($ch);
		return array($incomingURL,$webPageContent,$contentLoadTime,$contentSize);	// URL, Content, Loading Time, Size In KB
	}
}

To call or use this function, you just have to pass the URL to it and get the return data in an array and break it as per your need.

$incomingURL = "https://www.exeideas.com";
$returnTempArray = array();
$returnTempArray[] = curl_get_file_size_time( $incomingURL );
echo "URL Is: ".$returnTempArray[0][0];
echo "Content Is: ".$returnTempArray[0][1];
echo "Loading Time(s) Is: ".$returnTempArray[0][2];
echo "URL Size(kb) Is: ".$returnTempArray[0][3];

Thats it.

Final Words:

Be aware that the is placed well in your document. Rest all is in your hand if you want to customize it or play with it. That’s all we have. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allowed to us. Don’t forget to share this with your friends so they can also take benefit from it and leave.

Recommended For You:
Get Headers And Content Of A URL Using Single CURL Request In PHP

You Like It, Please Share This Recipe With Your Friends Using...

4 Responses to “How To Get Remote File Or URL Content, Size & Load Time Using PHP?”

  1. DVCOMM says:

    Hi, thanks for the article it is really nice , I am gonna bookmark this page, thanks for sharing this useful, valuable and excellent article, as share good stuff with good ideas and concepts, lots of great information and inspiration.

    • EXEIdeas says:

      Welcome here and thanks for reading our article and sharing your view. This will be very helpful to us to let us motivate to provide you more awesome and valuable content from a different mind. Thanks for reading this article.

  2. Yeah, your scripts are sweet! The way that you write PHP is very clean.

Leave a Reply

Your email address will not be published. Required fields are marked *