LoginSignup
22
19

More than 5 years have passed since last update.

PHP の file_get_contents でステータスコードを取得する

Last updated at Posted at 2014-05-31

上記記事より転載

$context = stream_context_create(array(
    'http' => array('ignore_errors' => true)
));
$response = file_get_contents('http://example.com/', false, $context);

preg_match('/HTTP\/1\.[0|1|x] ([0-9]{3})/', $http_response_header[0], $matches);
$status_code = $matches[1];

switch ($status_code) {
    case '200':
        // 200の場合
        break;
    case '404':
        // 404の場合
        break;
    default:
        break;
}

コメントでいただいた助言

・この関数はデフォルトで 200 以外は FALSE を返します
・ignore_errors と @ を両方付ける意味はありません

22
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
19