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"]
哪种方式?
进度...
在thrivecart中的另一个发现……他们这样说(在这里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.
和
传递的数据: 客户:数组(名称,名字,姓氏,电子邮件,地址)
有什么想法如何利用这些知识吗?
答案 0 :(得分:0)
我必须在这里同意@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'];