1

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('myapp@myapp.co.il: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)}

zinger
  • 11
  • 2
  • 2
    You sent `$postdata`, which isn't defined anywhere. – Nigel Ren Sep 23 '20 at 19:22
  • 1
    Well, just browsing directly to the $url value returns a 400 so why would you expect your program to receive a different response? – Drew Sep 23 '20 at 19:22
  • Perhaps this might be of interesst https://stackoverflow.com/questions/6213509/send-json-post-using-php – IronMan Sep 23 '20 at 19:24

1 Answers1

1

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