jeudi 13 août 2015

How to decode pagination in JSON?

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://ift.tt/1IM4Oio",
   "category_name": "Car Video",
   "category_url": "http://ift.tt/1DRy0FT",
   "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 = "http://ift.tt/1IM4PD5";
$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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire