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
  • 27,144
  • 16
  • 62
  • 82
imcrazy
  • 77
  • 2
  • 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
  • 6,246
  • 1
  • 34
  • 46
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
  • 2,132
  • 4
  • 20
  • 28
imcrazy
  • 77
  • 2
  • 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
  • 2,132
  • 4
  • 20
  • 28