Created
March 3, 2016 14:56
-
-
Save dynamicnet/90398f3ecc45413ae232 to your computer and use it in GitHub Desktop.
PHP Zend_Http_Client, limits the size of downloaded response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// You have to turn classic PHP errors into Exception, because cURL trigger errors | |
function exceptions_error_handler($severity, $message, $filename, $lineno) { | |
throw new ErrorException($message, 0, $severity, $filename, $lineno); | |
} | |
set_error_handler('exceptions_error_handler'); | |
$httpClient = new Zend_Http_Client( $Url, array( | |
"adapter" => "Zend_Http_Client_Adapter_Curl", | |
"curloptions" => array( | |
CURLOPT_BUFFERSIZE => 128, //small buffer / better progress | |
CURLOPT_NOPROGRESS => false, | |
CURLOPT_PROGRESSFUNCTION => function( $DownloadSize, $Downloaded, $UploadSize, $Uploaded ){ | |
// If $Downloaded exceeds 50MB, returning non-0 breaks the connection! | |
return ($Downloaded > (50 * 1024 * 1024)) ? 1 : 0; | |
} | |
) | |
) | |
); | |
try { | |
$Response = $httpClient->request(Zend_Http_Client::GET); | |
} catch( Exception $e ){ | |
if( preg_match("/callback aborted/i" , $e->getMessage()) ){ | |
// Handle the case where the downloaded response exceeds the specifed size in callback CURLOPT_PROGRESSFUNCTION | |
throw new Exception("File size is over the maximum of 50MB"); | |
} | |
} | |
// here you can handle the $Response as you want |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment