如果你在浏览器中输入url你可以看到两者都可以工作,cdon即使没有javascript也可以工作,他们是否阻止了cURL ?
我试图建立一个刮板,使合法电影在线受益,这将使他们受益很多,似乎愚蠢的阻止刮板一般。虽然我不确定这就是这里发生的事情!可能是某个地方出错了…
// Works
get_file1('http://sfanytime.com/sv-SE/Sokresultat/?field=all&q=The+Matrix', '/', 'sfanytime.html');
// Saves a blank 0 KB file
get_file1('http://downloads.cdon.com/index.phtml?action=search&search_terms=The+Matrix', '/', 'cdon.html');
function get_file1($file, $local_path, $newfilename) {
$out = fopen($newfilename, 'wb');
if ($out === FALSE) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$error = curl_error($ch);
if (strlen($error) > 0) {
echo "<br>Error is : ". $error;
return false;
}
curl_close($ch);
return true;
}
你应该改变行
curl_setopt($ch, CURLOPT_FAILONERROR, true);
……
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
CURLOPT_FAILONERROR
将导致"无声失败"——从你所说的,这不是你想要的。我已经用CURLOPT_FOLLOWLOCATION
代替了这个,因为当我访问第二个URL时,我被重定向到一个"选择你的国家"类型的页面,这将是一个空体的响应-这就是为什么你得到一个空文件。
代码本身没有问题,只是处理第二个URL响应的方式有问题。您不会看到错误,因为从技术上讲,没有错误。