php - 解析以多种格式传递的数据......如何从中获取我需要的数据
问题描述
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";
}
更新,尝试了评论中建议的那个,它也不起作用,尝试这样:
$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”值?基本上我需要的所有领域?
有没有办法将它放入某种类型的哈希或我可以这样称呼它的东西:
$fields{email} 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.
和
传递的数据:客户:数组(姓名、名字、姓氏、电子邮件、地址)
知道如何利用这些知识获得它吗?
解决方案
我必须在这里同意@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'];
推荐阅读
- javascript - Chrome 调试器显示错误的值
- bash - 如何在隐藏命令的同时读取命令输出?
- vue.js - VueJs - $route 在页面加载时不可用
- java - 制作一个循环,在遇到冒号字符之前返回字符
- node.js - 单击后禁用按钮
- python - 使用 python 从 Bash 脚本输出创建 Excel 文件
- python - 这两个代码应该做完全一样的事情,但是第一个没有像我预期的那样工作
- rest - 在分布式环境中调用一堆 REST API
- java - 使用 Spring Data Redis 序列化时如何正确忽略类成员?
- java - 在 poi 中复制过滤后的数据