/**
* 下载
*/
public function download($ids = null)
{
//一些条件参数啥的
$data = [];
//获取文件
$res = curl_post(url, $data);
//如果是base64之类格式的需要转码
//写入文件(创建文件)
file_put_contents('./1.pdf', $res);
}
curl_post函数
function curl_post($url,$data = null,$json = true)
{
if(empty($url))
return false;
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
if(!empty($data)) {
if($json && is_array($data)) {
$data = json_encode($data);
}
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
if($json) { //发送JSON数据
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_HTTPHEADER,[
'Content-Type: application/json;charset=UTF-8',
'Content-Length:' . strlen($data),
]);
}
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($curl);
$errorno = curl_errno($curl);
if($errorno) {
return ['errorno' => false,'errmsg' => $errorno];
}
curl_close($curl);
return json_decode($res,true);
}