使用curl在下载文件中重定向Https

时间:2014-04-11 17:50:51

标签: php redirect curl

我有卷毛的问题。我尝试从另一台服务器下载文件。脚本仅下载此内容:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Site</title>
<link rel="icon" type="image/ico" href="favicon.ico" />
<script type="text/javascript">
    if ('https:' !== location.protocol) location.replace(location.toString().replace(location.protocol, 'https:'));
</script>
<script type="text/javascript" src="src/includes.js"></script>
<link rel="search" type="application/opensearchdescription+xml" href="xml/search.xml"/>
</head>
<body>
<script type="text/javascript">
    if (!location.hash || '_=_' == location.hash) location.replace('/#' + location.pathname + location.search);
</script>
<noscript>
    <meta http-equiv="refresh" content="0;?nojs" />
    <a href="?nojs">HTML only version</a>
</noscript>
</body>
</html>

我认为,它关注https:// redirect。

我的CURL脚本:

$options = array( 
    CURLOPT_RETURNTRANSFER => true,     // return web page 
    CURLOPT_HEADER         => false,    // return headers 
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects 
    CURLOPT_ENCODING       => "",       // handle all encodings 
    CURLOPT_USERAGENT      => "spider", // who am i 
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect 
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect 
    CURLOPT_TIMEOUT        => 120,      // timeout on response 
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects 
    CURLOPT_SSL_VERIFYHOST => 1,     //put this also
    CURLOPT_SSL_VERIFYPEER => false  // this also
); 

$ch = curl_init(str_replace('https://', 'http://', $workerLocation)); 
curl_setopt_array($ch, $options);

$out = fopen($fileLocation, 'wb');
curl_setopt($ch, CURLOPT_FILE, $out);

$content    = curl_exec($ch); 
fclose($out);

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您提供的html中的JS脚本会将http协议替换为https。你的php脚本应该模仿这个操作。但事实并非如此,因为您已经混淆了str_replace电话中参数的顺序。 str_replace('https://', 'http://', $workerLocation)旨在取代&#39; https://&#39;与&#39; http://&#39;。但是你需要做相反的操作。

所以你必须修改参数的顺序:

$ch = curl_init(str_replace('http://', 'https://', $workerLocation));