简体   繁体   English

如何从 PHP Guzzle 请求获取 XML 响应数据

[英]How to get data on an XML response from a PHP Guzzle requests

I have the following request and am using guzzle to process it.我有以下请求,正在使用 guzzle 来处理它。 I am attempting to get the ID and NAME hopefully in key => value array but it doesn't seem to work.我试图在key => value数组中获取IDNAME ,但它似乎不起作用。

$api_url = 'http://example.com/test.asmx/GetUserDetails?userID=123';
$client = new Client();
$results = $client->request('GET', $api_url)->getBody()->getContents();
dd($results);

The response gotten when I echo $results is shown below当我echo $results时得到的响应如下所示

123EXAMPLE

Also, the response gotten when I dd($results) comes in triple quotes """ """ as shown below此外,当我dd($results)出现在三重引号""" """时得到的响应如下所示

"""
<?xml version="1.0" encoding="utf-8"?>

<DataSet xmlns="http://example.com">

  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:string" minOccurs="0" />
                <xs:element name="NAME" 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">
    <NewDataSet xmlns="">
      <Table diffgr:id="Table1" msdata:rowOrder="0">
        <ID>123</ID>
        <NAME>EXAMPLE</NAME>
      </Table>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>
"""

I tried using SimpleXMLElement but it resulted in this response SimpleXMLElement {#642}我尝试使用 SimpleXMLElement 但它导致了这个响应SimpleXMLElement {#642}

$response1 = simplexml_load_string($results);
$response2 = SimpleXMLElement($results);

Also, I used json_encode and json_decode but none worked.另外,我使用了json_encodejson_decode但都没有用。

Please, any help is appreciated.请,任何帮助表示赞赏。

It seems the namespaces are complicating the situation.名称空间似乎使情况复杂化。 This code:这段代码:

$response2 = new SimpleXMLElement($results);
$children = $response2->children('diffgr', true);
$children2 = $children->children();
echo $children2->NewDataSet->Table->ID;
echo PHP_EOL; //newline
echo $children2->NewDataSet->Table->NAME;

will get you the data.会给你数据。

It first gets all the children of the outer XML which have the "diffgr" namespace (which is only one, in this case).它首先获取具有“diffgr”命名空间(在本例中只有一个)的外部 XML 的所有子级。 Then within that child, its own child elements don't have a namespace again, so we have to use the children() function again without a namepsace specified in order to retrieve those.然后在那个子元素中,它自己的子元素再次没有命名空间,因此我们必须再次使用 children() 函数而不指定命名空间来检索它们。

Live demo: http://sandbox.onlinephpfunctions.com/code/3cd49767e5b1dd237d26b9f03ceff4dae0546eae现场演示: http : //sandbox.onlinephpfunctions.com/code/3cd49767e5b1dd237d26b9f03ceff4dae0546eae

Credit to this post for the idea. 这个想法归功于这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM