Home >Backend Development >PHP Tutorial >How to save remote images to local in Laravel 5?
Does Laravel's FileSystem support this (especially functions such as copy)?
If you use file_get_contents
, if it is an HTTPS website, you will encounter an SSL verification failure error, but you cannot ignore this error.
Does Laravel's FileSystem support this (especially functions such as copy)?
If you use file_get_contents
, if it is an HTTPS website, you will encounter an SSL verification failure error, but you cannot ignore this error.
Just use GuzzleHttp.
Added:
use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException;
$client = new Client(['verify' => false]); //忽略SSL错误 $response = $client->get($url, ['save_to' => public_path($file)]); //保存远程url到文件
can be written using try catch.
GuzzleHttp gets remote resources. Storage deals with file storage.
try { $client = new \GuzzleHttp\Client(); $data = $client->request('get','http://xxxx')->getBody()->getContents(); Storage::disk('local')->put('filename', $data); } catch (\GuzzleHttp\RequestException $e) { echo 'fetch fail'; }
SSL证书验证失败的问题是以为你本地没有最新的ca证书列表,下载一个就行了
$client = new \GuzzleHttp\Client($url,[ 'curl.options' => [ CURLOPT_SSL_VERIFYPEER=>2, CURLOPT_SSL_VERIFYHOST=true, ] ]);
You can do it with curl
file_put_contents('/tmp/logo.gif',file_get_contents('https://www.baidu.com/img/bdlogo.gif'));
As long as your PHP adds OpenSSL support (--with-openssl
), then file_get_contents will definitely support HTTPS.
If an error occurs:
SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
Then download the cacert.pem file, then specify it in php.ini, and then restart the PHP service:
wget https://curl.haxx.se/ca/cacert.pem php.ini: openssl.cafile=/path/to/cacert.pem var_export(openssl_get_cert_locations()); array ( 'default_cert_file' => '/opt/phpdroid/deps/ssl/cert.pem', 'default_cert_file_env' => 'SSL_CERT_FILE', 'default_cert_dir' => '/opt/phpdroid/deps/ssl/certs', 'default_cert_dir_env' => 'SSL_CERT_DIR', 'default_private_dir' => '/opt/phpdroid/deps/ssl/private', 'default_default_cert_area' => '/opt/phpdroid/deps/ssl', 'ini_cafile' => '', 'ini_capath' => '', )