首页 文章

PHP SOAP客户端没有创建正文

提问于
浏览
1

在尝试和阅读有关创建简单SOAP客户端的教程超过半天之后,我无法从API尝试使用的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>

以上返回预期数据 .

在将请求转换为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 . 谢谢

1 回答

  • 0

    您需要将参数传递给 $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密钥无效 .

相关问题