How to save remote images to local in Laravel 5?

不言
Release: 2023-03-01 06:36:02
Original
6367 people have browsed it

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.

Reply content:

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;
Copy after login
$client = new Client(['verify' => false]);  //忽略SSL错误
$response = $client->get($url, ['save_to' => public_path($file)]);  //保存远程url到文件
Copy after login

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';
}
Copy after login
SSL证书验证失败的问题是以为你本地没有最新的ca证书列表,下载一个就行了
Copy after login
$client = new \GuzzleHttp\Client($url,[
    'curl.options' => [
        CURLOPT_SSL_VERIFYPEER=>2,
        CURLOPT_SSL_VERIFYHOST=true,
    ]
]);
Copy after login

You can do it with curl

file_put_contents('/tmp/logo.gif',file_get_contents('https://www.baidu.com/img/bdlogo.gif'));
Copy after login

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' => '',
)
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!