投稿日: 
例えば、あのURLはきちんと301されているだろうかとか、リンク先とか画像が404になっていないだろうかとか気になって夜も眠れない日は誰にでもあるはずです。唐突。
そんなわけで簡単にではありますがURLを渡すとヘッダー情報(get_headers
)を見て、参照先URLのステータスを返す関数を書いてみました。
※ タイトルはキリッとしてますが大したものじゃありません
※ http://jp2.php.net/manual/ja/function.get-headers.php#119497 のほぼ完コピです。すいません。
2017/08/16 追記 クラスにしてみました
【PHP】URLを渡すとヘッダー情報を見てステータスを返すクラスを書いた | Shimabox Blog
ソースはこちら。
ソース
Example
YahooのURLを渡してみる
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | $ret = getUrlStatus( 'http://m.yahoo.co.jp' ); var_dump( $ret ); /* object(stdClass)#1 (10) { ["url"]=> string(20) "http://m.yahoo.co.jp" ["reachedUrl"]=> string(24) "https://www.yahoo.co.jp/" ["isValid"]=> bool(true) ["redirectedUrls"]=> array(2) { [0]=> string(26) "https://m.yahoo.co.jp:443/" [1]=> string(24) "https://www.yahoo.co.jp/" } ["redirectedCode"]=> array(2) { [0]=> int(301) [1]=> int(302) } ["code"]=> int(200) ["is200"]=> bool(true) ["is403"]=> bool(false) ["is404"]=> bool(false) ["is500"]=> bool(false) } */ |
これを見た感じ、http://m.yahoo.co.jp を渡すと、
- まず、https://m.yahoo.co.jp:443/ に 301 リダイレクト
- さらに、https://www.yahoo.co.jp/ に 302 リダイレクト
- そして、HTTPステータスコード200 を返却
されていることがわかります。
適当な画像を指定してみる
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | $ret = getUrlStatus( 'https://example.com/hoge.png' ); var_dump( $ret ); /* object(stdClass)#1 (10) { ["url"]=> string(28) "https://example.com/hoge.png" ["reachedUrl"]=> string(28) "https://example.com/hoge.png" ["isValid"]=> bool(true) ["redirectedUrls"]=> array(0) { } ["redirectedCode"]=> array(0) { } ["code"]=> int(404) ["is200"]=> bool(false) ["is403"]=> bool(false) ["is404"]=> bool(true) ["is500"]=> bool(false) } */ var_dump( $ret ->is404 === true); // => true |
is404
がtrue
となります。
おまけ (POSTの場合)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | $data = http_build_query([ 'foo' => 'bar' , 'hoge' => 'piyo' ]); $header = [ 'Content-Type: application/x-www-form-urlencoded' , 'Content-Length: ' . strlen ( $data ) ]; $options = [ 'http' => [ 'method' => 'POST' , 'header' => implode( "\r\n" , $header ), 'content' => $data ] ]; $ret = getUrlStatus( 'http://localhost/post.php' , $options ); |
第二引数にstream_context_set_default()
で使うオプションを渡してください。
PHP: stream_context_set_default – Manual
まとめ
冒頭で述べたとおり http://jp2.php.net/manual/ja/function.get-headers.php#119497 をほぼ参考にして書きましたが、うまくPHPUnit
やselenium
などと組み合わせるといいかもしれません。