gurl 要下載的圖片地址
$rfurl 來路
$filename 下載圖片保存的文件名
$gcookie 調整cookie 偽造的cookie
$JumpCount 跳轉計數
$maxtime 最大次數
調用方法
<?php
function DownImageKeep($gurl
{
$urlinfos = GetHostInfo($gurl);
$ghost = trim($urlinfos[
if($ghost==
{
return FALSE;
}
$gquery = $urlinfos[
if($gcookie=="" && !empty($rfurl))
{
$gcookie = RefurlCookie($rfurl);
}
$sessionQuery = "GET $gquery HTTP/
$sessionQuery
$sessionQuery
$sessionQuery
$sessionQuery
if($gcookie!="" && !preg_match("/[rn]/"
{
$sessionQuery
}
$sessionQuery
$errno = "";
$errstr = "";
$m_fp = fsockopen($ghost
fwrite($m_fp
$lnum =
//獲取詳細應答頭
$m_httphead = Array();
$httpstas = explode(" "
$m_httphead["http
$m_httphead["http
while(!feof($m_fp))
{
$line = trim(fgets($m_fp
if($line == "" || $lnum>
{
break;
}
$hkey = "";
$hvalue = "";
$v =
for($i=
{
if($v==
{
$hvalue
}
if($line[$i]==":")
{
$v =
}
if($v==
{
$hkey
}
}
$hkey = trim($hkey);
if($hkey!="")
{
$m_httphead[strtolower($hkey)] = trim($hvalue);
}
}
//分析返回記錄
if(preg_match("/^
{
if(isset($m_httphead["location"]) && $JumpCount<
{
$JumpCount++;
DownImageKeep($gurl
}
else
{
return FALSE;
}
}
if(!preg_match("/^
{
return FALSE;
}
if(!isset($m_httphead))
{
return FALSE;
}
$contentLength = $m_httphead[
//保存文件
$fp = fopen($filename
$i=
$okdata = "";
$starttime = time();
while(!feof($m_fp))
{
$okdata
$i++;
//超時結束
if(time()
{
break;
}
//到達指定大小結束
if($i >= $contentLength)
{
break;
}
}
if($okdata!="")
{
fwrite($fp
}
fclose($fp);
if($okdata=="")
{
@unlink($filename);
fclose($m_fp);
return FALSE;
}
fclose($m_fp);
return TRUE;
}
/**
* 獲得某頁面返回的Cookie信息
*
* @access public
* @param string $gurl 調整地址
* @return string
*/
function RefurlCookie($gurl)
{
global $gcookie
$gurl = trim($gurl);
if(!empty($gcookie) && $lastRfurl==$gurl)
{
return $gcookie;
}
else
{
$lastRfurl=$gurl;
}
if(trim($gurl)==
{
return
}
$urlinfos = GetHostInfo($gurl);
$ghost = $urlinfos[
$gquery = $urlinfos[
$sessionQuery = "GET $gquery HTTP/
$sessionQuery
$sessionQuery
$sessionQuery
$sessionQuery
$errno = "";
$errstr = "";
$m_fp = fsockopen($ghost
fwrite($m_fp
$lnum =
//獲取詳細應答頭
$gcookie = "";
while(!feof($m_fp))
{
$line = trim(fgets($m_fp
if($line == "" || $lnum>
{
break;
}
else
{
if(preg_match("/^cookie/i"
{
$gcookie = $line;
break;
}
}
}
fclose($m_fp);
return $gcookie;
}
/**
* 獲得網址的host和query部份
*
* @access public
* @param string $gurl 調整地址
* @return string
*/
function GetHostInfo($gurl)
{
$gurl = preg_replace("/^http:///i"
$garr[
$garr[
return $garr;
}
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/21209.html