guzzlehttp是php里一个网络请求客户端扩展,非常好用,特别推荐
composer安装
composer require guzzlehttp/guzzle:~6.0
针对远程文件下载:
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
$remoteFileUrl = 'http://xxx.pdf';
$saveFilePath = './xxx.pdf';
try{
$client = new Client(['verify'=>false]);
$response = $client->get($remoteFileUrl,['save_to'=>$saveFilePath]);
if(file_exits($saveFilePath)){
echo "下载成功";
}
return $response;
}catch(\Exception $e){
var_dump($e->getMessage());
}