Skip to content

Instantly share code, notes, and snippets.

@hamaguchi
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamaguchi/7925cdac57dcc116ae14 to your computer and use it in GitHub Desktop.
Save hamaguchi/7925cdac57dcc116ae14 to your computer and use it in GitHub Desktop.
simplexml_load_string の謎...
set_error_handler(function($errno, $errstr, $errfile, $errline) {
throw new \Exception($errstr, $errno);
});
try {
$search = array("\0", "\x01", "\x02", "\x03", "\x04", "\x05","\x06", "\x07", "\x08", "\x0b", "\x0c", "\x0e", "\x0f");
$options = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36',
),
);
$context = stream_context_create($options);
$xml_context = file_get_contents($rss_url, false, $context);
$xml_context = str_replace($search, '', $xml_context);
if (mb_detect_encoding($xml_context, 'UTF-8', true) === false) {
$xml_context = utf8_encode($xml_context);
}
if (strpos($xml_context, '<?xml', 0) === false){
echo "×:Error:". $rss_url . "\n";
continue 1;
}
$xml = simplexml_load_string($xml_context);
if ($xml === false) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
restore_error_handler(); //追記:2014.05.05
} catch (Exception $ex) {
restore_error_handler();
echo "×:Exception". $ex . "\n";
echo "×:Error:". $rss_url . "\n";
continue 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment