PHP SoapClient - 在错误的地方行动

时间:2017-04-13 23:00:25

标签: php soap

我使用的是PHP SoapClient,遇到了第三方想要的请求格式问题。

他们想要这样:

POST /service.asmx HTTP/1.1
Host: service.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <Order xmlns="http://someservice">
      <json>string</json>
    </Order>
  </soap12:Body>
</soap12:Envelope>

然而,使用SoapClient,我似乎最接近它,就像这样:

POST /service.asmx HTTP/1.1
Host: service.com
Content-Type: application/soap+xml; charset=utf-8; action="http://someservice"
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <Order>
      <json>string</json>
    </Order>
  </soap12:Body>
</soap12:Envelope>

请注意我的请求中的操作如何在http标头中,并且理想格式的操作位于Order标记中。问题是,它正在生成放置动作的SoapClient - 它从WSDL获取特定URL,甚至不在我的代码中。

如何告诉SoapClient将其放在正确的位置?就我而言,试图只包括必要的内容,这基本上就是代码:

$this->client = new SoapClient($this->wsdl, array(
                'soap_version' => SOAP_1_2,
                'encoding' => 'UTF-8',
                'stream_context' => stream_context_create($context),
                'trace' => true,
                'exceptions' => true,
            )
        );
    $json = json_encode($request);

    // Prepare the xml
    $xml = array();
    $xml[] = new SoapVar($json, XSD_STRING, 'string', 'http://www.w3.org/2001/XMLSchema', 'json');

    $this->finalXML = new SoapVar($xml, SOAP_ENC_OBJECT, null, null, 'Order');
    $this->response = $this->client->CreateOrder($this->finalXML);

1 个答案:

答案 0 :(得分:1)

经过几天的敲击,我发现了这一点,最终让我接受的hacky解决方案对我有用:

Change SOAP request format

显然,当您使用写得不好的Soap API时,您可以做的事情并不多。