skip to Main Content
curl -X 'POST' 
  'https://www.test.com/dosomething 
  -H 'accept: application/json' 
  -H 'Authorization: Basic value_for_basic_authentication 
  -H 'Content-Type: application/json' 
  -d '{
  "type": "limit",
  "quantity": "10",
  "price": „1“,
  "strictValidate": false
}'

This is the command which is working fine on command line

This should be now the php script snippet where I want to send the same request in php and curl. From Verbose mode I see, that the post data is never sent. I know the part with the lines can be done easier

            
   $line0 =  "type: limit";
   $line1 =  "quantity: 10";
   $line2 =  "price: 1";
   $line3 =  "strictValidate: false }" ;




  $params = array(
                        $line0,
                        $line1,
                        $line2,
                        $line3
                         );



    $headers = array(
        "Accept: application/json",
        $auth
        );
    
    $paramsJSON = json_encode ( $params );
    
    
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_VERBOSE, true);
    //curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $paramsJSON );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );    
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    
    $res = curl_exec($ch);

Here some part of the log output where is visible that post data is not sent
Logfile from Server

There the request is working

  • ALPN, server accepted to use http/1.1

POST /api/v2/createorder HTTP/1.1
Host: www.test.com/dosomething
User-Agent: curl/7.47.0
accept: application/json
Authorization: Basic MmEwN2MxZTBjZDY5ZmQ3NGEyYTVmNzFiMjI0MDdkN2M6MjQ5N2IzMGU3MTRhYjNmODg1MTZiMWExODRkYWViY2JhMmU1OWQ4MmVlZjI1Nzg5
Content-Type: application/json
Content-Length: 154

  • upload completely sent off: 154 out of 154 bytes

With my PHP Script request is not working

  • ALPN, server accepted to use http/1.1

POST /api/v2/createorder HTTP/1.1
Host: www.test.com/dosomething
Accept: application/json
Authorization: Basic MmEwN2MxZTBjZDY5ZmQ3NGEyYTVmNzFiMjI0MDdkN2M6MjQ5N2IzMGU3MTRhYjNmODg1MTZiMWExODRkYWViY2JhMmU1OWQ4MmVlZjI1Nzg5
Content-Length: 169
Content-Type: application/x-www-form-urlencoded

  • upload completely sent off: 169 out of 169 bytes

Difference is Content-Type: application/json -> Content-Type: application/x-www-form-urlencoded
no idea how to change it it "Content-Type: application/json" in my PHP code

2

Answers


  1. Chosen as BEST ANSWER

    The last answer solved the issue!!!

    $params = [
    "type" => "limit",
    "quantity" => "10",
    "price" => "1",
    "strictValidate" => "false"
    ];
    

    plus json_decode $params

    solved the issue :-)

    Many thanks to all who answered


  2. To produce a JSON object, $params should be an associative array, not an array of key: value strings.

    $params = [
      "type" => "limit",
      "quantity" => "10",
      "price" => "1",
      "strictValidate" => "false"
    ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search