2017-05-11 19 views
1

我在postman中使用POST將JSON消息發送到了我的web服務器。但是回顯只顯示GET並且沒有消息。我認爲回顯應顯示POST不GET。下面的代碼正在工作,但格式不是JSON。請看下面的圖片:使用php(REST API)在web服務器中進行錯誤的通信

<html> 
<head></head> 
<body> 
    <?php 
    $method = $_SERVER['REQUEST_METHOD']; 
    $url = 'http://115.145.163.244:5851/TEST52.php'; 
    echo $method; 
    echo "<br />\n"; 

    switch ($method) { 
     case 'POST': 
      $input = json_decode(file_get_contents('php://input'), true); 
     break; 
     case 'GET': 
      $input = json_decode(file_get_contents('php://input'), true); 
     break; 
     case 'PUT': 
      $input = json_decode(file_get_contents('php://input'), true); 
     break; 
     case 'DELETE': 
      $input = json_decode(file_get_contents('php://input'), true); 
     break; 
    } 
    echo ($input->data->ContextID); //it does not work 
    print_r($input); //it does work. But not in JSON format 
    ?>  
</body> 

enter image description here

回答

0

json_decode()轉換成JSON數組。因此,如果您想將JSON值保存到變量$input中,請改爲使用json_encode(),如果它已經在JSON中,則根本不需要解析它。