使用PHP发布数据时出现JSON错误

时间:2016-08-29 09:32:27

标签: php json curl

以下是我所做的编码。但在发布数据后,我收到的错误是:

'message' => string ''from' and 'to' date must be given' (length=34).

以下是我的代码:

$auth_token=$_REQUEST['auth_token'];
$ch = curl_init('https://api.datonis.io/api/v3/datonis_query/thing_aggregated_data');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,

CURLOPT_HTTPHEADER => array(
        'X-Auth-Token:'.$auth_token,
        'thing_key:6f2159f998',
        'idle_time_required:true',
        'from:2016/02/02 17:05:00',
        'to:2016/08/29 17:10:00',
        'time_zone:Asia/Calcutta',
        'Content-Type:application/json',
),


));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    die(curl_error($ch));
}

// Decode the response
//var_dump($response);
$responseData = json_decode($response, TRUE);

// Print the date from the response
var_dump($responseData);

实际上我也想获取数据,但详细信息包含在发布请求数据中。

1 个答案:

答案 0 :(得分:0)

使用“CURLOPT_POSTFIELDS”:

$data = array("from" => "2016/02/02 17:05:00", "to" => "2016/08/29 17:10:00");                                                                    
$data_string = json_encode($data);                                                                                   

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json')