简体   繁体   中英

Parsing Data passed in multiple formats… how to get data I need from it

Thrivecart is passing data through a webhook. here is some sample code it passes back, that is fake data, but the same format.

event=order.success&mode=test&mode_int=1&thrivecart_account=generic&thrivecart_secret=JLZE3Y54FEQ1&base_product=2&order_id=1514394&invoice_id=000000004&order_date=2019-03-06%2022%3A57%3A24&order_timestamp=1551913044&currency=USD&customer_id=6702306&customer_identifier=cus_EeRXonHQ7LQPBQ&customer%5Bid%5D=6702306&customer%5Bemail%5D=jsmith%40email.com&customer%5Baddress%5D%5Bcountry%5D=NZ&customer%5Baddress%5D%5Bstate%5D=Bay%20of%20Plenty&customer%5Baddress%5D%5Bline1%5D=1234%20Main%20Street&customer%5Baddress%5D%5Bcity%5D=Auckland&customer%5Baddress%5D%5Bzip%5D=3345&customer%5Bip_address%5D=255.255.255.255&customer%5Bname%5D=John%20Smith&customer%5Bcheckbox_confirmation%5D=false&customer%5Bfirst_name%5D=John&customer%5Blast_name%5D=Smith&order%5Btax%5D=null&order%5Btax_type%5D=null&order%5Bprocessor%5D=stripe&order%5Btotal%5D=10000&order%5Btotal_str%5D=100.00&order%5Bcharges%5D%5B0%5D%5Bname%5D=Webhook%20testing&order%5Bcharges%5D%5B0%5D%5Breference%5D=2&order%5Bcharges%5D%5B0%5D%5Bitem_type%5D=product&order%5Bcharges%5D%5B0%5D%5Bitem_identifier%5D=product_2&order%5Bcharges%5D%5B0%5D%5Bamount%5D=10000&order%5Bcharges%5D%5B0%5D%5Bamount_str%5D=100.00&order%5Bcharges%5D%5B0%5D%5Btype%5D=single&order%5Bcharges%5D%5B0%5D%5Bquantity%5D=1&order%5Bcharges%5D%5B0%5D%5Bpayment_plan_id%5D=17869&order%5Bcharges%5D%5B0%5D%5Bpayment_plan_name%5D=Subscription%20%28%24100.00%2Fmnth%29&order%5Bcharges%5D%5B1%5D%5Bname%5D=Webhook%20testing&order%5Bcharges%5D%5B1%5D%5Breference%5D=2&order%5Bcharges%5D%5B1%5D%5Bitem_type%5D=product&order%5Bcharges%5D%5B1%5D%5Bitem_identifier%5D=product_2&order%5Bcharges%5D%5B1%5D%5Bamount%5D=10000&order%5Bcharges%5D%5B1%5D%5Bamount_str%5D=100.00&order%5Bcharges%5D%5B1%5D%5Btype%5D=recurring&order%5Bcharges%5D%5B1%5D%5Bquantity%5D=1&order%5Bcharges%5D%5B1%5D%5Bfrequency%5D=month&order%5Bcharges%5D%5B1%5D%5Bpayment_plan_id%5D=17869&order%5Bcharges%5D%5B1%5D%5Bpayment_plan_name%5D=Subscription%20%28%24100.00%2Fmnth%29&order%5Bdate%5D=2019-03-06%2022%3A57%3A25&order%5Bdate_unix%5D=1551913045&transactions%5Bproduct-2%5D=ch_1EB8ftAB5gsRjJxz4wuVM65M&subscriptions%5Bproduct-2%5D=sub_EeRZvC1TxlYaT7&purchases%5B0%5D=Webhook%20testing&purchase_map%5B0%5D=product-2&purchase_map_flat=product-2&fulfillment%5Burl%5D=https%3A%2F%2Fgeneric.thrivecart.com%2Fwebhook-testing%2Fconfirm%2F%2F

So I created a parse function that takes all the data in the $_REQUEST and parses it, so that it can find that data.

Here is what it output to me:

"event"="order.success"
"mode"="test"
"mode_int"="1"
"thrivecart_account"="generic"
"thrivecart_secret"="JLZE3Y54FEQ1"
"base_product"="2"
"order_id"="1514394"
"invoice_id"="000000004"
"order_date"="2019-03-06 22:57:24"
"order_timestamp"="1551913044"
"currency"="USD"
"customer_id"="6702306"
"customer_identifier"="cus_EeRXonHQ7LQPBQ"
"customer"="Array"
"order"="Array"
"transactions"="Array"
"subscriptions"="Array"
"purchases"="Array"
"purchase_map"="Array"
"purchase_map_flat"="product-2"
"fulfillment"="Array"

My field I need is in the "customer" part, but it is an array.

I tried multiple ways and cannot seem to get that data. I looks like it is a json array. So I tried this code, someone else suggested to someone else that worked for them, but it did not work for me:

