1
votes

I try to send request from post type to server but it not success, It return me BadRequest with status 400,

My code:

<?php

$products = array('1'=>array('amount'=>'1','product_id'=>'11250'));
$data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products);
$cert = base64_encode('[email protected]:1234abcd');
$url = "https://myapp.co.il/api/aaa";


$ch = curl_init($url);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "authorization: Basic ".$cert,
    "Content-Type: application/json",
    "cache-control: no-cache"
    ));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);

?> 

It return me so:

array (2) {["message"] => string (51) "Bad Request: Syntax error, distorted JSON" ["status"] => int (400)}

1
You sent $postdata, which isn't defined anywhere. - Nigel Ren
Well, just browsing directly to the $url value returns a 400 so why would you expect your program to receive a different response? - Drew

1 Answers

1
votes

I see that you prepared variable $payload to send, but sent $postdata instead at curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata).