2017-08-03 97 views
-1

我嘗試使用JSON在rest-API中創建一個用戶。這就是成功。如何在PHP中解析POST請求並輸出JSON?

我的問題:

1)I需要接收從包含用戶信息的網絡表單POST請求?

2)添加一個APIkey並插入變量。

3)生成JSON發送到頁面:

rest.domain.com/api/v1/UserCreate

POST請求而從MY WEBFORM(實施例)

 { 
    "UserID":"11", 
    "GroupID":"22", 
    "MemberID":"97865454534231", 
    "UserFullName":"Mr Test", 
    "UserEmail":"[email protected]", 
    "UserRegistrationNumber":"9999999", 
     } 

JSON致電API(工作json)

POST /api/v1/UserCreate HTTP/1.1 
    Host: rest.domain.com 
    APIKey: XXXXXXXXXX-XXXXXXXXX-XXXXXXXXXXX 
    Content-Type: application/json 

    { 
    "APIKey":"XXXXXXXXXX-XXXXXXXXX-XXXXXXXXXXX", 
    "UserID":"11", 
    "GroupID":"22", 
    "UserEmail" : "[email protected]", 
    "UserRegistrationNumber" : "9999999", 
    "UserFullName" : "Mr Test", 
    "MemberID" : "123456789", 
    } 

回答

-1
  1. 創建一個php頁面,用於監聽預定義變量 $_POST中的帖子。

  2. 基於接收的數據構建JSON

    的字符串
  3. 調整報頭的內容類型和echoJSON串 你內置

實施例:

// CHECK POST FIELDS 
if(@$_POST['UserID'] != ''){ 

    // GENERATE API KEY 
    $apiKey = '' 

    // BUILD RESPONSE 
    $jsonResponse =  '{'; 
    $jsonResponse .=  '"APIKey":"'.$apiKey.'",'; 
    $jsonResponse .=  '"UserID":"'.$_POST['UserID'].'",'; 
    $jsonResponse .=  '"GroupID":"'.$_POST['GroupID'].'",'; 
    $jsonResponse .=  '"MemberID":"'.$_POST['MemberID'].'",'; 
    $jsonResponse .=  '"UserFullName":"'.$_POST['UserFullName'].'",'; 
    $jsonResponse .=  '"UserEmail":"'.$_POST['UserEmail'].'",'; 
    $jsonResponse .=  '"UserRegistrationNumber":"'.$_POST['UserRegistrationNumber'].'",'; 
    $jsonResponse .= '}'; 

    // SET HEADER 
    header('Content-Type: application/json'); 

    // ECHO RESPONSE 
    echo $jsonResponse; 

}