<?phpheader (' Content-type:text/html;charset=utf-8 '); $url = ' http://www.xxx.com/text.xml '; $content = Str_replace (' gb2312 ', ' UTF-8 ', file_get_contents ($url)); $content = Iconv (' GBK ', ' UTF-8 ', $content);//$content = Utf8_encode ($ content);//Print_r ($content);//echo "<br/>************<br/>"; try{$data = simplexml_load_string ($ Content) or Die (' Unable to load XML ');} catch (Excetion $e) {print_r ($e->getmessage ());} if (Isset ($data->item)) {echo "ok<br/>";} Else{echo "Error<br/>";} echo "<br/>************<br/>";p rint_r ($data);//$content = Sxe ($url);//Var_dump ($content); function Sxe ( $url) {$xml = file_get_contents ($url); foreach ($http _response_header as $header) {if (Preg_match (' #^content-type:text/xml; charset= (. *) #i ', $head Er, $m)) {switch (Strtolower ($m [1])) {case ' utf-8 ': Do nothing break; Case ' iso-8859-1 ': $xml = Utf8_encode ($xml); Break Default: $xml = Iconv ($m [1], ' utf-8 ', $xml); } break; }} return Simplexml_load_string ($xml);}
Reference:
Http://stackoverflow.com/questions/2899274/php-simplexml-why-does-simplexml-load-string-fail-to-parse-google-weather
http://bbs.csdn.net/topics/390070967#post-391437130
http://php.net/manual/zh/function.iconv.php
Http://zhidao.baidu.com/question/157551132.html?fr=iks&word=php+%BD%E2%CE%F6xml%C2%D2%C2%EB&ie=gbk
Simplexml_load_string parsing GB2312 encoded XML