Raghav
Raghav

Reputation: 4638

PHP cURL data not posted if headers are set?

I need to set header and post required parameters for an authentication application. But the problem is if i set header data is not posted.

target.php

<?php
echo $_POST['registration_id'];
?>

PHP Script With Post Parameters and Without Headers

<?php
  $inputdata = 'registration_id=123456789';
  $x = curl_init("http://localhost/target.php");
  curl_setopt($x, CURLOPT_POST, 1);
  curl_setopt($x, CURLOPT_POSTFIELDS, $inputdata);
  curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
  $data = curl_exec($x);
  var_dump($data);
  curl_close($x);
?>

PHP Script With Header and POST Data

   <?php
      $inputdata = 'registration_id=123456789';
      $x = curl_init("http://localhost/target.php");
      curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-length: 9'));
      curl_setopt($x, CURLOPT_POST, 1);
      curl_setopt($x, CURLOPT_POSTFIELDS, $inputdata);
      curl_setopt($x, CURLOPT_FOLLOWLOCATION, 0);
      curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);
      $data = curl_exec($x);
      var_dump($data);
      curl_close($x);
    ?>

So in case 1(Without headers) My Output is data which i sent. In case 2(With headers) my output is blank. If i set header data is not posted. So What is the possible solution. Thank You.

Upvotes: 0

Views: 1722

Answers (2)

Furgas
Furgas

Reputation: 2844

Try:

curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-length: '.strlen($inputdata)));

Upvotes: 1

marc
marc

Reputation: 6223

What about

$header = 'Content-length: ' . strlen($inputdata);
curl_setopt($x, CURLOPT_HTTPHEADER, array($header));

to let the web server get the full length of the data.

Upvotes: 1

Related Questions