How to send cURL POST without request data in PHP?

My goal is to send a POST request to the server and get the correct answer.

Note. Angle brackets are placeholders.

In the terminal, using the following code will give me the desired answer.

curl -u <user>:<pass> -H 'Content-Type: application/xml' -X POST https://<rest of url>

My current PHP looks something like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri); //$uri is the same that I use in terminal
curl_setopt($ch, CURLOPT_USERPWD,
    sprintf('%s:%s', $user, $pass)); //same as terminal user & pass
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array(
    'Content-Type: application/xml', //expect an xml response
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curl_result = curl_exec($ch);

Using this PHP, I get a 400 Bad Request error.

Detailed information:

> POST <same url> HTTP/1.1
Authorization: Basic YWRtaW5Ac3Bhcms0NTEuY29tOnNwYXJrc29tZXRoaW5n
Host: <correct host>
Accept: */*
Content-Type: application/xml
Content-Length: -1
Expect: 100-continue

* HTTP 1.0, assume close after body
< HTTP/1.0 400 Bad request
< Cache-Control: no-cache
< Connection: close
< Content-Type: text/html

Why am I getting a 400 Bad Request error when using PHP, but not when I use the command line? How can I fix this problem to get the desired answer using PHP?

+4
source share
2 answers
curl_setopt($ch, CURLOPT_POSTFIELDS, array());

. ; , CURLOPT_POSTFIELDS. PHP CURLOPT_POST, .

+2

, , Expect: 100-continue . :

http://php.net/manual/en/function.curl-setopt.php#82418

, , :

<?php
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
?>
0

All Articles