2

I want to add a comment to a magento order. When i try to do this with postman it works properly:

Postman post

But when i do this with my php code, the request return an error:

//GET TOKEN
$url = '***/rest/V1/integration/admin/token';

$datos_usuario = array("username" => "****", "password" => "*****");

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datos_usuario);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);
curl_close($curl);

date_default_timezone_set('Europe/Madrid');
setlocale(LC_TIME, 'es_ES.UTF-8');
$DateTime = new DateTime();
$fecha = date("Y-m-d")." ".date("H:i:s");

$json = json_decode($result, true);

        $url = '****/rest/V1/orders/430/comments';
    
        $data = [
            "statusHistory" => [
                "comment" => "Pedido entregado al cliente",
                "created_at" => $fecha,
                "parent_id" => "", 
                "is_customer_notified" => 0, 
                "parent_id" => 430,
                "is_customer_notified" => 0,
                "is_visible_on_front" => 1, 
                "status" => "complete"
            ]
        ];

        $data_string = json_encode($data);
        $authorization = "Authorization: Bearer ".$json;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($session, CURLOPT_POSTFIELDS, $data_string);
        $result2 = curl_exec($curl);
        
        $result = curl_exec($session);
        curl_close($session);
        $json2 = json_decode($result);

        var_dump($json2);
}

The error:

"message": "% fieldName \ is required." Enter it and try again. "
 "parameters": {
     "fieldName": "statusHistory"
 }

1 Answer 1

1

Variable/Type issue at your code;

Changes

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_POSTFIELDS, $data_string);
$result2 = curl_exec($curl);

$result = curl_exec($session);
curl_close($session);

To

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$result2 = curl_exec($curl);

$result = curl_exec($curl);
curl_close($curl);
1
  • Ohhhhhhh thank you so much!!!!! I was going crazy. I'm so stupid ... hahahaha Thanks again Jul 15, 2020 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.