[英]How to write REST web service in PHP for Android application backend?
Currently, I am developing an Android application with involves client server architecture. 当前,我正在开发一个涉及客户端服务器架构的Android应用程序。 It is said to me that I have to write REST web service in PHP for back end communication. 有人说我必须用PHP编写REST Web服务才能进行后端通信。 At that time I didn't know about RESTful architecture and etc.. 那时我还不了解RESTful架构等。
In last 3 days, I learned much about REST
web services and tried many tutorials. 在过去的三天内,我学到了很多有关REST
Web服务的知识,并尝试了许多教程。 Then, I tried some code from tutorials and from SO. 然后,我尝试了教程和SO中的一些代码。 What I have tried so far is as follows: 到目前为止,我已经尝试了以下方法:
I have three php files, a database named xyz
and table named user_accounts
with basic user details in phpmyadmin. 我有三个php文件,一个名为xyz
的数据库和一个名为user_accounts
表, user_accounts
包含phpmyadmin中的基本用户详细信息。 And I have installed Advanced REST client on my browser. 而且我已经在浏览器上安装了高级REST客户端。 All code is in WWW
directory of WAMP server
under folder named my project
. 所有代码都位于WAMP server
WWW
目录中,该文件夹名为my project
。 So, let me show some code: 因此,让我显示一些代码:
1. db_connect.php 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();
}
In second file I have a function named adduser
for adding user records into the database : 在第二个文件中,我有一个名为adduser
的函数,用于将用户记录添加到数据库中:
index.php : 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);
}
?>
When I test it using advanced REST client by giving url : 当我通过提供url使用高级REST客户端对其进行测试时:
http://127.0.0.1/my project/index.php/adduser
and method POST
and parameters: 以及方法POST
和参数:
firstname=somename&lastname=name&email=a@b.gmail.com&password=101010
It shows following response: 它显示以下响应:
{"success":0,"message":"An error occurred during registration."}
I can not identify where the error is. 我无法确定错误在哪里。 I am new to this. 我是新来的。 Please help if anything wrong. 如果有任何问题,请提供帮助。
your should try this: 您应该尝试这样:
$affected_rows = $stmt->rowCount();
then check if row count is greater than 0. 然后检查行数是否大于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.