zhu894532094 发表于 2017-3-25 08:07:12

php中下载网站文件

  以图片为例。   
1、fsockopen方法
  <?php <br />$url = “http://www.example.net/xxx/xxx.jpg”;      
$t = parse_url($url);      
$host = $t['host'];      
$file = $t['path'];
  $fp = fsockopen($host,80, $errno, $errstr, 30);   
if($fp)      
{
  $header = “GET $file HTTP/1.1/r/n”;   
$header .= “Host: $host/r/n”;      
$header .= “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1/r/n”;      
$header .= “Referer: http://$host/r/n”;      
$header .= “Connection: Close/r/n/r/n”;
  fwrite($fp, $header);
  $jpg = fopen($path . basename($file), “wb”);   
while (!feof($fp))      
{      
$s = fgets($fp,128);      
fwrite($jpg,$s);      
}      
fclose($jpg);
  fclose($fp);   
}      
?>
  2、curl方法。需要php中启用curl。来自:http://www.askapache.com/php/curl-multi-downloads.html,我加上了几个选项:REFERER检查等。
  <?php <br />set_time_limit(0);      
ini_set(’display_errors’,true);//Just in case we get some errors, let us know….
  $host = “www.example.net”;   
$urls=array(      
“http://$host/xxx/xxx.jpg”,      
“http://$host/xxx/yyy.jpg”      
);
  $save_to=’./’;
  $mh = curl_multi_init();
  foreach ($urls as $i => $url)   
{      
$g=$save_to.basename($url);      
if(!is_file($g))      
{      
$conn[$i]=curl_init($url);
  $fp[$i]=fopen ($g, “wb”);
  curl_setopt($conn[$i], CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1″);   
curl_setopt($conn[$i], CURLOPT_REFERER, “http://$host”);      
curl_setopt($conn[$i], CURLOPT_FILE, $fp[$i]);      
curl_setopt($conn[$i], CURLOPT_HEADER ,0);      
curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,600);      
curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);      
//curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 0);      
//curl_setopt($conn[$i], CURLOPT_VERBOSE, 0);
  curl_multi_add_handle ($mh,$conn[$i]);   
}      
}
  do {   
$n=curl_multi_exec($mh,$active);      
}      
while ($active);
  foreach ($urls as $i => $url)   
{      
curl_multi_remove_handle($mh,$conn[$i]);      
curl_close($conn[$i]);      
fclose ($fp[$i]);      
}
  curl_multi_close($mh);   
?>
页: [1]
查看完整版本: php中下载网站文件