-2

I have command

curl -v -X POST -H "Content-Type: application/xml" -d @case.xml --user test:test url

in php send

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_USERPWD, "test:test");
curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => new CurlFile('case.xml')]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

but this code not work, error 400 Bad Request.

The web server uses Basic HTTP Authentication. I'm using 7.0.11

halfer
  • 19,824
  • 17
  • 99
  • 186
vladimir
  • 1
  • 1
  • What is your $url contains, means show me your URL? – Mayank Pandeyz Oct 03 '16 at 11:29
  • yes, $url = 'http://example.com'; – vladimir Oct 03 '16 at 11:47
  • 1
    Possible duplicate of [How do I make a request using HTTP basic authentication with PHP curl?](http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – halfer Oct 03 '16 at 12:38
  • Hmm, I've suggested it is a duplicate of another question (and it may still be) but I'd not spotted you've used `CURLOPT_USERPWD` already. I'd recommend pointing it to a service you have control over, and record the request headers, to see what the difference is. – halfer Oct 03 '16 at 12:40

1 Answers1

0
<?php
$login='test';
$password='test';
$xml_data =file_get_contents(realpath('case.xml'));
$url ='mega_url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$output = curl_exec($ch);

it is work

vladimir
  • 1
  • 1