murilo
murilo

Reputation: 11

problem sending POST request through curl

I have a problem in the code in which the API is not receiving the command for set_system_time, I don't know if it could be an error in the way of passing the parameters, follow the code used and the api documentation, in case anyone can help me in this case.

date.php (connects to the api, takes the session token and uses it for the second request)

<?php
$cookie="cookie.txt";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://192.168.5.23/login.fcgi');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"login\": \"admin\", \"password\": \"senha\"}");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 

$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
$session1 = json_decode($result);
$sessao = $session1->{'session'};

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
//curl_close($ch);

///////////////
echo $sessao;
$ch1 = curl_init();

curl_setopt($ch1, CURLOPT_URL, "http://192.168.5.23/set_system_time.fcgi?session=$sessao");

curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, "{ \"day\": \"1\", \"month\": \"2\", \"year\": \"2020\", \"hour\": \"21\", \"minute\": \"20\", \"second\": \"22\" }");
curl_setopt($ch1, CURLOPT_TIMEOUT, 60);
curl_setopt($ch1, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie); 

curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);

//$result = curl_exec($ch1);

curl_close($ch1);



?>

as the api shows that the information has to be passed, their example is in js, but I'm trying to do it using curl from PHP

$.ajax({
    url: "/set_system_time.fcgi?session=" + session,
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
        day: 10,
        month: 12,
        year: 1983,
        hour: 21,
        minute: 30,
        second: 00
    })
});

I made the same request, taking the token and executing another api command, but this request didn't need to pass parameters, maybe the problem is in this part but I couldn't identify it.

Upvotes: 0

Views: 487

Answers (1)

Misunderstood
Misunderstood

Reputation: 5661

Did you mean to include the API documentation?

This is an invalid JSON string.

$jsn = '{ \"day\": \"1\", \"month\": \"2\", \"year\": \"2020\", \"hour\": \"21\", \"minute\": \"20\", \"second\": \"22\" }';

I tested the above $jsn string by converting it to an array:

$array = json_decode($jsn);
var_export($array);
echo "\n";
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            echo ' - Unknown error';
        break;
    }

The above returns

NULL 
- Syntax error, malformed JSON

Whereas this string works:

$jsn = '{ "day": "1", "month": "2", "year": "2020", "hour": "21", "minute": "20", "second": "22" }';

I would never build JSON manually.

$jsn = json_encode(array (
  'day' => '1',
  'month' => '2',
  'year' => '2020',
  'hour' => '21',
  'minute' => '20',
  'second' => '22',)
);
echo $jsn;

The above returns:{"day":"1","month":"2","year":"2020","hour":"21","minute":"20","second":"22"}

Upvotes: 1

Related Questions