BigCommerce API - 按ID获取订单产品

时间:2017-07-12 06:46:22

标签: php api curl bigcommerce php-curl

我正在尝试使用PHP cURL中的以下BigCommerce API调用获取订单集合中给定product_id的所有订单产品:

Get an Order Product

$pid = $_POST['product_id'];

$filter = array("status_id"=>11);

$orders = Bigcommerce::getOrders($filter);

foreach($orders as $order) {
    $oid = $order->id;
    $URL = $store_url . "/api/v2/orders/$oid/products/$pid.json";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Get status code

    $response = curl_exec($ch);
    curl_close($ch);

    $result = json_decode($response);

    echo "OID: " . $oid . " PID: " . $pid . " var_dump: ";
    print_r(var_dump($result) . "<br/>");
}

输出:

OID: 113948 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#3 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 
OID: 113949 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#220 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 
OID: 113954 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#3 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 
OID: 113957 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#220 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 

起初,我认为Orders可能没有包含我正在寻找的product_id所以我将此代码添加到循环的顶部:

//Set $pid equal to the product_id of the first Order Product in the Order.
$pid = $order->products[0]->product_id; 

新输出:

OID: 113948 PID: 3703 var_dump: array(1) { [0]=> object(stdClass)#3 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 
OID: 113949 PID: 3627 var_dump: array(1) { [0]=> object(stdClass)#220 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 
OID: 113954 PID: 3816 var_dump: array(1) { [0]=> object(stdClass)#225 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 
OID: 113957 PID: 3646 var_dump: array(1) { [0]=> object(stdClass)#222 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } } 

我正在尝试使用BigCommerce API实现的目标,还是我必须遍历订单集合中的所有订单产品?

0 个答案:

没有答案