简体   繁体   中英

php cURL POST content length always show -1

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~

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM