1

I have the following PHP code:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            "https://10.0.0.99/api/login");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "somethingsomething");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/plain',
    'Content-Length: 18')
);

$cookie = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL,            "https://10.0.0.99/api/getcmds");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/plain',
    'Content-Length: 0')
);

$response = curl_exec($ch);
$json = json_decode($response, true);

curl_setopt($ch, CURLOPT_URL,            "https://10.0.0.99/api/cmd/" . $json["value"]);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "on");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: text/plain',
    'Content-Length: 2')
);

curl_exec($ch);

curl_close($ch);

The first call to curl_exec posts the string as expected and returns the response. Second call also works as expected.

The third call however doesn't post the string "on" to the server. It correctly gets the returned data (which, without the "on" is an error), but the server never receives the posted data.

The code itself is running on PHP 5.5.30 in the command line using the -f flag. Curl Verbose mode is showing that the Content-Length is beeing set correctly, however it doesn't show anything about posted data. The rest of the post seems to be correct, it's just that no data arrives.

(PS: This is not supposed to be production code, just something I'm playing around with, so please no comments about code quality/security.)

PiMaker
  • 511
  • 3
  • 16

2 Answers2

0

You can try closing the connection and then starting again for a new call/request. I personally do this thing.

Yash Lotan
  • 378
  • 1
  • 5
  • 21
0

From php.net docs on cURL: PHP: curl_setop

As for CURL_POSTFIELDS: This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.

In your case, you set an empty variable $_POST['on']. If you want it to have some particular value:

curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "on=value");
Reversal
  • 622
  • 5
  • 19
  • I don't want to set variables, I just wan't to directly send a raw post request. The code I'm using was found here: http://stackoverflow.com/questions/871431/raw-post-using-curl-in-php – PiMaker Mar 25 '16 at 17:42
  • Try not to set a priori `Content-Length` as in the example you linked: cURL will do it by himself. – Reversal Mar 25 '16 at 17:50