簡體   English   中英

如何在Android應用程序后端的PHP中編寫REST Web服務?

[英]How to write REST web service in PHP for Android application backend?

當前,我正在開發一個涉及客戶端服務器架構的Android應用程序。 有人說我必須用PHP編寫REST Web服務才能進行后端通信。 那時我還不了解RESTful架構等。

在過去的三天內,我學到了很多有關REST Web服務的知識,並嘗試了許多教程。 然后,我嘗試了教程和SO中的一些代碼。 到目前為止,我已經嘗試了以下方法:

我有三個php文件,一個名為xyz的數據庫和一個名為user_accounts表, user_accounts包含phpmyadmin中的基本用戶詳細信息。 而且我已經在瀏覽器上安裝了高級REST客戶端。 所有代碼都位於WAMP server WWW目錄中,該文件夾名為my project 因此,讓我顯示一些代碼:

1. db_connect.php

 <?php

            define("SERVER", '127.0.0.1');
            define("USER", 'root');
            define("PASSWORD", '');
            define("DB", 'xyz');


            $con = new mysqli(SERVER,USER,PASSWORD,DB);

            if ($con->connect_errno){
                die("Database Connection Failed");
                exit();

            }

在第二個文件中,我有一個名為adduser的函數,用於將用戶記錄添加到數據庫中:
index.php:

    <?php
            require_once('db_connect.php');

           $response = array();

           $result = "";


           function adduser($firstname, $lastname, $email, $password) {


            global $app;

            $req = $app->request();

            $firstname= $req->params['firstname'];

            $lastname= $req->params['lastname'];

            $email = $req->params['email'];

             $password = $req->params['password'];


    $stmt = $con->prepare("INSERT INTO user_accounts (first_name,last_name,email,password)VALUES (?,?,?,?)");

                        $stmt->bind_param('ssss', $firstname, $lastname, $email, $password); 

                        $stmt->execute();

                        $result = $stmt->close();


        }

                if($result){

                          $response["success"] = 1;

                          $response["message"] = "account successfully created.";

                          echo json_encode($response);

                }

                else{

                     $response["success"] = 0;

                     $response["message"] = "An error occurred during registration.";

                     echo json_encode($response);
                 }



            ?>    

當我通過提供url使用高級REST客戶端對其進行測試時:

http://127.0.0.1/my project/index.php/adduser

以及方法POST和參數:

firstname=somename&lastname=name&email=a@b.gmail.com&password=101010

它顯示以下響應:

{"success":0,"message":"An error occurred during registration."}

我無法確定錯誤在哪里。 我是新來的。 如果有任何問題,請提供幫助。

您應該嘗試這樣:

$affected_rows = $stmt->rowCount();

更新:

然后檢查行數是否大於0。

<?php
        require_once('db_connect.php');

       $response = array();

       $result = "";


function adduser($firstname, $lastname, $email, $password) {
    global $app;
    $req = $app->request();
    $firstname= $req->params['firstname'];
    $lastname= $req->params['lastname'];
    $email = $req->params['email'];
    $password = $req->params['password'];

    $stmt = $con->prepare("INSERT INTO user_accounts (first_name,last_name,email,password)VALUES (?,?,?,?)");

    $stmt->bind_param('ssss', $firstname, $lastname, $email, $password); 
    $stmt->execute();

    return $stmt->rowCount();
}

$adduser = addUser($firstname, $lastname, $email, $password);

if($adduser > 0){
    $response["success"] = 1;
    $response["message"] = "account successfully created.";

    echo json_encode($response);
} else{
    $response["success"] = 0;
    $response["message"] = "An error occurred during registration.";

    echo json_encode($response);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM