我用了一个方式去抓一列something…之后我要解析成xml的格式
当然是可行的,但时不及会出错 于是我写下了
try
{
$sports = new SimpleXMLElement($parsedJson);
}
catch (Exception $e)
{
Print_r($e)
}
结果为
Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Space required after the Public Identifier in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: hr line 5 and BODY in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 6: parser error : Opening and ending tag mismatch: BODY line 4 and HTML in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HEAD line 2 in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): Entity: line 7: parser error : Premature end of data in tag HTML line 2 in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): in /home/ns/www/admin/gamble/baseball.php on line 121 Warning: SimpleXMLElement::__construct(): ^ in /home/ns/www/admin/gamble/baseball.php on line 121
我想到我之前用 curl 抓东西时,用过 json_decode
当时我用这样的方式来让程序自动重抓
do{
$data = @json_decode(file_get_contents($url), true);
}while($data == false);
我想请教的是
如果我的 new SimpleXMLElement 失败了,我也可以用类似的方式作吗?
Do
{
抓资料;
$data=@json_decode(something);
$xml = new SimpleXMLElement($data);
}while($xml == false);
如果 new SimpleXMLElemen 发生以上的错误,$xml 会 等于 false吗?
求解
首先,问题可能是因为xml捕获了一个非正常XML格式的数据,如果要捕获错误,可以这样
另外,你通过file_get_contents($url),直接抓取URL,是有可能抓不全数据的哦,这里我推荐一个HTTP请求的类:
简单而强大的HTTP请求类,来自类库:lv
http://levi.cg.am/archives/3534