simplexml_load_fileのエラーはtry catchで補足できない
phpのsimplexml_load_file()で存在しないxmlファイルを読もうとするとエラーになる。
このエラーはtry catchで補足できない様子。
※php version 5.4.29では補足できなかったけど、もしかしてもっと上のバージョンだとできるようになってるのかも。
例:
↓(×)これだとエラー補足できない
$url = "http://hoge/hogege.xml"; try { $xml = simplexml_load_file($url); } catch (Exception $e) { echo "エラー!".$e->getMessage(); }
↓(○)こうしてエラー判定するのが正しい
$url = "http://hoge/hogege.xml"; $xml = simplexml_load_file($url); if (!$xml){ echo "エラー!"; }
↓(○)これでもいいかも
$url = "http://hoge/hogege.xml"; try { $xml = simplexml_load_file($url); if (!$xml){ throw new Exception("xml read error!") } } catch (Exception $e) { echo "エラー!".$e->getMessage(); }