使用CURL从安全网站(https)下载文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用CURL从安全网站(https)下载文件相关的知识,希望对你有一定的参考价值。

  1. /**
  2. * Copy File from HTTPS/SSL location
  3. *
  4. * @param string $FromLocation
  5. * @param string $ToLocation
  6. * @return boolean
  7. */
  8. function copySecureFile($FromLocation,$ToLocation,$VerifyPeer=false,$VerifyHost=true)
  9. {
  10. // Initialize CURL with providing full https URL of the file location
  11. $Channel = curl_init($FromLocation);
  12.  
  13. // Open file handle at the location you want to copy the file: destination path at local drive
  14. $File = fopen ($ToLocation, "w");
  15.  
  16. // Set CURL options
  17. curl_setopt($Channel, CURLOPT_FILE, $File);
  18.  
  19. // We are not sending any headers
  20. curl_setopt($Channel, CURLOPT_HEADER, 0);
  21.  
  22. // Disable PEER SSL Verification: If you are not running with SSL or if you don't have valid SSL
  23. curl_setopt($Channel, CURLOPT_SSL_VERIFYPEER, $VerifyPeer);
  24.  
  25. // Disable HOST (the site you are sending request to) SSL Verification,
  26. // if Host can have certificate which is nvalid / expired / not signed by authorized CA.
  27. curl_setopt($Channel, CURLOPT_SSL_VERIFYHOST, $VerifyHost);
  28.  
  29. // Execute CURL command
  30. curl_exec($Channel);
  31.  
  32. // Close the CURL channel
  33. curl_close($Channel);
  34.  
  35. // Close file handle
  36. fclose($File);
  37.  
  38. // return true if file download is successfull
  39. return file_exists($ToLocation);
  40. }
  41. echo file_get_contents("https://www.verisign.com/hp07/i/vlogo.gif");exit;
  42. // Function Usage
  43. if(copySecureFile("https://www.verisign.com/hp07/i/vlogo.gif","c:/verisign_logo.gif"))
  44. {
  45. echo 'File transferred successfully.';
  46. }
  47. else
  48. {
  49. echo 'File transfer failed.';
  50. }
  51. ?>

以上是关于使用CURL从安全网站(https)下载文件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用curl 访问https类型的网站

如何修复 curl:(35)无法与对等方安全通信:没有通用加密算法

如何使用 curl 或 wget 从 GitLab 下载 maven 包?

php用curl时,HTTP链接正常,HTTPS时,获取不到数据

如何使用 cURL 从文本文件下载 URL 列表并更改文件名?

使用wget或curl下载网站进行存档