Php 正在分析以多种格式传递的数据。。。如何从中获取我需要的数据
ThreeveCart正在通过webhook传递数据。下面是它传回的一些示例代码,即伪数据,但格式相同Php 正在分析以多种格式传递的数据。。。如何从中获取我需要的数据,php,arrays,json,Php,Arrays,Json,ThreeveCart正在通过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=201
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
因此,我创建了一个parse函数,它接受$\u请求中的所有数据并对其进行解析,以便它能够找到这些数据
以下是它向我输出的内容:
"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";
}
因此,又一次尝试失败了(
我尝试了多个版本,但从来没有获得数据来提取它,这样我就可以知道如何从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'
既然我知道“customer”中的数据可以通过这种方式转储,那么如何从中获取“email”值?还有“ip\u address”值?first\u name?基本上就是我需要的所有字段
有没有一种方法可以将其放入某种类型的散列或类似的东西中,我可以这样称呼它:
$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);
$fields{email} or $fields["email"]
$email = $data['customer']['email'];
不管怎样
进展
另一项发现……在thrivecart中,他们说(这里:)关于他们如何传递数据:
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
如果需要较短的变量名,只需按如下方式分配:
$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);
$fields{email} or $fields["email"]
$email = $data['customer']['email'];
可能的重复不一样,我试图找出如何获得数据的值。就像在“客户”中有一个“数组”。。。有一个电子邮件地址,我需要把它取出来,这样我可以将他们的访问级别升级到下一个级别,但它在阵列中,所以不确定如何访问该阵列,我尝试了4种不同的方式,没有任何内容可以读取它。此页面:显示它提供的数据的图像,自定义字段如下:{“id”:“6782386”,“email”:jsmith@email.com“…}等等,那么我如何在json格式的数据中获取电子邮件呢?