PHP SOAP 客户端未创建正文

PHP SOAP client not creating body

经过半天多的尝试和阅读有关创建简单 SOAP 客户端的教程,我离检索来自 API 我尝试使用的请求还差得很远。

WSDL:http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL

我可以使用以下简单的 SOAP 请求从 SOAP UI 发出请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://publicapi.ekmpowershop.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <pub:GetOrders>
         <!--Optional:-->
         <pub:GetOrdersRequest>
                <!--Optional:-->
            <pub:APIKey>myApiKey</pub:APIKey>
         </pub:GetOrdersRequest>
      </pub:GetOrders>
   </soapenv:Body>
</soapenv:Envelope>

以上 returns 预期数据。

在将请求翻译成 PHP 时,我有以下几点:

$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;

$client = new SoapClient($wsdl,
      array(
        'trace' => $trace,
        'exceptions' => $exceptions,
        'debug' => $debug,
      ));


$param = array('GetOrdersRequest' => array(
               'APIKey' => 'myApiKey'
            )
          );

$resp = $client->GetOrders();

print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());

如果将 $param 放入 GetOrders 函数中,它会中断并且什么也不会发生。

即使我在 $client->GetOrders(array('someArry' => $param)) 中使用数组,响应和请求仍然看起来总是一样的,看起来像 SOAP 请求的主体从未创建:

要求:

?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://publicapi.ekmpowershop.com/"><SOAP-ENV:Body><ns1:GetOrders/></SOAP-ENV:Body></SOAP-ENV:Envelope>

回复:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/"><GetOrdersResult><Status>Failure</Status><Errors><string>Object reference not set to an instance of an object.</string></Errors><Date>2017-04-03T16:00:42.9457446+01:00</Date><TotalOrders>0</TotalOrders><TotalCost xsi:nil="true" /></GetOrdersResult></GetOrdersResponse></soap:Body></soap:Envelope>

如果有人能阐明我在这里做错了什么,那将是真正的大帮助?

P.S 我在 PHP 中使用 SOAP 的经验有限,因为我习惯于在 java 环境中使用 SOAP。谢谢

您需要将参数传递到 $client->GetOrders() 调用中。 WSDL 还定义了一些必需的参数,所以一个最小的例子是这样的:

$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;

$client = new SoapClient($wsdl,
      array(
        'trace' => $trace,
        'exceptions' => $exceptions,
        'debug' => $debug,
      ));


$param = array(
    'GetOrdersRequest' => array(
        'APIKey' => 'dummy-key',
        'CustomerID' => 1,
        'ItemsPerPage' => 1,
        'PageNumber' => 1,
    )
);

$resp = $client->GetOrders($param);


print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());

这给出了错误响应:

<Errors><string>Invalid character in a Base-64 string.</string></Errors>

这大概是因为我的 API 密钥无效。