I am using laravel as a server API backEnd but when clients send request using cURL I got the following json decode exception
JSON_ERROR_SYNTAX
here is the code of client
$data = [ "items"=>array(
"order_reference"=> "123412",
"mode" =>"payment",
"products" => [array(
"id"=> 1,
"product_name"=> "product 1",
"quantity"=>1,
"unit_amount"=> 100)
]
, "currency"=>"YER",
"total_amount"=>1500,
"success_url"=> "https://company.com/success",
"cancel_url"=> "https://company.com/cancel",
"metadata"=>[
"Customer name"=> "somename",
"order id"=> 0 ]
)];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/api/order",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"private-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et",
"public-key: HGvTMLDssJghr9tlN9gr4DVYt0qyBy",
),
));
$response = curl_exec($curl);
and this is the code of API backEnd
json_decode($request->items,true);
and the result of the code is
JSON_ERROR_SYNTAX
CodePudding user response:
It should be json_decode($response->items,true);
CodePudding user response:
Add this header
Content-Type: application/json
to your client code in this manner:
CURLOPT_HTTPHEADER => array(
"private-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et",
"public-key: HGvTMLDssJghr9tlN9gr4DVYt0qyBy",
"Content-Type: application/json"
)
then laravel will automatically decode in the backend and you don't need to use json_decode. You can simply use $request->items to access to the client's items.