如何使用JSON类型头连接PHP

如何使用JSON类型头连接PHP,php,android,json,api,Php,Android,Json,Api,这是我的PHP代码 <?php require_once '../includes/DbOperations.php'; $response = array(); if($_SERVER['REQUEST_METHOD']=='POST'){ if(isset($_POST['username']) and isset($_POST['password']) and isset($_POST['email'])) { //operate the data

这是我的PHP代码

<?php require_once '../includes/DbOperations.php'; $response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){
    if(isset($_POST['username']) and isset($_POST['password']) and isset($_POST['email']))
    {
        //operate the data further 

        $db = new DbOperations(); 

        $result = $db->createUser($_POST['username'],$_POST['password'],$_POST['email']);
        if($result == 1){
            $response['error'] = false; 
            $response['message'] = "User registered successfully";
        }elseif($result == 2){
            $response['error'] = true; 
            $response['message'] = "Some error occurred please try again";          
        }elseif($result == 0){
            $response['error'] = true; 
            $response['message'] = "It seems you are already registered, please choose a different email and username";                     
        }

    }else{
        $response['error'] = true; 
        $response['message'] = "Required fields are missing";
    }
}else{
    $response['error'] = true; 
    $response['message'] = "Invalid Request";
}

echo json_encode($response);
?>
结果

{"error":true,"message":"Required fields are missing"}

您的服务器代码需要定期提交
应用程序/x-www-form-urlencoded
值(也称为定期提交后表单),但您的客户端正在
应用程序/json
(json对象)中对数据进行编码。应该是哪一个?客户机和服务器必须相互同意

如果您想使用JSON作为数据编码标准,您的代码应该如下所示:


请写下您的发送代码snippets@ASHKARAN我更新了我的帖子,我使用的代码被剪掉了。我从你的答案中学到了一些东西。非常感谢,但是当我使用你的代码时,它在第10行中指出了一些错误,你能帮我解决遗漏的问题吗
致命错误:未捕获错误:无法在C:\AllMyFiles\Features\Database\Program\Xampp\htdocs\Android\v1\registerUser.php中使用stdClass类型的对象作为数组:10堆栈跟踪:#0{main}在第10行的C:\AllMyFiles\featured\Database\Program\Xampp\htdocs\Android\v1\registerUser.php中抛出
我忘记了json_decode默认为您提供对象。因此,无法使用
$input['key']
检索结果
$input
值。我已经将json_decode$assoc标志更改为TRUE,现在一切都应该正常了。或者,您可以通过
$input->key
获得某个键。这两个都可以。谢谢,现在可以了,JSON对象需要使用$input->key,如果我将JSON对象转换为数组,我需要使用$input['key'],谢谢你的时间:D
{"error":true,"message":"Required fields are missing"}