Yii Framework Forum: Should I Do This In The Controller Or Create An Extension? - Yii Framework Forum

Jump to content

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

Should I Do This In The Controller Or Create An Extension? Rate Topic: -----

#1 User is offline   AustinGeek 

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

Posted 29 January 2013 - 06:57 PM

I am building a series of calls to an external API.

I have in the controller a simple function to open a connection
public function openConnection()
	{

	$jsonObject = Yii::app()->curl->post( $this->auth_uri , $this->data);
	$obj = json_decode($jsonObject);
	$ok = $obj->response->status; 
		if ($ok == "OK") {
		$token = $obj->response->token;
		return $token;		
		} else { return 'Error';}
	
	}


I also have in the controller a function to make a CURL GET call to the API


public function actionGetData()
{
	// Authorize
	$token = openConnection();
        $auth = array('Authorization:'.$token);
	$jsonObject = Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, $auth)->get($url);
        $obj = json_decode($jsonObject);
	$ok = $obj->response->status;
	if ($ok == "OK") {
		$count = $obj->response->count;			
		$start_element = $obj->response->start_element;
		$num_elements = $obj->response->num_elements;
        }
        $this->render('result', array('msg' => $ok,'token' => $token,));
}


For some strange reason the call to openConnection() fails and the system hangs.

If I put it all in one routine "actionGetData()" then it works fine.

What I am doing wrong?

Should I be able to define a private or public $var in the controller and reference it as $this->$token ?

I setup public varialbles for $auth_uri and it works fine but it is a constant...I cannot re-assign it a value.

I must be missing something really obvious.

Would I be better off making this a module or extention rather than just a controller?

Thanks !
0

#2 User is offline   AustinGeek 

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

Posted 29 January 2013 - 11:48 PM

Found the solution
made the function protected and used $this-> function...
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