无法从Soap响应中对base64decode进行gzdecode

时间:2019-10-15 23:26:06

标签: php soap base64 gzip

我对此SOAP响应有疑问:

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetStockResponse xmlns="http://retailexpress.com.au/">
          <GetStockResult>base64Binary</GetStockResult>
        </GetStockResponse>
      </soap:Body>
    </soap:Envelope>

因此,我用于处理此响应的代码如下:

    public function getStock()
    {

        $soapClient = $this->getSoapClient();

        $xmlRequest = soapHead . '<ret:GetStock></ret:GetStock>' . soapFoot;

        try {
            $request = $soapClient->__anotherRequest('GetStock', $xmlRequest);
            $requestDecoded = base64_decode($request);
            $stock = gzdecode($requestDecoded);
        } catch (SoapFault $e ){
            var_dump($e);
            exit();
        }

        $response = new Response($stock);
        $response->headers->set('Content-Type', 'text/xml');

        return $response;

    }

使用此代码,我总是会遇到此错误:

Warning: gzdecode(): data error

但是如果我对var_dump $ request进行了解码,并且我尝试对这个字符串进行gzdecode,那就可以了!

我在这里找不到我的错误... 每次尝试我都要等待5分钟^^'

我希望我能理解

多谢

Tanguy

1 个答案:

答案 0 :(得分:0)

我找到了解决方案, soap响应是一个xml,因此您需要创建一个SimpleXmlElement来获取内容

        $request = $soapClient->__anotherRequest('GetStock', $xmlRequest);
        $xmlRequest     = new \SimpleXMLElement($request);
        $response = $xmlRequest->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->GetStockResponse;
        $result = (string) $response->GetStockResult;

        $requestDecoded = base64_decode($result);
        $stock = gzdecode($requestDecoded);