POST请求JSON

时间:2016-12-13 09:42:23

标签: php jquery json ajax

我有这个ajax脚本,但我想将其转换为PHP。

这是我的Json身体:

{
"DateTime": "2016-10-25T11:09:5702:00", 
"AlarmType": "Rmn", 
"DespatchId": Uniquid from alarm center, 
"Latitude": 2.1,
"Longitude": 3.1
}

Ajax jQuery

$.ajax({url: '/ api/Alarm/AlarmInLocation,
type: 'post',
datatype: 'json',
data: Payload,
beforeSend: function(request)
{   request.setRequestHeader("Authorization", "Bearer " + accessToken);
                         },
 success: function(response) {
          var d = JSON.stringify(response);
} });

我试过这种方式,但没有用,它回应了DIE:

// The data to send to the API
    $postData = array('DateTime' => '2016-10-25T11:09:5702:00',
                      'AlarmType' => 'Rmn',
                      'DespatchId' => '1234',
                      'Latitude' => '2.1',
                      'Longitude' => '3.1'
                     );

                    // Create the context for the request
                    $context_alarm = stream_context_create(array(
                    'http' => array(
                    'method' => 'POST',
                    'header' => "Authorization: {$authToken}\r\n".
                    "Content-Type: application/json\r\n"
                    'content' => json_encode($postData)
                    )
                    ));

    $response_alarm = file_get_contents('https://myapi.com/api/Alarm/AlarmInLocation', false, $context_alarm); 

    // Check for errors

    if($response_alarm === FALSE){
                            die('Error Get Response Alarm');
                            }

0 个答案:

没有答案