I need to call an API that returns JSON.
It limits to 100 records each time.
$params = "start_rec = 0, num_max = 100";
So I call GET
$jsonObject = GET($url, $params);
if the response->count > $num_max I need to
$start_rec=100
call fcn again
Not sure how to handle the recursion and the json merging the data.
Any suggestions?
Thanks very much !
public function getJson($url, $params) // $params = array()
{
return $jsonObject = Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, $auth)->get($url, $params);
}
public function actionGetData($service) // $params = array()
{
public $start_rec = 0;
public $num_max = 100;
public $rec = 0;
$params = "start_rec = 0, num_max = 100";
$jsonObject = $this->getJson($url, $params);
$obj = json_decode($jsonObject);
$status = $obj->response->status;
if ($status == "OK") {
$count = $obj->response->count; // total records in set to be returned
$start_rec = $obj->response->start_rec;
if ( $count < $num_max)
$num_max = $count;
if ( $rec < $count ) {
for ($i=$start_rec; $i<$start_rec+$num_max; $i++) {
$id = $obj->response->categories[$i]->id;
$name = $obj->response->categories[$i]->name;
}
$start_rec = $start_rec + $i;
$rec = $rec + i;
$params = "start_rec = $start_rec, num_max = $num_max";
$jsonObject = $this->getJson($url, $params);
if ( $count < $num_max)
$num_max = $count;
}
}