2017-09-04 135 views
0

IAM尝试发布以下JSON数据广东话后的数据在PHP

{"name":"somename","email":"somemail","password":"abcdef"} 
在我的API的身体

<?php 


require_once 'include/DB_Functions.php'; 
$db = new DB_Functions(); 

// json response array 
$response = array("error" => FALSE); 

if (isset($_POST['name']) && isset($_POST['email']) && 
isset($_POST['password'])) { 

// receiving the post params 
$name = $_POST['name']; 
$email = $_POST['email']; 
$password = $_POST['password']; 

// check if user is already existed with the same email 
if ($db->isUserExisted($email)) { 
    // user already existed 
    $response["error"] = TRUE; 
    $response["error_msg"] = "User already existed with " . $email; 
    echo json_encode($response); 
} else { 
    // create a new user 
    $user = $db->storeUser($name, $email, $password); 
    if ($user) { 
     // user stored successfully 
     $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"]; 
     echo json_encode($response); 
    } else { 
     // user failed to store 
     $response["error"] = TRUE; 
     $response["error_msg"] = "Unknown error occurred in registration!"; 
     echo json_encode($response); 
    } 
} 
} else { 
$response["error"] = TRUE; 
$response["error_msg"] = "Required parameters (name, email or password) is 
missing!"; 
echo json_encode($response); 
} 
?> 

我的问题是,if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']))始终是假的,消息Required parameters (name, email or password) is missing! is shown

在wampserver日志,误差给定为

PHP已弃用:自动填充$ HTTP_RAW_POST_DATA为 弃用,将在未来的版本中删除。为避免这种情况,警告在php.ini中将'always_populate_raw_post_data'设置为'-1',并改为使用 php://输入流。在在线0

+1

'“password”,“abcdef”'应该像其他人一样冒号,* n'est-ce pas * *编辑:此评论按照原帖https://stackoverflow.com/revisions/ 46042691/1 –

+0

是的,那是一个类型错误 –

+0

这是哪里的HTML /表单? –

回答

1

注意:这是一个社区维基答案。

按照说明书上$HTTP_RAW_POST_DATA

http://php.net/manual/en/reserved.variables.httprawpostdata.php

“一般来说,PHP://输入应该被用来代替$ HTTP_RAW_POST_DATA。”。

正如评论也注意到OP:

@弗雷德-II-感谢,它的工作,加入这样的$ POSTDATA =的file_get_contents( “PHP://输入”);

1

未知如果发送后的数据作为JSON,在API必须输入数据进行解码,以阵列

$data = json_decode($_POST)

,然后后继续与条件

if (isset($data['name'], $data['email'], $data['password']) {