SO has a lot of questions for cases where a user is getting the desired result with curl but not with php's Guzzle http. In my case I am trying to use the new Httpclient of laravel 7 (essentially a wrapper around Guzzle) in the following manner:
$xml= '<root></root>';
$url = 'http://10.0.0.2/rcp';
$req = Http::withHeaders([
'Content-Type' => 'application/xml',
'Accept' => 'application/xml',
'user-agent' => 'Curl 7',
])
->withBasicAuth('user', 'secret')
->bodyFormat('xml')
->withOptions([
'body' => $xml,
'debug' => true,
])
->contentType('application/xml');
$req->post($url);
This gives me a 400 response.
With curl I have done:
curl http://10.0.0.2/rcp -X POST -u "user:secret" -H "Content-Type: application/xml" -H "Accept: application/xml" --data '<root></root>'
I have tried intercepting my requests and inspecting them: they seem identical. Still, there must be something wrong in the way I'm setting the body for Http, since I keep getting 400. Any insights?
Ps. I've also tried using mere Guzzle instead of Http, but the result is the same. I have also tried using:
$req->send('POST', $url, ['body' => $xml]);
instead of setting the body in the withOptions
call but with no success. I even added a user-agent header in order to 100% mimic curl but, of course, that didn't help.
UPDATE:
I noticed that I get this attached to the response with Http:
* Mark bundle as not supporting multiuse
UPDATE2:
I also managed to get the request working using curl from inside PHP in the following manner:
$xml= '<root></root>';
$url = 'http://10.0.0.2/rcp';
$username = 'user';
$password = 'secret';
$curl = curl_init();
curl_setopt_array($curl, [
curlopt_verbose => 1,
curlopt_url => $url,
curlopt_userpwd => $username . ':' . $password,
curlopt_timeout => 30,
curlopt_http_version => curl_http_version_1_1,
curlopt_customrequest => 'post',
curlopt_postfields => $xml,
curlopt_httpheader => [
'content-type: application/xml',
'accept: application/xml',
],
]);
$response = curl_exec($curl);