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

Posted

tags:

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

<php
/**
* Copy File from HTTPS/SSL location
*
* @param string $FromLocation
* @param string $ToLocation
* @return boolean
*/
function copySecureFile($FromLocation,$ToLocation,$VerifyPeer=false,$VerifyHost=true)
{
// Initialize CURL with providing full https URL of the file location
$Channel = curl_init($FromLocation);
 
// Open file handle at the location you want to copy the file: destination path at local drive
$File = fopen ($ToLocation, "w");
 
// Set CURL options
curl_setopt($Channel, CURLOPT_FILE, $File);
 
// We are not sending any headers
curl_setopt($Channel, CURLOPT_HEADER, 0);
 
// Disable PEER SSL Verification: If you are not running with SSL or if you don't have valid SSL
curl_setopt($Channel, CURLOPT_SSL_VERIFYPEER, $VerifyPeer);
 
// Disable HOST (the site you are sending request to) SSL Verification,
// if Host can have certificate which is nvalid / expired / not signed by authorized CA.
curl_setopt($Channel, CURLOPT_SSL_VERIFYHOST, $VerifyHost);
 
// Execute CURL command
curl_exec($Channel);
 
// Close the CURL channel
curl_close($Channel);
 
// Close file handle
fclose($File);
 
// return true if file download is successfull
return file_exists($ToLocation);
}
echo file_get_contents("https://www.verisign.com/hp07/i/vlogo.gif");exit;
// Function Usage
if(copySecureFile("https://www.verisign.com/hp07/i/vlogo.gif","c:/verisign_logo.gif"))
{
echo 'File transferred successfully.';
}
else
{
echo 'File transfer failed.';
}
?>

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

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

如何使用 curl 和 php 连接到安全 (https) 代理?

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

使用安全登录抓取网站内容

使用PHP curl模拟浏览器抓取网站信息

PHP 使用cURL从页面下载并保存图像