[英]Parsing Mixed XML and HTML with php
I am doing a curl request and getting the following back: 我正在执行curl请求,并返回以下信息:
//....curl stuff....//
$result = curl_exec($curl);
curl_close ($curl);
print_R($result);
<html><body onload="if (parent.submitterLoaded)
parent.submitterLoaded();">{"AuthenticationType":0,
"DateDisplayFormat":1, "SystemURL":"https://rmm.server.com",
"Username”:”user”, "UserID":"12205_1", "Error":"", "Success":true,
"ClientID":1, "SessionGuid":"9eb91231b04-feca-4704-b445-
cc5b369581e3", "tag":"", "LastRequestDateTime":"636421428277379996"}
</body></html><?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<LoginResponse xmlns="http://Iris.net" /></soap:Body></soap:Envelope>
I have tried xml_parser_create and 我尝试了xml_parser_create和
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
and i get a garbled mess in return. 作为回报,我得到了乱码。
Warning: simplexml_load_string(): Entity: line 1: parser error : XML
declaration allowed only at the start of the document in
/var/www/cron/billing/test.php on line 68
PHP Warning: simplexml_load_string(): b6-bd4dd8a0760b",
"LastRequestDateTime":"636421426011959977"}</body></html><?xml in
/var/www/cron/billing/test.php on line 68
PHP Warning: simplexml_load_string():
^ in /var/www/cron/billing/test.php on line 68
I can see what appears to be some json at the {"Keys" area of the response. 我可以在响应的{“ Keys”区域看到一些json。 How can i parse this correctly? 我如何正确解析?
What other info do you need to help answer question? 您还需要什么其他信息来帮助回答问题?
The first warning indicate the parser doesn't like the second part <?xml version..
. 第一个警告表明解析器不喜欢第二部分<?xml version..
。 So get rid of it: 因此,摆脱它:
$result = substr($result, 0, strpos($result, '<?xml version'));
Then to pull out the JSON string, use: 然后要提取JSON字符串,请使用:
$jsonString = (string) simplexml_load_string($result)->body;
$array = json_decode($jsonString);
extract the json with DOMDocument, and parse it with json_decode 使用DOMDocument提取json,然后使用json_decode解析
$domd=@DOMDocument::loadHTML($response);
$json_data=json_decode(trim($domd->getElementsByTagName("body")->item(0)->textContent));
now the stuff in the json can be accessed like $UserID=$json_data->UserID;
现在可以像$UserID=$json_data->UserID;
一样访问json中的内容$UserID=$json_data->UserID;
, ... and the stuff in the HTML can be accessed in $domd, like $loginResponse=$domd->getElementsByTagName("LoginResponse")->item(0)->textContent;
,...和HTML中的内容都可以在$ domd中访问,例如$loginResponse=$domd->getElementsByTagName("LoginResponse")->item(0)->textContent;
- didn't see anything useful in the html other than the json, though.. -不过,除了json之外,没有看到其他有用的html。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.