2

I am new to using cURL for POST request to the server, but the server always show the content-length is -1, my code is below:

$data = array(
    'data'  =>  'Testing data',
    'name'  =>  'Testing',
    'no'    =>  '1234'
);
foreach($data as $key=>$value) { $data_string .= $key.'='.$value.'&'; }
$data_string = trim($data_string, '&'); 
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, Yii::$app->request->post('url'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Length: ' . strlen($data_string)]); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Why it's content-length always show -1, thanks~

Nulra
  • 547
  • 5
  • 19
  • @JYoThI thanks for your reply, var_dump($data_string) result is string(38) "data=Testing data&name=Testing&no=1234" – Nulra Jan 18 '19 at 06:43
  • Where do you add the body to your post request? You only add $data to the URL, the way it is done for a get request – Ferrybig Jan 18 '19 at 07:44
  • Also note that a content-type header is required for most servers when you execute a post request – Ferrybig Jan 18 '19 at 07:46
  • @Ferrybig i have updated it using POSTFIELDS, but still no luck – Nulra Jan 18 '19 at 08:06

2 Answers2

0

Updated

Please run this updated code and post the results:

$postData = array(
'data' => Yii::$app->request->post('data'),
'mac' => Yii::$app->request->post('mac'),
'ksn' => '1'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
curl_setopt($ch,CURLOPT_URL, Yii::$app->request->post('url'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); //Add this line so we can grab the headers that were sent with your request.
curl_setopt($ch, CURLOPT_FAILONERROR, 1); //Helps with http errors.
curl_setopt($ch, CURLOPT_VERBOSE, 1); //This shows the response headers in the response.
curl_setopt($ch, CURLOPT_HEADER, 1); //Oppps
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

if(curl_exec($ch) === false){

echo 'There was an error with your request.<br>';
echo 'Here are the curl errors:<br>';
echo curl_error($ch) . '<br><br>';

}else{

  echo 'There were no errors with the request.<br>';
  $result = curl_exec($ch);
  echo $result;

}

//Get your sent headers:
$info = curl_getinfo($ch);
echo '<pre>';
echo 'Here are the headers that you sent:<br>';
print_r($info);
echo '</pre>';

curl_close($ch);

return $result;
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
  • print_r($info['request_header']) is POST /xxxxx/xxxxx/xxxxxxx?data=Testing data&name=Testing&no=1234 HTTP/1.1 Host: xxxxxxx.xxxxxxx.com.xx Accept: */* Content-Type: text/plain Content-Length: 38 Expect: 100-continue – Nulra Jan 18 '19 at 07:16
  • So this says your content length of your sent headers is 38. Are you looking at the headers that you received from the curl request? Where are you getting the -1 value? – Joseph_J Jan 18 '19 at 07:18
  • Also just for info. If I echo out your string length using your code it also equals 38. – Joseph_J Jan 18 '19 at 07:23
  • I am calling a third party API, and I receive the block error page, After checking, the technician from this third party said my request content-length is -1, so blocked.. – Nulra Jan 18 '19 at 07:26
  • Well I would say he is wrong. You just proved your content length is 38 not -1. Try adding a user agent to you curl options. I will update the code in my question here in a second. **Updated – Joseph_J Jan 18 '19 at 07:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186905/discussion-between-joseph-j-and-nulra). – Joseph_J Jan 18 '19 at 07:32
0

This has already been suggested in the comments. I've added an a Content type (change it as you wish if your content type is different).

   $data = array(
        'data'  =>  'Testing data',
        'name'  =>  'Testing',
        'no'    =>  '1234');
    $data_string = json_encode($data); 
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, Yii::$app->request->post('url'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_SSLVERSION, 6); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . mb_strlen($data_string) )
            ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
I.Dzinka
  • 44
  • 1
  • 4