Tomas Dohnal
Tomas Dohnal

Reputation: 2015

Curl not working with Content-Type

I have following piece of code:

$input = http_build_query(array('text' => 'hello'));
$url = "http://localhost/deskline/server.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain', 'Content-Length:'.strlen($input)));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);

echo $data;

And in the server.php file this code:

echo $_POST['text'];

Whenever I load the php file which should echo out the text (hello), nothing happens unless I remove the 'Content-Type: text/plain' from the header. I tried using 'text/html' insted but it didn't help at all.

I'll be very grateful for every piece of advice.

Thank you

Upvotes: 0

Views: 437

Answers (1)

Focorunner
Focorunner

Reputation: 56

I think you need to add an additional curl_setopt statement to make this a POST request.

Add the following:

curl_setopt($ch, CURLOPT_POST, 1);

Upvotes: 1

Related Questions