【问题标题】:guzzle xml request returns only plain textguzzle xml 请求仅返回纯文本
【发布时间】: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


【解决方案1】:

您发送的标头是接收服务器用来决定要提供哪些内容的标头。虽然它仍然是文本内容,但只有不同的 Content-Type 标头。

guzzlehttp/guzzle 6.x

在 6.x 中删除了 $response-&gt;json() 和 $response-&gt;xml() 助手。以下行可用于复制该行为:

// Get an associative array from a JSON response.
$data = json_decode($response->getBody(), true);

见https://www.php.net/manual/en/function.json-decode.php

// Get a `SimpleXMLElement` object from an XML response.
$xml = simplexml_load_string($response->getBody());

见https://www.php.net/manual/en/function.simplexml-load-string.php

guzzlehttp/guzzle 5.x

Guzzle 5.x 有一些快捷键可以帮助您:

$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.

现在,无论您如何处理 $body,都由您决定。如果是 JSON 响应,你会这样做:

$data = $response->json();

如果是XML,可以调用:

$xml = $response->xml();

我从不使用 XML API,所以我不能再给你提供更多关于如何遍历你将检索到的 XML 的示例。

【讨论】:

  • 谢谢我去尝试了,我让你知道结果。谢谢
  • 我尝试过,但它不是 guzzle 6 的方法,我尝试使用 curl 并且我有相同的错误响应它的纯文本
  • 我已经更新了答案,我不知道它们已被删除。
  • 完美,现在我尝试使用 // 从 JSON 响应中获取关联数组。 $data = json_decode($response->getBody(), true);
  • 用 6.x 返回一个 NULL 值
猜你喜欢
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多