Reputation: 77
im trying to make a xml request to a ws using guzzle,(and i try with curl to) in php but always the response its in plain text no in xml
$client = new \GuzzleHttp\Client(['verify' => false]);
$soapRequest = <<<XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:san="mywebsservice">
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:Action>http://tempuri.org/mywebsservice</wsa:Action>
<To soap:mustUnderstand="1" xmlns="http://www.w3.org/2005/08/addressing">mywebsservice </To>
</soap:Header>
<soap:Body>
<tem:GetSecurityToken>
<tem:request>
<san:Connection>mywebsservice</san:Connection>
<san:Passwoord>mywebsservice</san:Passwoord>
<san:System>mywebsservice</san:System>
<san:UserName>mywebsservice</san:UserName>
</tem:request>
</tem:GetSecurityToken>
</soap:Body>
</soap:Envelope>
XML;
$request = $client->request('POST','mywebsservice', [
'headers' => [
'Content-Type' => 'application/soap+xml'
],
'body' => $soapRequest
]);
$response = $request->getBody()->getContents();
var_dump($response);
this is the response this is the response
string(1870) "
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://tempuri.org/webservices/GetSecurityTokenResponse</a:Action>
<ActivityId CorrelationId="cf1c12da-af1b-4013-ba89-25db2fa67dc1" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId>
</s:Header>
<s:Body>
<GetSecurityTokenResponse xmlns="http://tempuri.org/">
<GetSecurityTokenResult xmlns:b="webservices" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:AccessToken>token access</b:AccessToken>
<b:IdToken>the token</b:IdToken>
<b:TokenType>Bearer</b:TokenType>
</GetSecurityTokenResult>
</GetSecurityTokenResponse>
</s:Body>
</s:Envelope>
"
Upvotes: 0
Views: 1718
Reputation: 7571
The headers you are sending is what the receiving server uses to decide what content to serve. It will still be text content though, but only with a different Content-Type
header.
guzzlehttp/guzzle 6.x
The $response->json()
and $response->xml()
helpers were removed in 6.x. The following lines can be used to replicate that behaviour:
// Get an associative array from a JSON response.
$data = json_decode($response->getBody(), true);
See https://www.php.net/manual/en/function.json-decode.php
// Get a `SimpleXMLElement` object from an XML response.
$xml = simplexml_load_string($response->getBody());
See https://www.php.net/manual/en/function.simplexml-load-string.php
guzzlehttp/guzzle 5.x
Guzzle 5.x has some shortcuts to help you out:
$client = new Client(['base_uri' => 'https://example.com']);
$response = $client->get('/');
// $response = Psr\Http\Message\ResponseInterface
$body = (string) $response->getBody();
// $body = raw request contents in string format.
// If you dont cast `(string)`, you'll get a Stream object which is another story.
Now whatever you do with $body
is up to you. If it is a JSON response, you'd do:
$data = $response->json();
If it is XML, you can call:
$xml = $response->xml();
I never work with XML APIs so i can't give you any more examples on how to traverse the XML you will retrieve.
Upvotes: 3