前提・実現したいこと
前回、phpでのスクレイピング時に相対パスを絶対パスに置き換える方法について質問させていただきました。
その後、自分でgoogleで検索してみたところ
https://qiita.com/kd9951/items/787f7a6de85e5917dbc3
以上のページを見つけました。
そのページに記載されているコードを少し手を加え、実行してみたのですが、https://paiza.io/ja/projects/new で実行した時は正しく実行できたものの、自分の契約しているレンタルサーバー(lolipopさn)では画面が真っ白のまま動作しませんでした。
どこが問題なのでしょうか。
このことによって1時間近く行き詰まっています。
どうか御指南ください。
発生している問題・エラーメッセージ
画面が真っ白
該当のソースコード
PHP
1<?php 2class Rel2Abs { 3 4 /** 5 * HTML に含まれるパスを、相対パスから絶対パスに置き換え 6 */ 7 8 public static function rel2abs($url,$html) { 9 $replacer = []; 10 // <BASE>タグがあればそれを採用。なければ引数指定のURL(HTMLの元パス) 11 if( preg_match( '/<base [^>]*href="(.*?)"/', $html, $match ) != false ){ $base = $match[1]; } 12 else $base = $url; 13 14 preg_match_all( '/(href|src)="(.+?)"/', $html, $urls, PREG_SET_ORDER); 15 foreach( $urls as $url ) { 16 $replacer[$url[2]] = static::convert_to_uri($url[2],$base); 17 } 18 foreach( $replacer as $key => $val ) $html = str_replace( '"'.$key.'"', '"'.$val.'"', $html ); 19 20 $replacer = []; 21 preg_match_all( '/(href|src)=\'(.+?)\'/', $html, $urls, PREG_SET_ORDER); 22 foreach( $urls as $url ) $replacer[$url[2]] = static::convert_to_uri($url[2],$base); 23 foreach( $replacer as $key => $val ) $html = str_replace( "'$key'", "'$val'", $html ); 24 25 $replacer = []; 26 preg_match_all( '/(href|src)=([^\'\"].+?)[>\s]/', $html, $urls, PREG_SET_ORDER); 27 foreach( $urls as $url ) $replacer[$url[2]] = static::convert_to_uri($url[2],$base); 28 foreach( $replacer as $key => $val ) $html = str_replace( "=$key", "=$val", $html ); 29 30 return $html; 31 } 32 33 /** 34 * http://web-tsukuru.com/187 35 * スクレイピングなどで画像URLを取得する時に使うために 36 * ベースURLを元に相対パスから絶対パスに変換する関数 37 * 38 * @param string $target_path 変換する相対パス 39 * @param string $base ベースとなるパス 40 * @return $uri string 絶対パスに変換済みのパス 41 */ 42 public static function convert_to_uri($target_path, $base) { 43 $component = parse_url($base); 44 45 $directory = @preg_replace('!/[^/]*$!', '/', $component["path"]); 46 47 switch (true) { 48 49 // [0] 絶対パスのケース(簡易版) 50 case preg_match("/^http/", $target_path): 51 $uri = $target_path; 52 break; 53 54 // [1]「//exmaple.jp/aa.jpg」のようなケース 55 case preg_match("/^\/\/.+/", $target_path): 56 $uri = $component["scheme"].":".$target_path; 57 break; 58 59 // [2]「/aaa/aa.jpg」のようなケース 60 case preg_match("/^\/[^\/].+/", $target_path): 61 $uri = $component["scheme"]."://".$component["host"].$target_path; 62 break; 63 64 // [2']「/」のケース 65 case preg_match("/^\/$/", $target_path): 66 $uri = $component["scheme"]."://".$component["host"].$target_path; 67 break; 68 69 // [3]「./aa.jpg」のようなケース 70 case preg_match("/^\.\/(.+)/", $target_path,$maches): 71 $uri = $component["scheme"]."://".$component["host"].$directory.$maches[1]; 72 break; 73 74 // [4]「aa.jpg」のようなケース([3]と同じ) 75 case preg_match("/^([^\.\/]+)(.*)/", $target_path,$maches): 76 $uri = $component["scheme"]."://".$component["host"].$directory.$maches[1].$maches[2]; 77 break; 78 79 // [5]「../aa.jpg」のようなケース 80 case preg_match("/^\.\.\/.+/", $target_path): 81 //「../」をカウント 82 preg_match_all("!\.\./!", $target_path, $matches); 83 $nest = count($matches[0]); 84 85 //ベースURLのディレクトリを分解してカウント 86 $dir = preg_replace('!/[^/]*$!', '/', $component["path"])."\n"; 87 $dir_array = explode("/",$dir); 88 array_shift($dir_array); 89 array_pop($dir_array); 90 $dir_count = count($dir_array); 91 92 $count = $dir_count - $nest; 93 94 $pathto=""; 95 $i = 0; 96 while ( $i < $count) { 97 $pathto .= "/".$dir_array[$i]; 98 $i++; 99 } 100 $file = str_replace("../","",$target_path); 101 $uri = $component["scheme"]."://".$component["host"].$pathto."/".$file; 102 103 break; 104 105 default: 106 $uri = $target_path; 107 } 108 return $uri; 109 } 110 111} 112 113$url = "https://tetsudo.com/"; 114 115$html = file_get_contents("https://tetsudo.com"); 116 117$result = Rel2Abs::rel2abs( $url, $html ); 118 119var_dump($result); 120?>
https://paiza.io/ja/projects/newの時
自分のサーバーの時
試したこと
エラー処理(途中の$directory = @preg_replace('!/[^/]*$!', '/', $component["path"]);
のところ)
詳細
もし質問に不備がある場合はコメントから教えてください。
訂正させていただきます。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。