php - 如何解析顶级XML元素php
问题描述
我有一个如下所示的 XML 响应:
<?xml version="1.0" encoding="UTF-8"?>
<new_subscription_notification>
<account>
<account_code>1</account_code>
<username nil="true">verena</username>
<email>verena@example.com</email>
<first_name>Verena</first_name>
<last_name>Example</last_name>
<company_name nil="true">Company, Inc.</company_name>
</account>
<subscription>
<plan>
<plan_code>bronze</plan_code>
<name>Bronze Plan</name>
</plan>
<uuid>8047cb4fd5f874b14d713d785436ebd3</uuid>
<state>active</state>
<quantity type="integer">2</quantity>
<total_amount_in_cents type="integer">17000</total_amount_in_cents>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_code>premium_support</add_on_code>
<name>Premium Support</name>
<quantity type="integer">1</quantity>
<unit_amount_in_cents type="integer">15000</unit_amount_in_cents>
<external_sku>pre-123<external_sku>
<add_on_type>fixed</add_on_type>
<usage_percentage nil="true"></usage_percentage>
<measured_unit_id nil="true"></measured_unit_id>
</subscription_add_on>
<subscription_add_on>
<add_on_code>email_blasts</add_on_code>
<name>Email Blasts</name>
<quantity type="integer">1</quantity>
<external_sku>email-123<external_sku>
<unit_amount_in_cents type="integer">50</unit_amount_in_cents>
<add_on_type>usage</add_on_type>
<usage_percentage nil="true"></usage_percentage>
<measured_unit_id type="integer">394681687402874853</measured_unit_id>
</subscription_add_on>
<subscription_add_on>
<add_on_code>donations</add_on_code>
<name>Donations</name>
<quantity type="integer">1</quantity>
<unit_amount_in_cents nil="true"></unit_amount_in_cents>
<add_on_type>usage</add_on_type>
<usage_percentage>0.6</usage_percentage>
<measured_unit_id type="integer">394681920153192422</measured_unit_id>
</subscription_add_on>
</subscription_add_ons>
<activated_at type="datetime">2009-11-22T13:10:38Z</activated_at>
<canceled_at type="datetime"></canceled_at>
<expires_at type="datetime"></expires_at>
<current_period_started_at type="datetime">2009-11-22T13:10:38Z</current_period_started_at>
<current_period_ends_at type="datetime">2009-11-29T13:10:38Z</current_period_ends_at>
<trial_started_at type="datetime">2009-11-22T13:10:38Z</trial_started_at>
<trial_ends_at type="datetime">2009-11-29T13:10:38Z</trial_ends_at>
<collection_method>automatic</collection_method>
</subscription>
</new_subscription_notification>
我将其转换为可用的数组:
$xml = simplexml_load_string($request);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
它工作得很好,给了我这个数组:
{"account":{"account_code":"shopify-3207741833301","username":{"@attributes":{"nil":"true"}},"email":"mum.matt@gmail.com","first_name":"Chrysanthemum","last_name":"Hayes","company_name":{"@attributes":{"nil":"true"}},"phone":{"@attributes":{"nil":"true"}}},"invoice":{"uuid":"53f16d5e15f0e9043a2c7c480d89af86","state":"paid","origin":"purchase","invoice_number_prefix":[],"invoice_number":"2567","address":{"address1":"3646 Deedham Drive","address2":[],"city":"San Jose","state":"CA","zip":"95148","country":"US","phone":[]},"vat_number":{"@attributes":{"nil":"true"}},"currency":"USD","balance_in_cents":"0","total_in_cents":"3389","tax_in_cents":"0","subtotal_in_cents":"3389","subtotal_before_discount_in_cents":"3389","discount_in_cents":"0","subscription_ids":{"@attributes":{"type":"array"}},"customer_notes":[],"created_at":"2020-06-05T03:59:23Z","updated_at":"2020-06-05T03:59:23Z","closed_at":"2020-06-05T03:59:23Z","shipping_address":{"name":"Angela Swartz","address1":"1324 Rainbow Dr","address2":[],"city":"San Mateo","state":"CA","zip":"94402","country":"US","phone":[]},"po_number":{"@attributes":{"nil":"true"}},"terms_and_conditions":[],"due_on":"2020-06-05T03:59:23Z","net_terms":"0","collection_method":"automatic"}}
问题是这个 webhook 发送了几种不同类型的响应(它将所有响应发送到所有 webhook 地址,我不能只订阅我想要的一个),我只想处理<new_subscription_notification>
顶级 XML 元素的响应。当我将所有转换为数组时,该元素不在数组中,因为它本质上只是元素,但在这种情况下,它具有有用的数据。
有没有一种方法可以访问该顶级 XML 标记,以便我的代码仅在收到<new_subscription_notification>
响应时运行?
解决方案
推荐阅读
- node.js - npm WARN tar ENOENT 没有这样的文件或目录 node_modules/.staging/typescript-8be04997/lib/zh-CN/diagnosticMessages.generated.json
- amazon-cognito - 使用 AWS Cognito 处理多个“账户”中的一个用户?
- python - How can I print the number of occurences of each value with the value from the csv
- vba - VBA CommandBarButton 不运行代码 OnAction
- macos - USB HID 设备在启动时被 OS X Mojave 抓取。如何避免这种情况?
- amazon-web-services - 为什么使用 Amazon Athena 复制 Parquet 文件大小会变小
- java - @Tag("myTestName") 不适用于 Groovy 测试类
- json - 需要使用一个 AWS CloudFormation 模板为不同的环境启动多个实例
- asp.net-mvc - 什么是 IIS 站点下的应用程序,我们应该何时创建它?
- angular7 - 类型'HttpClient'Angular 7上不存在属性'post'