解析以多种格式传递的数据......如何从中获取我需要的数据
Posted
技术标签:
【中文标题】解析以多种格式传递的数据......如何从中获取我需要的数据【英文标题】:Parsing Data passed in multiple formats... how to get data I need from it 【发布时间】:2020-01-20 22:29:30 【问题描述】:Thrivecart 正在通过 webhook 传递数据。这是它传回的一些示例代码,即假数据,但格式相同。
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¤cy=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
所以我创建了一个解析函数,它获取 $_REQUEST 中的所有数据并对其进行解析,以便它可以找到该数据。
这是它输出给我的内容:
"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"
我需要的字段在“客户”部分,但它是一个数组。
我尝试了多种方法,但似乎无法获得该数据。我看起来像是一个 json 数组。 所以我尝试了这段代码,其他人向其他为他们工作的人提出了建议,但它对我不起作用:
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";
更新,尝试了cmets中建议的那个,它也不起作用,尝试这样:
$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);
所以又一次尝试失败了。 :(
我尝试了多个版本,但始终无法获取数据以将其提取出来,因此我可以弄清楚如何从 webhook 中发布的数据中获取我需要的内容。
有人知道怎么弄吗?
更新:好的,有些进展...
这个:var_dump 作为数组的“客户”数据的值向我展示了这一点:
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'
既然我知道“客户”中的数据可以通过这种方式转储,我将如何从中获取“电子邮件”值?还有那个“ip_address”值? “first_name”值?基本上我需要的所有字段?
有没有办法将它放入某种类型的哈希或我可以这样称呼它的东西:
$fieldsemail or $fields["email"]
不管怎样?
进展……
另一个发现...在throwcart 中,他们这样说(这里:https://support.thrivecart.com/help/using-webhook-notifications/)关于他们如何传递数据:
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.
和
传递的数据: 客户:数组(姓名、名字、姓氏、电子邮件、地址)
知道如何利用这些知识获得它吗?
【问题讨论】:
Parse query string into an array的可能重复 不一样,我想弄清楚如何获取数据的价值。就像在具有“阵列”的“客户”中......有一个电子邮件地址,我需要将其拉出,以便我可以将他们的访问级别升级到下一个级别,但它在阵列中,所以不知道如何到达那个数组,我尝试了 4 种不同的方法,没有任何内容。此页面:support.thrivecart.com/help/using-webhook-notifications 显示了它提供的数据的图像,自定义字段如下所示:"id":"6782386","email":"jsmith@email.com"... 等等 那么怎么做我在那个 json 查找数据中收到电子邮件? 【参考方案1】:我必须在这里同意@ggorlen,因为您需要的解决方案已在其他答案中讨论,但以防万一:
$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¤cy=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
会变成一个包含这个的数组:
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//'
现在是棘手的部分:)
访问客户数据:
$data['customer']['email']; // is the email
$data['customer']['ip_address']; // is the ip address
$data['customer']['address']['city'] // you get it
如果你想要一些更短的变量名,只需像这样分配它:
$email = $data['customer']['email'];
【讨论】:
以上是关于解析以多种格式传递的数据......如何从中获取我需要的数据的主要内容,如果未能解决你的问题,请参考以下文章