foreach ($_REQUEST as $key => $value) {
    if($key == "customer") {
        foreach($value->data as $mydata) {
            echo $mydata->name . "\n";
            foreach($mydata->values as $values) {
                echo $values->value . "\n";
            }
        }
    }
    echo '"' . $key . '"="' . $value . '"<br />' . "\n";
 }

Update, Tried the one suggested in comments, it did not work either, tried it like this:

$json = $value;
$obj = json_decode($json);
echo "... found email='" .  $obj->{'email'} . '"';
parse_str($json, $get_array);
print_r($get_array);
parse_str($value, $get_array2);
print_r($get_array2);

So Yet another attempt failed. :(

I tried multiple versions, but never do I get the data to pull it out, so that I can figure out how to grab what I need out of the data posted in the webhook.

Anyone know how to get it?

UPDATE: okay, SOME progress...

this: var_dump of the value of the "customer" data that is an array, showed me this:

NULL array(8) { ["id"]=> string(7) "6702306" ["email"]=> string(16) "jsmith@email.com" ["address"]=> array(5) { ["country"]=> string(2) "NZ" ["state"]=> string(13) "Bay of Plenty" ["line1"]=> string(16) "1234 Main Street" ["city"]=> string(8) "Auckland" ["zip"]=> string(4) "3345" } ["ip_address"]=> string(15) "255.255.255.255" ["name"]=> string(10) "John Smith" ["checkbox_confirmation"]=> string(5) "false" ["first_name"]=> string(4) "John" ["last_name"]=> string(5) "Smith" } qstring: 'Array'

so now that I know the data in "customer" can be dumped that way, how would I get the "email" value out of it? also that "ip_address" value? the "first_name" value? basically all the fields I need?

Is there a way to put that into some type of hash or something where I could call it like this:

$fields{email} or $fields["email"]

Either way?

Progress...

ANother find... in thrivecart they say this (here: https://support.thrivecart.com/help/using-webhook-notifications/ ) about how they pass data:

We send the data through as POST variables as key/value pairs, and in these, we do have nested variables which create objects/arrays when decoded.

and

passed data: customer: array (name, firstname, lastname, email, address)

Any idea how to get it with that knowledge?

I have to agree with @ggorlen here, as the solution you need is discussed in that other answer, but just in case:

$your_query_string = 'event=order.success&mode=test&mode_int=1&thrivecart_account=generic&thrivecart_secret=JLZE3Y54FEQ1&base_product=2&order_id=1514394&invoice_id=000000004&order_date=2019-03-06%2022%3A57%3A24&order_timestamp=1551913044&currency=USD&customer_id=6702306&customer_identifier=cus_EeRXonHQ7LQPBQ&customer%5Bid%5D=6702306&customer%5Bemail%5D=jsmith%40email.com&customer%5Baddress%5D%5Bcountry%5D=NZ&customer%5Baddress%5D%5Bstate%5D=Bay%20of%20Plenty&customer%5Baddress%5D%5Bline1%5D=1234%20Main%20Street&customer%5Baddress%5D%5Bcity%5D=Auckland&customer%5Baddress%5D%5Bzip%5D=3345&customer%5Bip_address%5D=255.255.255.255&customer%5Bname%5D=John%20Smith&customer%5Bcheckbox_confirmation%5D=false&customer%5Bfirst_name%5D=John&customer%5Blast_name%5D=Smith&order%5Btax%5D=null&order%5Btax_type%5D=null&order%5Bprocessor%5D=stripe&order%5Btotal%5D=10000&order%5Btotal_str%5D=100.00&order%5Bcharges%5D%5B0%5D%5Bname%5D=Webhook%20testing&order%5Bcharges%5D%5B0%5D%5Breference%5D=2&order%5Bcharges%5D%5B0%5D%5Bitem_type%5D=product&order%5Bcharges%5D%5B0%5D%5Bitem_identifier%5D=product_2&order%5Bcharges%5D%5B0%5D%5Bamount%5D=10000&order%5Bcharges%5D%5B0%5D%5Bamount_str%5D=100.00&order%5Bcharges%5D%5B0%5D%5Btype%5D=single&order%5Bcharges%5D%5B0%5D%5Bquantity%5D=1&order%5Bcharges%5D%5B0%5D%5Bpayment_plan_id%5D=17869&order%5Bcharges%5D%5B0%5D%5Bpayment_plan_name%5D=Subscription%20%28%24100.00%2Fmnth%29&order%5Bcharges%5D%5B1%5D%5Bname%5D=Webhook%20testing&order%5Bcharges%5D%5B1%5D%5Breference%5D=2&order%5Bcharges%5D%5B1%5D%5Bitem_type%5D=product&order%5Bcharges%5D%5B1%5D%5Bitem_identifier%5D=product_2&order%5Bcharges%5D%5B1%5D%5Bamount%5D=10000&order%5Bcharges%5D%5B1%5D%5Bamount_str%5D=100.00&order%5Bcharges%5D%5B1%5D%5Btype%5D=recurring&order%5Bcharges%5D%5B1%5D%5Bquantity%5D=1&order%5Bcharges%5D%5B1%5D%5Bfrequency%5D=month&order%5Bcharges%5D%5B1%5D%5Bpayment_plan_id%5D=17869&order%5Bcharges%5D%5B1%5D%5Bpayment_plan_name%5D=Subscription%20%28%24100.00%2Fmnth%29&order%5Bdate%5D=2019-03-06%2022%3A57%3A25&order%5Bdate_unix%5D=1551913045&transactions%5Bproduct-2%5D=ch_1EB8ftAB5gsRjJxz4wuVM65M&subscriptions%5Bproduct-2%5D=sub_EeRZvC1TxlYaT7&purchases%5B0%5D=Webhook%20testing&purchase_map%5B0%5D=product-2&purchase_map_flat=product-2&fulfillment%5Burl%5D=https%3A%2F%2Fgeneric.thrivecart.com%2Fwebhook-testing%2Fconfirm%2F%2F';

$data = [];
parse_str($your_query_string, $data);

$data will become an array containing this:

array(21)
{
    ['event'] => string(13) 'order.success'
    ['mode'] => string(4) 'test'
    ['mode_int'] => string(1) '1'
    ['thrivecart_account'] => string(7) 'generic'
    ['thrivecart_secret'] => string(12) 'JLZE3Y54FEQ1'
    ['base_product'] => string(1) '2'
    ['order_id'] => string(7) '1514394'
    ['invoice_id'] => string(9) '000000004'
    ['order_date'] => string(19) '2019-03-06 22:57:24'
    ['order_timestamp'] => string(10) '1551913044'
    ['currency'] => string(3) 'USD'
    ['customer_id'] => string(7) '6702306'
    ['customer_identifier'] => string(18) 'cus_EeRXonHQ7LQPBQ'
    ['customer'] => array(8)
    {
        ['id'] => string(7) '6702306'
        ['email'] => string(16) 'jsmith@email.com'
        ['address'] => array(5)
        {
            ['country'] => string(2) 'NZ'
            ['state'] => string(13) 'Bay of Plenty'
            ['line1'] => string(16) '1234 Main Street'
            ['city'] => string(8) 'Auckland'
            ['zip'] => string(4) '3345'
        }
        ['ip_address'] => string(15) '255.255.255.255'
        ['name'] => string(10) 'John Smith'
        ['checkbox_confirmation'] => string(5) 'false'
        ['first_name'] => string(4) 'John'
        ['last_name'] => string(5) 'Smith'
    }
    ['order'] => array(8)
    {
        ['tax'] => string(4) 'null'
        ['tax_type'] => string(4) 'null'
        ['processor'] => string(6) 'stripe'
        ['total'] => string(5) '10000'
        ['total_str'] => string(6) '100.00'
        ['charges'] => array(2)
        {
            [0] => array(10)
            {
                ['name'] => string(15) 'Webhook testing'
                ['reference'] => string(1) '2'
                ['item_type'] => string(7) 'product'
                ['item_identifier'] => string(9) 'product_2'
                ['amount'] => string(5) '10000'
                ['amount_str'] => string(6) '100.00'
                ['type'] => string(6) 'single'
                ['quantity'] => string(1) '1'
                ['payment_plan_id'] => string(5) '17869'
                ['payment_plan_name'] => string(27) 'Subscription ($100.00/mnth)'
            }
            [1] => array(11)
            {
                ['name'] => string(15) 'Webhook testing'
                ['reference'] => string(1) '2'
                ['item_type'] => string(7) 'product'
                ['item_identifier'] => string(9) 'product_2'
                ['amount'] => string(5) '10000'
                ['amount_str'] => string(6) '100.00'
                ['type'] => string(9) 'recurring'
                ['quantity'] => string(1) '1'
                ['frequency'] => string(5) 'month'
                ['payment_plan_id'] => string(5) '17869'
                ['payment_plan_name'] => string(27) 'Subscription ($100.00/mnth)'
            }
        }
        ['date'] => string(19) '2019-03-06 22:57:25'
        ['date_unix'] => string(10) '1551913045'
    }
    ['transactions'] => array(1)
    {
        ['product-2'] => string(27) 'ch_1EB8ftAB5gsRjJxz4wuVM65M'
    }
    ['subscriptions'] => array(1)
    {
        ['product-2'] => string(18) 'sub_EeRZvC1TxlYaT7'
    }
    ['purchases'] => array(1)
    {
        [0] => string(15) 'Webhook testing'
    }
    ['purchase_map'] => array(1)
    {
        [0] => string(9) 'product-2'
    }
    ['purchase_map_flat'] => string(9) 'product-2'
    ['fulfillment'] => array(1)
    {
        ['url'] => string(56) 'https://generic.thrivecart.com/webhook-testing/confirm//'
    }
}

And now the tricky part:)

To access the customer data:

$data['customer']['email']; // is the email
$data['customer']['ip_address']; // is the ip address
$data['customer']['address']['city'] // you get it

If you want some shorter variable name, just assign it like this:

$email = $data['customer']['email'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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