2015-12-06 178 views
0

我已经看到人们在数组中发送JSON数据的例子。使用cURL发送数据。问题是,他们大多是像使用cURL发送JSON请求到API

$data = array('first_name' => 'Thomas', 'last_name' => 'Woods') 

我的问题是,如果我有一些象下面这样不是一样干切。例如:

"products": [ 
{ 
    "product_id": "B00I9KDFK0", 
    "quantity": 1, 
    "seller_selection_criteria": [ 
    { 
     "condition_in": ["New"], 
     "international": false, 
     "handling_days_max": 5 
    } 
    ] 
} 

],

所以,我怎么会将该信息放入一个数组?

更多信息:香港专业教育学院尝试没有运气以下

<?php 
    // Your ID and token 


    // The data to send to the API 
    $postData = array(
     'client_token' => 'XXXXXXXXXXXXXXXXXXXXXX', 
     'retailer' => 'amazon', 
     'products' => array('product_id' => '0923568964', 'quantity' => '1',      'seller_selection_criteria' => array('condition_in' => 'New', 'international' =>   'false', 'handling_days_max' => '5')), 
     'max_price' => '1300', 
     'shipping_address' => array('first_name' => 'Thomas', 'last_name' =>    'XXXXX', 'address_line1' => 'XXXXXX Loop', 'address_line2' => '', 'zip_code' =>   'XXXXX', 'city' => 'XXXX', 'state' => 'MS', 'country' => 'US', 'phone_number' =>   'XXXXXX'), 
'is_gift' => 'false', 
'gift_message' => "", 
'shipping_method' => 'cheapest', 
'payment_method' => array('name_on_card' => 'XXXXXXXX', 'number' => 'XXXXXXXXXXXX', 'security_code' => 'XXX', 'expiration_month' => 'XX', 'expiration_year' => 'XXXX', 'use_gift' => 'false'), 
'billing_address' => array('first_name' => 'Thomas', 'last_name' => 'XXXXXX', 'address_line1' => 'XXXXXXXXX', 'address_line2' => '', 'zip_code' => 'XXXXXX', 'city' => 'XXXXXXXX', 'state' => 'MS', 'country' => 'US', 'phone_number' => 'XXXXXXXX'), 
'retailer_credentials' => array('email' => 'XXXXXXXX', 'password' =>   'XXXXXXXXX') 
    ); 

    // Setup cURL 
    $ch = curl_init('https://api.zinc.io/v0/order'); 
    curl_setopt_array($ch, array(
CURLOPT_POST => TRUE, 
CURLOPT_RETURNTRANSFER => TRUE, 
CURLOPT_POSTFIELDS => json_encode($postData) 
    )); 

    // Send the request 
    $response = curl_exec($ch); 

    // Check for errors 
      if($response === FALSE){ 
     die(curl_error($ch)); 
    } 

    // Decode the response 
    $responseData = json_decode($response, TRUE); 

    // Print the date from the response 
    echo $responseData['published']; 
    echo $response; 
    ?> 

有错误即时得到的是:

{"_type":"error","_request_id":"5663c16b75f682d920000758","code":"invalid_request","message":"Validation failed on the request.","data":{"validator_errors":[{"value":[],"path":"products","message":"'' is not a permitted value for 'products' field."}]},"host":"zincapi-5","offers_urls":[],"screenshot_urls":[],"_created_at":"2015-12-06T05:02:35.567Z"} 

这里是Json_echo

{"client_token":"xxxxxxxxxxxxxxxxxxxxxxx","retailer":"amazon","products":{"product_id":"0923568964","quantity":1,"seller_selection_criteria":{"condition_in":"New","international":false,"handling_days_max":5}},"max_price":"1300","shipping_address":{"first_name":"Thomas","last_name":"xxxxxx","address_line1":"xxxxx","address_line2":"","zip_code":"xxxx","city":"xxxxx","state":"MS","country":"US","phone_number":"xxxxxx"},"is_gift":"false","gift_message":"","shipping_method":"cheapest","payment_method":{"name_on_card":"xxxxxxxxxx","number":"xxxxxxxxxxxxx","security_code":"xxxx","expiration_month":"xx","expiration_year":"xxxx","use_gift":false},"billing_address":{"first_name":"Thomas","last_name":"xxxxx","address_line1":"xxxxxxx","address_line2":"","zip_code":"xxxx","city":"xxxxxx","state":"MS","country":"US","phone_number":"xxxxxx"},"retailer_credentials":{"email":"xxxxxxxxxx","password":"xxxxxxxxxx"}} 
+0

更多信息:Ive尝试了以下给我的错误: –

+0

在尝试使您的请求执行'echo json_encode($ postData,JSON_PRETTY_PRINT);'它应该有助于我们看到数据在编码过程中是如何格式化的。 – jswat

+0

我添加了回声响应。林不知道如果我正确地做嵌套数组 –

回答

0

的回声代码你应该可以使用json_encode()这将把它放入一个格式正确的数组。

在阅读了您的回复以及您从API获取的错误后,您将向其发出请求,但我认为错误与您的请求格式有关。

如果你看看他们的API文档,你会发现它期待着一系列的产品。你只发送一个,但它需要以数组格式。 这里是它期待:

"products": [ 
{ 
    "product_id": "0923568964", 
    "quantity": 1, 
    "seller_selection_criteria": [ 
    { 
     "condition_in": ["New"], 
     "international": false, 
     "handling_days_max": 5 
    } 
    ] 
} 

],

因此,要改变这种情况,你需要更新你的阵列到产品的关键:

'products' => array(array('product_id' => '0923568964', 'quantity' => '1','seller_selection_criteria' => array('condition_in' => 'New', 'international' =>'false', 'handling_days_max' => '5'))), 

如果您注意到,我在您的产品详细信息数组周围放置了一个数组声明。如果你有第二个产品,你会在第一批产品详细信息后加一个逗号,然后再添加一个。

+0

OMG谢谢!就是这样。必须将您在那里做的同样的事情添加到卖家选择标准中!谢谢!!! –

+0

太棒了!当你深入数组时,它会变得混乱。 – jswat