简体   繁体   中英

PHP CURL PUT Equivalent to Command Line

I am working on a PHP webservice that will be making REST calls to an application API. My webservice is built on a LAMP server running ubuntu and the following command is working perfect from command line:

curl -k -u username -H "Content-type: application/xml" -X PUT -d @request.xml https://server/path/

However, when I try to build the same request in PHP I am getting the following response from the REST server, indicating that the XML is missing:

HTTP400Premature end of file.

Here is the code of my PHP request:

$ch2 = curl_init($service_url);
$headers = array('Content-Type: application/xml', 'Accept: application/xml');
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_PUT, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch2, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch2, CURLOPT_USERPWD, 'user:password');
$curl_response = curl_exec($ch2);

Everything I have read indicates the CURLOPT_POSTFIELDS is the place to do this, but I thought this was for adding "?" variables to the actual URL. Anyway, any help would be appreciated.

According to the manual , if you use CURLOPT_PUT you should also provide CURLOPT_INFILE and CURLOPT_INFILESIZE .

Maybe instead of

curl_setopt($ch2, CURLOPT_POSTFIELDS, $xmlRequest);

try

curl_setopt($ch2, CURLOPT_INFILE, fopen('request.xml', 'r'));
curl_setopt($ch2, CURLOPT_INFILESIZE, filesize('request.xml'));

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