Laravel 5 How to save remote pictures to local?

Source: Internet
Author: User
Tags try catch ssl certificate
Does the Laravel filesystem support this (especially for functions such as copy)?

file_get_contents If the HTTPS Web site encounters an SSL validation failure , the error cannot be ignored.

Reply content:

Does the Laravel filesystem support this (especially for functions such as copy)?
file_get_contents If the HTTPS Web site encounters an SSL validation failure , the error cannot be ignored.

You can use Guzzlehttp.
Plus:

Use Guzzlehttp\client;use guzzlehttp\exception\guzzleexception;
$client = new Client ([' verify ' = false]);  Ignore SSL Error $response = $client->get ($url, [' save_to ' = Public_path ($file)]);  Save remote URL to file

Can be written with try catch.

Guzzlehttp gets the remote resource. Storage handles file storage.

try {    $client = new \guzzlehttp\client ();    $data = $client->request (' Get ', ' http://xxxx ')->getbody ()->getcontents ();    Storage::d ISK (' local ')->put (' filename ', $data);} catch (\guzzlehttp\requestexception $e) {    echo ' fetch fail ';}
The problem with SSL certificate validation failure is that you do not have the latest list of CA certificates locally, just download one.
$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 ), the file_get_contents will certainly support HTTPS.
If you are prompted with an error:
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 as well:

wget Https://curl.haxx.se/ca/cacert.pemphp.ini:openssl.cafile=/path/to/cacert.pemvar_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 ' = ', '  
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.