<?php
$url = "http://nsv.ckcdn.com/images/logo1.png";
$refer = "http://nsv.ckcdn.com/";
$opt=array("http"=>array("header"=>"Referer: " . $refer));
$context=stream_context_create($opt);
$file_contents = file_get_contents($url,false, $context);
echo $file_contents;
?>
我想偽造Referer,然後用 file_get_contents 取得圖片顯示出來,不過顯示出來的是亂碼,該如何解決?
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
<?php
header("Content-type: image/png");
$url = 'http://misc.360buyimg.com/lib/img/e/logo-201305.png';
$file_contents = file_get_contents($url);
echo $file_contents;
?>
結果可以正常輸出 某東 的LOGO(因為題主提供的圖片鏈接訪問不到,所以找了同為PNG格式的LOGO)。
我覺得問題出在file_get_contents
上。文檔:http://cn2.php.net/file_get_contents
因為file_get_contents
的作用是整個文件讀入一個字符串中,輸出是當然是以字符串輸出,瀏覽器不知道它是圖片,所以要在加上header("Content-type: image/png")
,以告訴瀏覽器這是png格式的圖片,瀏覽器才會顯示成圖片。