yii中移动应用程序的Web服务

时间:2016-08-11 04:01:39

标签: php web-services yii

在我的Yii Web应用程序中,我想为移动应用程序编写一个Web服务。我使用GET方法创建了一个访问学生详细信息的URL。我想将GET方法改为POST方法或PUT方法。我的网址是,

url/index.php/user/login/studentdetails/?username=demo

我从客户端获取此用户名,并以json格式从服务器端向客户端提供响应。 我的代码是,

 public function actionStudentdetails() {

        if (isset($_GET['username'])) {
            $user = Users::model()->findByAttributes(array('username' => $_GET['username']));
            $usertypeid = $user->usertypeid;
            if ($usertypeid === '1') {
                $studentid = $user->userid;
            } else if ($usertypeid === '3') {
                $guardianid = $user->userid;
                $studentid = $_GET['studentid'];
            } else {
                $employeemasterid = $user->userid;
            }
            $student = Student::model()->findByPk($studentid);
            header('Content-type: application/json');
            $response["student_admissionno"] = $student->student_admissionno;
            $response["student_firstname"] = $student->student_firstname;
            $response["student_middlename"] = $student->student_middlename;
            $response["student_lastname"] = $student->student_lastname;
            $response["student_admissiondate"] = $student->student_admissiondate;
            $response["student_dob"] = $student->student_dob;
            $response["student_gender"] = $student->student_gender;
            $response["student_religion"] = $student->student_religion;
            $response["student_caste"] = $student->student_caste;
            $response["student_address1"] = $student->student_address1;
            $response["student_address2"] = $student->student_address2;
            $response["student_city"] = $student->student_city;
            $response["student_state"] = $student->student_state;

            $response["success"] = 1;
            $response["message"] = "success";

            echo json_encode($response);
        }
    }
}

请帮帮我。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码进行webserivce。您可以在服务器端使用任何方法GET或POST方法进行数据发布。

<强> StudentController.php

public function actionStudentdetails() {
    $json_data = array();
    $params = isset($_REQUEST) ? $_REQUEST: "";
    if (!empty($params)) {
        $username = $params['username'];
        if($username == "") {
            $json_data['success'] = false;
            $json_data['message'] = 'Username are required.';
        } else {
            $user = Users::model()->findByAttributes(array('username' => $params['username']));
            $usertypeid = $user->usertypeid;
            if ($usertypeid === '1') {
                $studentid = $user->userid;
            } else if ($usertypeid === '3') {
                $guardianid = $user->userid;
                $studentid = $_GET['studentid'];
            } else {
                $employeemasterid = $user->userid;
            }
            $student = Student::model()->findByPk($studentid);
            header('Content-type: application/json');
            $json_data["student_admissionno"] = $student->student_admissionno;
            $json_data["student_firstname"] = $student->student_firstname;
            $json_data["student_middlename"] = $student->student_middlename;
            $json_data["student_lastname"] = $student->student_lastname;
            $json_data["student_admissiondate"] = $student->student_admissiondate;
            $json_data["student_dob"] = $student->student_dob;
            $json_data["student_gender"] = $student->student_gender;
            $json_data["student_religion"] = $student->student_religion;
            $json_data["student_caste"] = $student->student_caste;
            $json_data["student_address1"] = $student->student_address1;
            $json_data["student_address2"] = $student->student_address2;
            $json_data["student_city"] = $student->student_city;
            $json_data["student_state"] = $student->student_state;

            $json_data['success'] = true;
            $json_data['message'] = "Data found Successful.";
        }
    } else {
      $json_data['success'] = false;
      $json_data['message'] = "Please try again.";
    }
    echo json_encode($response);
}