note 86534 added to function.curl-getinfo
| From: | raghuveer at spiderace dot com | Date: | Thu, 23 Oct 2008 12:47:11 +0000 |
| Subject: | note 86534 added to function.curl-getinfo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145637@lists.php.net to get a copy of this message | ||
A code snippet that I had attempted to enumerate the Remote File Size and to post Amount of Time
taken for that particular Curl Request.
<?php
/* borrowed the Byte Conversion code (byte_convert function) posted by "olafurw at
gmail.com" and modified by "d.abromeit" in http://in.php.net/filesize. I did make changes in the notation
of expression of multiples of bytes from KiB => KB, MiB => MB etc all symbols in this Byte
Code*/
function byte_convert($bytes)
{
$symbol = array('B', 'KB', 'MB', 'GB', 'TB',
'PB', 'EB', 'ZB', 'YB');
$exp = 0;
$converted_value = 0;
if( $bytes > 0 )
{
$exp = floor( log($bytes)/log(1024) );
$converted_value = ( $bytes/pow(1024,floor($exp)) );
}
return sprintf( '%.2f '.$symbol[$exp], $converted_value );
}
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
$url = 'http://www.spiderace.com/samplefiles/testfile.zip';
// initialize curl with given url
$ch = curl_init($url);
// make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1);
// add useragent
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
//Tell curl to write the response to a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// The maximum number of seconds to allow cURL functions to execute.
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
// Tell curl to stop when it encounters an error
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$execute = curl_exec($ch);
// Check if any error occured
if(!curl_errno($ch))
{
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
// Display the File Size
echo byte_convert($bytes);
echo "<br><br>";
$total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
echo 'Took ' . $total_time . ' seconds to send a request to ' . $url;
clearstatcache();
}
curl_close($ch);
?>
NOTE: This code worked on Both Linux (tried on CentOS) & Windows Servers (tried on XAMPP For
Windows on Windows XP PC), thank you. Hope this will be helpful to others as well.
----
Server IP: 59.162.86.33
Probable Submitter: 117.200.17.37
----
Manual Page -- http://www.php.net/manual/en/function.curl-getinfo.php
Edit -- https://master.php.net/note/edit/86534
Del: integrated -- https://master.php.net/note/delete/86534/integrated
Del: useless -- https://master.php.net/note/delete/86534/useless
Del: bad code -- https://master.php.net/note/delete/86534/bad+code
Del: spam -- https://master.php.net/note/delete/86534/spam
Del: non-english -- https://master.php.net/note/delete/86534/non-english
Del: in docs -- https://master.php.net/note/delete/86534/in+docs
Del: other reasons-- https://master.php.net/note/delete/86534
Reject -- https://master.php.net/note/reject/86534
Search -- https://master.php.net/manage/user-notes.php