2017-09-04 138 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']) {