我建立了一个注册系统,每次提交HTML表单时都会返回某些值并更新数据库。以下是 register.php
的代码<?php
require_once "include/functions.php";
$db = new functions;
//json response array
$response = array("error" => FALSE);
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['confirm_password']) && !empty($_POST['name']) && !empty($_POST['email'])
&& !empty($_POST['password']) && !empty($_POST['confirm_password'])){
//receiving the POST parameters
$name = $db->sanitizeString($_POST['name']); //Sanitizing the string
$email = $db->sanitizeString($_POST['email']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
//check if password is equal to confirm_password to continue
if($db->passwordsMatch($password, $confirm_password)){
//check whether user exists with the same validated email
if($db->userExist($email) && filter_var($email, FILTER_VALIDATE_EMAIL)){
//user exists already
$response["error"]= TRUE;
$response["error_msg"]= "User already exists with email". $email;
echo json_encode($response);
}
else if(!($db->userExist($email)) && filter_var($email, FILTER_VALIDATE_EMAIL)){
//user does not exist
$hash= bin2hex(openssl_random_pseudo_bytes(78));
$user= $db-> storeUser($name, $email, $password, $hash);
if($user){
//new user
$response["error"]= FALSE;
$response["uid"]= $user["unique_id"];
$response["user"]["name"]= $user["name"];
$response["user"]["email"]= $user["email"];
$response["user"]["created_at"]= $user["created_at"];
$response["user"]["updated_at"]= $user["updated_at"];
$response["user"]["status"]= $user["status"];
echo json_encode($response);
$db->sendEmail($email, $hash);
}
else{
//user failed to store
$response["error"]= TRUE;
$response["error_msg"]= "Unknown error occurred in registration";
echo json_encode($response);
}
}
else{
//email address is not valid
$response["error"]= TRUE;
$response["error_msg"]= "Invalid email address ".$email;
echo json_encode($response);
}
}
//password and confirm password do not match
else{
$response["error"]= TRUE;
$response["error_msg"]= "Password and Confirm password mismatch";
echo json_encode($response);
}
}
//Some parameters may be missing in the form being submitted
else{
$response["error"]= TRUE;
$response["error_msg"]= "Required parameters are missing";
echo json_encode($response);
}
}
?>
functions.php 基本上具有 register.php 所需的所有辅助功能。 现在我想使用这个可以从Android设备调用的文件进行注册。现在制作Android部分的人要求我提供调用API所需的现成URL,并且他向我发送了一个URL的示例,其中包含用JSON对象包装的参数值。
有人可以帮助我理解他实际上对我的要求以及我真正需要提供什么。
修改
Android人员提供给我的示例网址是
http://umbria4.prosperoware.com/Api/Account/PingrequestObject= {&#34;验证&#34; {&#34;的AppId&#34;:&#34; c30605e1-4920-48ff-88d0-e5b95d9f9f27&#34;&#34;密码& #34;:&#34;布里亚&#34;&#34;用户名&#34;:&#34; alexey.marcus&#34;&#34; useWindowsAuthentication&#34;:&#34;假&#34; }}
答案 0 :(得分:0)
我们假设这是您的网址(您不会通过GET参数获取用户名和密码!):
http://www.mywebsite/api/register.php
我认为您将获得一个JSON内容作为POST请求的主体。
现在您可以简单地解析JSON并获取参数。我们假设您将获得一个类似的JSON:
{username: "Username", password:"some_kind_of_a_password"}
您可以像这样阅读POST请求的正文(这实际上取决于数据的发送方式):
$inputJSON = file_get_contents('php://input');
现在,将此JSON字符串转换为关联数组:
$registrationInput = json_decode( $inputJSON, TRUE );
因此,您现在可以在任何关联数组中访问字段:
$name = $registrationInput["username"];
$pass = $registrationInput["password"];
这有多大帮助。
答案 1 :(得分:0)
当您收到json数据时,您可以解码它并将值用于表单中的chekc:
$json = '{"authentication":{
"AppId":"c30605e1-4920-48ff-88d0-e5b95d9f9f27",
"password":"umbria",
"username":"alexey.marcus",
"useWindowsAuthentication":"false"
}
}';
$data = json_decode($json);
var_dump( $data->authentication->AppId );
var_dump( $data->authentication->password );
var_dump( $data->authentication->username );
现在将它们输入您的脚本并检查它们是否匹配
答案 2 :(得分:0)
根据您的问题=&gt;你的php部分应该监听2种类型的请求,种类:
if($_SERVER['REQUEST_METHOD'] === 'POST'){
// your code here
} elseif( $_SERVER['REQUEST_METHOD'] === 'GET' ){
try{
$authentication = json_decode($_GET['PingrequestObject']);
if(isset($authentication->name) && isset($authentication->email) && isset($authentication->password) && // etc...) ){
// make your code here
}
}catch(Exception $ex){
echo $ex->getMessage();
}
}