HTMLから画像が読めない場合があります。
具体的に言うとウェブサイトから画像を取得しようと思っているのですが一部の画像がうまく取得できません。
何が原因でしょうか?
例ですがhttps://www.stanford.eduのサイトの画像が全て取れません。
file_put_contentsの段階で一部の画像が読み込めていないみたいです。
<?php $URL = "https://www.stanford.edu"; image($URL); echo "\n"; function image($targetUrl ){ // get html resourse $htmlSource = @file_get_contents( $targetUrl ); if ( $htmlSource ){ // image file preg_match_all('/src="(.*?(\.jpg|\.jpeg|\.gif|\.png))"/i', $htmlSource, $res); if ( isset( $res[1] ) ){ foreach( $res[1] as $targetImgUrl ) { // file names $fileNameTmp = explode( '/', $targetImgUrl); $fileNameTmp = array_reverse( $fileNameTmp); $fileName = $fileNameTmp[0]; // save image $imgData = @file_get_contents($targetImgUrl); if ($imgData){ @file_put_contents(image.$fileName, $imgData); } } } } } ?> コード
回答2件
あなたの回答
tips
プレビュー