public function callFunction($url, $postData, $method, header='')
{
$maxRetryTimes = 3;
$curl = curl_init();
/******初始化请求参数start******/
if(strtoupper($method) !== 'GET' && $postData){
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postData));
}elseif (strtoupper($method) === 'GET' && $postData){
$url .= '?'. http_build_query($postData);
}
/******初始化请求参数end******/
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_TIMEOUT => 10,
CURLOPT_NOBODY => 0,
CURLOPT_RETURNTRANSFER => 1
));
if(method == 'POST'){
curl_setopt($curl, CURLOPT_POST, true);
}
if(false == empty()){
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
$response = false;
while(($response === false) && (--$maxRetryTimes > 0)){
$response = trim(curl_exec($curl));
}
return $response;
}
$ch = curl_init(); $options = array(CURLOPT_URL => 'http://www.php.net/', CURLOPT_HEADER => false, CURLOPT_HEADERFUNCTION => 'on_curl_header', CURLOPT_WRITEFUNCTION => 'on_curl_write' );
function on_curl_write($ch, $data)
{
$pid = getmypid();
$downloadSizeRecorder = DownloadSizeRecorder::getInstance($pid);
$bytes = strlen($data);
$downloadSizeRecorder->downloadData .= $data;
$downloadSizeRecorder->downloadedFileSize += $bytes;
// error_log(' on_curl_write '.$downloadSizeRecorder->downloadedFileSize." > {$downloadSizeRecorder->maxSize} \n", 3, '/tmp/hyb.log');
//确保已经下载的内容略大于最大限制
if (($downloadSizeRecorder->downloadedFileSize - $bytes) > $downloadSizeRecorder->maxSize) {
return false;
}
return $bytes; //这个不正确的返回,将会报错,中断下载 "errno":23,"errmsg":"Failed writing body (0 != 16384)"
}
class DownloadSizeRecorder
{
const ERROR_FAILED_WRITING = 23; //Failed writing body
public $downloadedFileSize;
public $maxSize;
public $pid;
public $hasOverMaxSize;
public $fileFullName;
public $downloadData;
private static $selfInstanceList = array();
public static function getInstance($pid)
{
if(!isset(self::$selfInstanceList[$pid])){
self::$selfInstanceList[$pid] = new self($pid);
}
return self::$selfInstanceList[$pid];
}
private function __construct($pid)
{
$this->pid = $pid;
$this->downloadedFileSize = 0;
$this->fileFullName = '';
$this->hasOverMaxSize = false;
$this->downloadData = '';
}
/**
* 保存文件
*/
public function saveMaxSizeData2File(){
if(empty($resp_data)){
$resp_data = $this->downloadData;
}
$fileFullName = '/tmp/http_'.$this->pid.'_'.time()."_{$this->maxSize}.download";
if($resp_data && strlen($resp_data)>0)
{
list($headerOnly, $bodyOnly) = explode("\r\n\r\n", $resp_data, 2);
$saveDataLenth = ($this->downloadedFileSize < $this->maxSize) ? $this->downloadedFileSize : $this->maxSize;
$needSaveData = substr($bodyOnly, 0, $saveDataLenth);
if(empty($needSaveData)){
return;
}
file_put_contents($fileFullName, $needSaveData);
if(file_exists($fileFullName)){
$this->fileFullName = $fileFullName;
}
}
}
/**
* 返回文件的md5
* @return string
*/
public function returnFileMd5(){
$md5 = '';
if(file_exists($this->fileFullName)){
$md5 = md5_file($this->fileFullName);
}
return $md5;
}
/**
* 返回已下载的size
* @return int
*/
public function returnSize(){
return ($this->downloadedFileSize < $this->maxSize) ? $this->downloadedFileSize : $this->maxSize;
}
/**
* 删除下载的文件
*/
public function deleteFile(){
if(file_exists($this->fileFullName)){
unlink($this->fileFullName);
}
}
}
…… curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'on_curl_write');//设置回调函数 …… $pid = getmypid(); $downloadSizeRecorder = DownloadSizeRecorder::getInstance($pid); $downloadSizeRecorder->maxSize = $size_limit; …… //发起curl请求 $response = curl_exec($ch); …… //保存文件,返回md5 $downloadSizeRecorder->saveMaxSizeData2File(); //保存 $downloadFileMd5 = $downloadSizeRecorder->returnFileMd5(); $downloadedfile_size = $downloadSizeRecorder->returnSize(); $downloadSizeRecorder->deleteFile();
if($response === true){
$response = $downloadSizeRecorder->downloadData;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有