I have an XML response that looks like this:
<?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>
I am converting it to a useable array with this:
$xml = simplexml_load_string($request);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
It works super and gives me this array:
{"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"}}
The problem is that this webhook sends several different types of responses (It sends all responses to all webhooks addresses, I can't subscribe to just the one I want) and I only want to do work on responses with the <new_subscription_notification>
top level XML element. When I do all my conversion to array, that element is not in the array, since it is essentially just the element, but in this case, it has useful data.
Is there a way I can access that top level XML tag so that my code only runs when it gets the <new_subscription_notification>
response?