1

我正在尝试创建一个脚本来从 FoxyCart 的 Order Desk 接收 POST。我在 requestb.in 设置了一个临时请求箱。当我将 POST 指向那里时,我得到了新的 JSON 订单。我用这段代码在我的脚本中复制/粘贴了 JSON 字符串,我能够解析 JSON 并成功地将变量添加到我的数据库中:

$string = "{"id":"1191583","email":"office@burntimpressions.com","shipping_method":"","quantity_total":1,"weight_total":0,"product_total":0,"shipping_total":0,"handling_total":0,"tax_total":0,"discount_total":0,"order_total":0,"cc_number_masked":"","cc_exp":"","processor_response":"","payment_type":"","payment_status":"Approved","processor_balance":0,"customer_id":"","email_count":"1","ip_address":"71.161.83.59","tag_color":"","source_name":"Order Desk","source_id":"1191583","fulfillment_name":"","fulfillment_id":"","tag_name":"","folder_id":6814,"date_added":"2015-06-14 16:53:54","date_updated":"2015-06-14 16:54:27","shipping":{"first_name":"Galen","last_name":"Dively","company":"Vermont Novelty Toaster Corporation","address1":"136 Bay Street","address2":"Ste 2","address3":"","address4":"","city":"Saint Johnsbury","state":"Vermont","postal_code":"05819","country":"US","phone":"(916) 448-5517"},"customer":{"first_name":"Galen","last_name":"Dively","company":"Vermont Novelty Toaster Corporation","address1":"136 Bay Street","address2":"Ste 2","city":"Saint Johnsbury","state":"Vermont","postal_code":"05819","country":"US","phone":"(916) 448-5517"},"checkout_data":[],"order_metadata":[],"discount_list":[],"order_notes":[],"order_items":[{"id":"2090400","name":"selfie test","price":0,"quantity":1,"weight":0,"code":"selfie","delivery_type":"ship","category_code":"","variation_list":[],"metadata":[]}],"order_shipments":[]}";
$order = json_decode($string, true);
$transactionId = $order [id];
$firstName = $order[customer][first_name];
foreach($order [order_items] as $p)
{
$cid = $p[id];
$productName = $p[name];
$code = $p[code];
$quantity = $p[quantity];
}

Order Desk 的文档说要通过以下方式获取 POST:

$order = json_decode($_POST['order'], 1);

我什么也得不到。我已经尝试了几乎所有我能想到的东西。浏览了这个论坛,没有发现任何可以解析 JSON 的东西。我假设 POST 是成功的,因为我没有从 Order Desk 的管理面板中收到任何错误。

出于安全目的,Order Desk 有此代码。POST 似乎通过了所有这些。

<?php
//Check For Order
if (!isset($_POST['order'])) {
    header(':', true, 400);
    die('No Data Found');
}

//Cbeck Store ID
//Be sure to set your store ID. Ask Order Desk support if you aren't sure    what it is.
if (!isset($_SERVER['HTTP_X_ORDER_DESK_STORE_ID']) ||   $_SERVER['HTTP_X_ORDER_DESK_STORE_ID'] != "YOUR-STORE-ID") {
    header(':', true, 403);
    die('Unauthorized Request');
}

//Check the Hash (optional)
//The API Key can be found in the Advanced Settings section. Order Desk Pro only
if (!isset($_SERVER['HTTP_X_ORDER_DESK_HASH']) || hash_hmac('sha256', rawurldecode($_POST['order']), 'YOUR_API_KEY') != $_SERVER['HTTP_X_ORDER_DESK_HASH']) {
    header(':', true, 403);
    die('Unauthorized Request');
}

//Check Order Data
$order = json_decode($_POST['order'], 1);
if (!is_array($order)) {
    header(':', true, 400);
    die('Invalid Order Data');
}

//Everything Checks Out -- do your thing
echo "<pre>" . print_r($order, 1) . "</pre>";

任何帮助是极大的赞赏!

我想我在问你将如何解析:

$order = json_decode($_POST['order'], 1);

我从 requestb.in 得到什么:

表格/发布参数

order: {"id":"1191583","email":"office@burntimpressions.com","shipping_method":"","quantity_total":1,"weight_total":0,"product_total":0,"shipping_total":0,"handling_total":0,"tax_total":0,"discount_total":0,"order_total":0,"cc_number_masked":"","cc_exp":"","processor_response":"","payment_type":"","payment_status":"Approved","processor_balance":0,"customer_id":"","email_count":"1","ip_address":"71.161.83.59","tag_color":"","source_name":"Order Desk","source_id":"1191583","fulfillment_name":"","fulfillment_id":"","tag_name":"","folder_id":6814,"date_added":"2015-06-14 16:53:54","date_updated":"2015-06-14 16:54:27","shipping":{"first_name":"Galen","last_name":"Dively","company":"Vermont Novelty Toaster Corporation","address1":"136 Bay Street","address2":"Ste 2","address3":"","address4":"","city":"Saint Johnsbury","state":"Vermont","postal_code":"05819","country":"US","phone":"(916) 448-5517"},"customer":{"first_name":"Galen","last_name":"Dively","company":"Vermont Novelty Toaster Corporation","address1":"136 Bay Street","address2":"Ste 2","city":"Saint Johnsbury","state":"Vermont","postal_code":"05819","country":"US","phone":"(916) 448-5517"},"checkout_data":[],"order_metadata":[],"discount_list":[],"order_notes":[],"order_items":[{"id":"2090400","name":"selfie test","price":0,"quantity":1,"weight":0,"code":"selfie","delivery_type":"ship","category_code":"","variation_list":[],"metadata":[]}],"order_shipments":[]}

标题

Total-Route-Time: 0
Connect-Time: 1
X-Request-Id: 26eb3b73-bd15-4d75-9605-ea4fda0191dd
User-Agent: Guzzle/5.3.0 curl/7.19.7 PHP/5.5.21
X-Order-Desk-Store-Id: XXXXXXX
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 2228
Via: 1.1 vegur
Host: requestb.in
X-Order-Desk-Hash: XXXXXXXXXXXXX
4

0 回答 0