Maison > Article > développement back-end > PHP如何解析 WEBSERVICES接口返回的数据
WEBSERVICE服务器,他们用.net写了个接口 http://XXX.XXX.XXX.XX/Info.asmx?wsdl
我用PHP调用这个接口
try{ $wsdl = "http://XXX.XXX.XXX.XX/Info.asmx?wsdl"; $client = new SoapClient($wsdl); $param = array('strName'=>'吴小明', 'strIDCard'=>'4528XXXXXXXX', 'decZiparea'=>'450900'); $res = $client->GetBasicInfo($param); print_r($res);}catch(SOAPFault $e){ var_dump($e);}
stdClass Object( [GetBasicInfoResult] => stdClass Object ( [any] => <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Employee" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Employee"> <xs:complexType> <xs:sequence> <xs:element name="ID_ECERT" type="xs:decimal" minOccurs="0"/> <xs:element name="ECERTID" type="xs:string" minOccurs="0"/> <xs:element name="NAME" type="xs:string" minOccurs="0"/> <xs:element name="SEX" type="xs:string" minOccurs="0"/> <xs:element name="IDCARD" type="xs:string" minOccurs="0"/> <xs:element name="BIRTHDAY" type="xs:dateTime" minOccurs="0"/> <xs:element name="ECERTAREA" type="xs:string" minOccurs="0"/> <xs:element name="FIRSTDATE" type="xs:dateTime" minOccurs="0"/> <xs:element name="BEGINDATE" type="xs:dateTime" minOccurs="0"/> <xs:element name="ENDDATE" type="xs:dateTime" minOccurs="0"/> <xs:element name="GRANTORGAN" type="xs:string" minOccurs="0"/> <xs:element name="CREDITLEVEL" type="xs:string" minOccurs="0"/> <xs:element name="TOTALSCORE" type="xs:unsignedByte" minOccurs="0"/> <xs:element name="TEL" type="xs:string" minOccurs="0"/> <xs:element name="DRVLICENSE" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <DocumentElement xmlns=""> <Employee diffgr:id="Employee1" msdata:rowOrder="0"> <ID_ECERT>4507…………</ID_ECERT> <ECERTID>4507…………</ECERTID> <NAME>吴……</NAME> <SEX>男性</SEX> <IDCARD>4528…………</IDCARD> <BIRTHDAY>1966-03-10T00:00:00+08:00</BIRTHDAY> <ECERTAREA>道路…………</ECERTAREA> <FIRSTDATE>2011-05-23T00:00:00+08:00</FIRSTDATE> <BEGINDATE>2011-05-23T00:00:00+08:00</BEGINDATE> <ENDDATE>2017-05-22T00:00:00+08:00</ENDDATE> <GRANTORGAN>钦州市…………</GRANTORGAN> <CREDITLEVEL>AAA级</CREDITLEVEL> <DRVLICENSE>C1证</DRVLICENSE> </Employee> </DocumentElement> </diffgr:diffgram> ))
您给出的数据缺少根节点,自己补上吧
数据分为两部分,第一部分是数据类型声明,第二部分是数据
我们关心的是第二部分
用 SimpleXMLElement 可得
SimpleXMLElement Object( [DocumentElement] => SimpleXMLElement Object ( [Employee] => SimpleXMLElement Object ( [ID_ECERT] => 4507………… [ECERTID] => 4507………… [NAME] => 吴…… [SEX] => 男性 [IDCARD] => 4528………… [BIRTHDAY] => 1966-03-10T00:00:00+08:00 [ECERTAREA] => 道路………… [FIRSTDATE] => 2011-05-23T00:00:00+08:00 [BEGINDATE] => 2011-05-23T00:00:00+08:00 [ENDDATE] => 2017-05-22T00:00:00+08:00 [GRANTORGAN] => 钦州市………… [CREDITLEVEL] => AAA级 [DRVLICENSE] => C1证 ) ))
您给出的数据缺少根节点,自己补上吧
数据分为两部分,第一部分是数据类型声明,第二部分是数据
我们关心的是第二部分
用 SimpleXMLElement 可得
SimpleXMLElement Object( [DocumentElement] => SimpleXMLElement Object ( [Employee] => SimpleXMLElement Object ( [ID_ECERT] => 4507………… [ECERTID] => 4507………… [NAME] => 吴…… [SEX] => 男性 [IDCARD] => 4528………… [BIRTHDAY] => 1966-03-10T00:00:00+08:00 [ECERTAREA] => 道路………… [FIRSTDATE] => 2011-05-23T00:00:00+08:00 [BEGINDATE] => 2011-05-23T00:00:00+08:00 [ENDDATE] => 2017-05-22T00:00:00+08:00 [GRANTORGAN] => 钦州市………… [CREDITLEVEL] => AAA级 [DRVLICENSE] => C1证 ) ))
您给出的数据缺少根节点,自己补上吧
数据分为两部分,第一部分是数据类型声明,第二部分是数据
我们关心的是第二部分
用 SimpleXMLElement 可得
SimpleXMLElement Object( [DocumentElement] => SimpleXMLElement Object ( [Employee] => SimpleXMLElement Object ( [ID_ECERT] => 4507………… [ECERTID] => 4507………… [NAME] => 吴…… [SEX] => 男性 [IDCARD] => 4528………… [BIRTHDAY] => 1966-03-10T00:00:00+08:00 [ECERTAREA] => 道路………… [FIRSTDATE] => 2011-05-23T00:00:00+08:00 [GRANTORGAN] => 钦州市………… ) ))
用正则截取
$s = preg_replace('/.+(<diffgr.+)$/s', '$1', $res->GetBasicInfoResult->any);$xml = simplexml_load_string($s);print_r($xml);就可得到我示例的结果
我原以为可以直接拿到数据,原来还是要对字符串进行操作,算了,结帖吧