我想从网上下载一个页面,当您使用像 Firefox 这样的简单浏览器时,这是允许的,但是当我使用“file_get_contents”时,服务器拒绝并回复说它理解该命令,但不允许此类下载。
那么该怎么办?我想我在一些脚本(在 Perl 上)中看到了一种通过创建用户代理和 cookie 使您的脚本像真正的浏览器一样的方法,这使服务器认为您的脚本是真正的 Web 浏览器。
有谁对此有想法,如何做到?
使用卷曲。
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set the UA
curl_setopt($ch, CURLOPT_USERAGENT, 'My App (http://www.example.com/)');
// Alternatively, lie, and pretend to be a browser
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
(From http://uk.php.net/manual/en/curl.examples-basic.php)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)