【发布时间】:2020-06-21 19:14:46
【问题描述】:
我正在尝试使用 guzzle 向 ws 发出 xml 请求,(我尝试使用 curl to)在 php 中,但始终以纯文本形式的响应在 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);
这是回复 这是回复
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>
"
【问题讨论】:
-
我想要一个 xml 响应而不是纯文本
-
也许您需要在请求中添加
Accept标头。Accept标头的作用在这里得到解释 --> webmasters.stackexchange.com/a/31231 -
除了文本之外,您认为 XML 是什么?发布你得到的和你期望的,以及为什么
-
我尝试使用 'Accept' => 'application/json' 和 'Accept' => 'application/soap+xml',我得到了相同的结果
-
我需要将该请求解析为 json 或数组,当我使用类似这样的东西时 $array_data = json_decode(json_encode(simplexml_load_string($response)), false);仅当我编码为数组返回空数组并且如果我编码为对象返回空对象时才返回给我
标签: php xml laravel request guzzle