Yii Framework Forum: How To Deal With Recursive Calls To Api And Json Data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Deal With Recursive Calls To Api And Json Data Rate Topic: -----

#1 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 02 February 2013 - 12:47 PM

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

0

#2 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 03 February 2013 - 03:49 PM

I have many services (api's) that I need to call.
Not sure how to make a generic function to deal with the json arrays.
Each JSON has it's own structure, so I can reference array elements by name
using as example
$id = $obj->response->category[$i]->id;

Could I create a model for each (service) JSON?

Can I map a json (or a model of it) into a CDataProvider?

What are the downsides of trying to get several hundred records vs. doing a fetch of just some?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users