Reputation: 157
I am trying to populate my website with products using the API of chinavasion.com. I've successfully fetch a list of products under a certain category but the response JSON only gives 10 products and a pagination which I literally don't know how to use, I'm guessing that it might be the one limiting the list of products echo'd?
Here is the sample JSON response:
{
"products": [
{
"product_id": 19433,
"model_code": "CVABR-C405",
"short_product_name": "13.3 Inch Roof Mounted Car Monitor ",
"product_url": "http://www.chinavasion.com/china/wholesale/Car_Video/Roof_Monitors/13.3-Inch-Roof-Mounted-Car-Monitor/",
"category_name": "Car Video",
"category_url": "http://www.chinavasion.com/china/wholesale/Car_Video/",
"subcategory_name": "Roof Monitors",
"status": "In Stock"
},
.... and 9 more.
"pagination": {
"start": 0,
"count": 10,
"total": 53
}
}
And here is my PHP so far, I just want to echo all the short product name of all the item, thing is I only get 10 items but there is a total of 53 items. (which can be seen on the sample JSON response pagination total)
<?php
$API_KEY = '4rxVx5-bxo7ldVQ5GcPSmX8XeqcSZoTnJnxF7xhRr8g.';
$url = "https://secure.chinavasion.com/api/getProductList.php";
$data = array(
'key' => $API_KEY,
'categories' => array('Car Video')
);
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
foreach($response['products'] as $res)
{
echo $res['short_product_name'],'<br />';
}
?>
So is there a way to fetch the other remaining 43 products at this point? I am not pretty sure if its possible or not as I am really pretty new to programming and haven't done JSON before, hope you guys can help me.
Upvotes: 1
Views: 1612
Reputation: 1733
That pagination array is there to indicate that there are more items. You just got to pass a start
variable to get the next 10. Think of it as an offset in SQL.
Upvotes: 4
Reputation: 3396
next 10 items:
$API_KEY = '4rxVx5-bxo7ldVQ5GcPSmX8XeqcSZoTnJnxF7xhRr8g.';
$url = "https://secure.chinavasion.com/api/getProductList.php";
$data = array(
'key' => $API_KEY,
'categories' => array('Car Video'),
'pagination' => array('start' => 10)
);
$content = json_encode($data);
changing the 'pagination' => 'start' parameter you can set the starting item index.
EDIT: you must insert the 'start' parameter inside the 'pagination' array. An alternative way is to set the 'count' parameter to a big number. This way,you'll receive all items on a single call
$API_KEY = '4rxVx5-bxo7ldVQ5GcPSmX8XeqcSZoTnJnxF7xhRr8g.';
$url = "https://secure.chinavasion.com/api/getProductList.php";
$data = array(
'key' => $API_KEY,
'categories' => array('Car Video'),
'pagination' => array('count' => 999)
);
Upvotes: 2