当前位置:Gxlcms > PHP教程 > php下载远程图片函数示例(伪造来路)

php下载远程图片函数示例(伪造来路)

时间:2021-07-01 10:21:17 帮助过:35人阅读

  1. /**
  2. * @功能:下载远程图片
  3. * @ bbs.it-home.org
  4. */
  5. function DownImageKeep($gurl, $rfurl, $filename, $gcookie="", $JumpCount=0, $maxtime=30)
  6. {
  7. $urlinfos = GetHostInfo($gurl);
  8. $ghost = trim($urlinfos['host']);
  9. if($ghost=='')
  10. {
  11. return FALSE;
  12. }
  13. $gquery = $urlinfos['query'];
  14. if($gcookie=="" && !empty($rfurl))
  15. {
  16. $gcookie = RefurlCookie($rfurl);
  17. }
  18. $sessionQuery = "GET $gquery HTTP/1.1\r\n";
  19. $sessionQuery .= "Host: $ghost\r\n";
  20. $sessionQuery .= "Referer: $rfurl\r\n";
  21. $sessionQuery .= "Accept: */*\r\n";
  22. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
  23. if($gcookie!="" && !preg_match("/[\r\n]/", $gcookie))
  24. {
  25. $sessionQuery .= $gcookie."\r\n";
  26. }
  27. $sessionQuery .= "Connection: Keep-Alive\r\n\r\n";
  28. $errno = "";
  29. $errstr = "";
  30. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10);
  31. fwrite($m_fp,$sessionQuery);
  32. $lnum = 0;
  33. //获取详细应答头
  34. $m_httphead = Array();
  35. $httpstas = explode(" ",fgets($m_fp,256));
  36. $m_httphead["http-edition"] = trim($httpstas[0]);
  37. $m_httphead["http-state"] = trim($httpstas[1]);
  38. while(!feof($m_fp))
  39. {
  40. $line = trim(fgets($m_fp,256));
  41. if($line == "" || $lnum>100)
  42. {
  43. break;
  44. }
  45. $hkey = "";
  46. $hvalue = "";
  47. $v = 0;
  48. for($i=0; $i {
  49. if($v==1)
  50. {
  51. $hvalue .= $line[$i];
  52. }
  53. if($line[$i]==":")
  54. {
  55. $v = 1;
  56. }
  57. if($v==0)
  58. {
  59. $hkey .= $line[$i];
  60. }
  61. }
  62. $hkey = trim($hkey);
  63. if($hkey!="")
  64. {
  65. $m_httphead[strtolower($hkey)] = trim($hvalue);
  66. }
  67. }
  68. //分析返回记录
  69. if(preg_match("/^3/", $m_httphead["http-state"]))
  70. {
  71. if(isset($m_httphead["location"]) && $JumpCount<3)
  72. {
  73. $JumpCount++;
  74. DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
  75. }
  76. else
  77. {
  78. return FALSE;
  79. }
  80. }
  81. if(!preg_match("/^2/", $m_httphead["http-state"]))
  82. {
  83. return FALSE;
  84. }
  85. if(!isset($m_httphead))
  86. {
  87. return FALSE;
  88. }
  89. $contentLength = $m_httphead['content-length'];
  90. //保存文件
  91. $fp = fopen($filename,"w") or die("写入文件:{$filename} 失败!");
  92. $i=0;
  93. $okdata = "";
  94. $starttime = time();
  95. while(!feof($m_fp))
  96. {
  97. $okdata .= fgetc($m_fp);
  98. $i++;
  99. //超时结束
  100. if(time()-$starttime>$maxtime)
  101. {
  102. break;
  103. }
  104. //到达指定大小结束
  105. if($i >= $contentLength)
  106. {
  107. break;
  108. }
  109. }
  110. if($okdata!="")
  111. {
  112. fwrite($fp,$okdata);
  113. }
  114. fclose($fp);
  115. if($okdata=="")
  116. {
  117. @unlink($filename);
  118. fclose($m_fp);
  119. return FALSE;
  120. }
  121. fclose($m_fp);
  122. return TRUE;
  123. }
  124. /**
  125. * 获得某页面返回的Cookie信息
  126. *
  127. * @access public
  128. * @param string $gurl 调整地址
  129. * @return string
  130. */
  131. function RefurlCookie($gurl)
  132. {
  133. global $gcookie,$lastRfurl;
  134. $gurl = trim($gurl);
  135. if(!empty($gcookie) && $lastRfurl==$gurl)
  136. {
  137. return $gcookie;
  138. }
  139. else
  140. {
  141. $lastRfurl=$gurl;
  142. }
  143. if(trim($gurl)=='')
  144. {
  145. return '';
  146. }
  147. $urlinfos = GetHostInfo($gurl);
  148. $ghost = $urlinfos['host'];
  149. $gquery = $urlinfos['query'];
  150. $sessionQuery = "GET $gquery HTTP/1.1\r\n";
  151. $sessionQuery .= "Host: $ghost\r\n";
  152. $sessionQuery .= "Accept: */*\r\n";
  153. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
  154. $sessionQuery .= "Connection: Close\r\n\r\n";
  155. $errno = "";
  156. $errstr = "";
  157. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.'
    ');
  158. fwrite($m_fp,$sessionQuery);
  159. $lnum = 0;
  160. //获取详细应答头
  161. $gcookie = "";
  162. while(!feof($m_fp))
  163. {
  164. $line = trim(fgets($m_fp,256));
  165. if($line == "" || $lnum>100)
  166. {
  167. break;
  168. }
  169. else
  170. {
  171. if(preg_match("/^cookie/i", $line))
  172. {
  173. $gcookie = $line;
  174. break;
  175. }
  176. }
  177. }
  178. fclose($m_fp);
  179. return $gcookie;
  180. }
  181. /**
  182. * 获得网址的host和query部份
  183. *
  184. * @access public
  185. * @param string $gurl 调整地址
  186. * @return string
  187. */
  188. function GetHostInfo($gurl)
  189. {
  190. $gurl = preg_replace("/^http:\/\//i", "", trim($gurl));
  191. $garr['host'] = preg_replace("/\/(.*)$/i", "", $gurl);
  192. $garr['query'] = "/".preg_replace("/^([^\/]*)\//i", "", $gurl);
  193. return $garr;
  194. }
  195. ?>

人气教程排行