使用cURL向API发送JSON请求(Sending a JSON Request to API using cURL)

我见过人们在数组中发送JSON数据的例子。 使用cURL发送数据。 问题是大多数都是这样的

$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的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"}}

I have seen examples of people sending JSON data in an array. An using cURL to send the data. Problem is most of them are something like

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

My question is, if I have something like below that isnt as dry cut. Example:

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

],

So how would I put that info into an Array?

More Info: Ive tried the following with no luck

<?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; ?>

There error im getting is:

{"_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"}

Here is the code from the Echo of 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"}}

最满意答案

您应该能够使用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'))),

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

You should be able to use json_encode() which will put it into a properly formatted array.

After reading your response and the error you were getting back from the API you are making the request to, I think the error has to do with the format of your request.

If you look at their API documentation you'll see that it's expecting an array of products. You are only sending one, but it will need to be in an array format. Here is what it is expecting:

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

],

So, to change this, you'll need to update the products key of your array to:

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

If you noticed, I put an array declaration around the array of your product details. If you had a second product, you'd put a comma after your first array of product details and add a second.

更多推荐