2

I'm kinda struggling with yii framework. I would like to connect to mobile app from my php server. How should I do the Json encoding and sent to mobile app in yii framework?

jh314
  • 24,533
  • 14
  • 58
  • 79
imcrazy
  • 67
  • 1
  • 8

3 Answers3

0

The Yii way to do it is

echo CJSON::encode($myArray);

The underlying code use json_encode, but there are advantages discussed here : Why use CJSON encode when we have json_encode

Community
  • 1
  • 1
crafter
  • 5,878
  • 1
  • 31
  • 42
0

Im using this:

 public function actionGetUser($user_id)
        {   
            header('Content-type: application/json');
            $user = User::model()->findAll();
            echo CJSON::encode($user);
            Yii::app()->end();
     }   

this does work when i test: localhost/myproject/index.php/user/getuser/user_id

Is it possible for json to just encode just one attribute and send to mobile app? Example: there are the attributes like: user_id, user_name, user_email, user_photo

If I want to pass in just user_name to mobile app, is it possible? How should I do that?

4EACH
  • 1,540
  • 3
  • 14
  • 24
imcrazy
  • 67
  • 1
  • 8
0

Q: How should I do the Json encoding and sent to mobile app in yii framework?

A:

echo CJSON::encode($myArray);

Q: If I want to pass in just user_name to mobile app, is it possible? How should I do that?

A:

$criteria=new CDbCriteria;
$criteria->select='user_name';  // only select the 'user_name' column
$criteria->condition='user_id=:user_id';
$user_id = filter_input(INPUT_GET,'user_id');
if(FALSE===$user_id){
   header($_SERVER['SERVER_PROTOCOL'].' 404 not found');
   return false;
}

$criteria->params=array(':user_id'=>$user_id); // check 
$user=Users::model()->find($criteria); // $params is not needed

//echo json as written at answer #1

Good luck!

4EACH
  • 1,540
  • 3
  • 14
  • 24