直接读取即可
1.file_get_contents
header("Content-type: image/png"); $url = $_GET["url"]; $filename = $_GET['url']; echo file_get_contents($url); exit;
http://localhost/qq/showimg.php?url=http://app.qlogo.cn/mbloghead/41c62e6cdde14055f484/50
速度很慢
2.fopen fpassthru
header("Content-type: image/png"); $url = $_GET["url"]; $filename = $_GET['url']; $fp = fopen($filename, 'r'); fpassthru($fp); fclose($fp); exit;
采用fpassthru 还是很慢啊
http://localhost/qq/showimg.php?url=http://www.baidu.com/img/baidu_jgylogo3.gif
速度很快。说明如果下载的时候不知道文件结尾,php就下载的很慢。估计有一大块时间在计算文件到底是说明结尾的。
3.使用socket的fsockopen
$host = "app.qlogo.cn"; $f = "/mbloghead/41c62e6cdde14055f484/50"; $fp = fsockopen($host, 80, $errno, $errstr, 30); if (!$fp) { echo "ERR:$errstr ($errno)<br />\n"; } else { $out = "GET $f HTTP/1.1\r\n"; //$out = "GET / HTTP/1.1\r\n"; $out .= "Accept:*/*\r\n"; $out .= "Host: $host\r\n"; //$out .= "Referer: $url\r\n"; $out .= "Connection: Close\r\n\r\n"; $str = ""; fwrite($fp, $out); while (!feof($fp)) { $str.=fgets($fp, 128); } //echo $str; $str = trimHeader($str); echo $str; fclose($fp); } function getHeader($content){ $array=explode("\r\n\r\n",$content); return $array[0]."\r\n\r\n"; } function trimHeader($content){ $content = str_replace(getHeader($content),"",$content); return $content; }
读取腾讯微博的没有文件结尾的图片速度还是很慢,而读取百度的gif 速度很快。
socket的最大好处当然是伪造cookie,reffer等信息。