在PHP中设置具有多个命名空间的SoapHeaders

时间:2015-10-09 12:29:48

标签: php soap

我正在使用SOAP从第三方获取和更新数据,而在通过__setSoapHeaders设置连接时我无法设置命名空间。

这是我的代码(示例):

$ns = "http://wms.website.net/";
$clientid = "123"; 
$username = "username"; 
$password = base64_encode("password");  
$socket_context = stream_context_create(array('http' => array('protocol_version'  => 1.0)));
$client = new SoapClient("http://website.net/$clientid/resources/integrationservicev4.asmx?WSDL", array('exceptions' => 0,'stream_context' => $socket_context,'trace' => 1)); 

$params = array("clientId"=>$clientid,"username"=>$username,"password"=>$password);
$start = $client->Authenticate($params);
if (is_soap_fault($start)) {
    trigger_error("SOAP Fault: (faultcode: {$start->faultcode}, faultstring: {$start->faultstring})", E_USER_ERROR);
    print "<br />";
} else {
    $response = $start->AuthenticateResult->Detail;
    $response_explode = explode(",",$response);
    $sessionid = $response_explode[1];

    //Body of the Soap Header. 
    $headerbody = array('ClientId' => $clientid, 'SessionId' => $sessionid); 
    //Create Soap Header.        
    $header = new SOAPHeader($ns, 'UserSessionCredentials', $headerbody);       
    //set the Headers of Soap Client. 
    $client->__setSoapHeaders($header); 
}

我正在连接好并获得会话ID,这很棒。但是,每当我调用API时,都会被告知我的会话ID无效。进一步研究,我可以看到SOAP请求中有两个名称空间,只有ns2是$ ns的值:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.website.net/" xmlns:ns2="http://wms.website.net/">
    <SOAP-ENV:Header>
        <ns2:UserSessionCredentials>
            <item>
                <key>ClientId</key>
                <value>cls22754</value>
            </item>
            <item>
                <key>SessionId</key>
                <value>4b62f147-0277-4f2f-be45-005fed25e6db</value>
            </item>
        </ns2:UserSessionCredentials>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:SaveData>
            <ns1:saveRequest>
                <ns1:TemplateName>Sales orders</ns1:TemplateName>
                <ns1:CsvData>SalesOrderNumber,ShippingCost,TotalSale,CreatedDate    
                    123,1.00,49,2015-10-08 12:16:06    
                    456,1.00,100,2015-10-08 18:13:36    
                    789,0.00,16.50,2015-10-08 18:52:12</ns1:CsvData>
                <ns1:Action>0</ns1:Action>
            </ns1:saveRequest>
        </ns1:SaveData>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何将ns1更改为与ns2相同的命名空间?

1 个答案:

答案 0 :(得分:0)

我知道公司可能已经解决了这个问题,但是对于向公司API申请数据没有明确的答案。

我已经包含了一个我正在努力解决的完成代码,使用此公司API的任何人只需要阅读他们的GITHUB文档并相应地使用下面的代码:

<?php
$ns = "http://www.thenamespace.net/";
$userid = 123; // you can find out this part from loggin into WMS and checking HTTP requests/responses and you can get the ID
$clientid = "aaa1234"; 
$username = "username"; 
$password = base64_encode("password");  
$socket_context = stream_context_create(array('http' => array('protocol_version'  => 1.0)));
$client = new SoapClient("http://wms.system.net/$clientid/resources/integrationservicev4.asmx?WSDL", array('exceptions' => 0,'stream_context' => $socket_context,'trace' => 1)); 


// body vars
$someTemplateName = 'Critical'; //from the created reporting template
$somePageNo = 1;
$someItemsPerPage = 1000;
$someSearchClause = '';
$someFilterClause = 'RequestedDeliveryDate >= DateTime(2017,04,13,06,00,00)'; // here we can do $date = date(Y,m,d,) . $time
$someOrderBy = '[Type]';
$someColumns = '[Type],[Total]'; // columns to show

$params = array("clientId"=>$clientid,"username"=>$username,"password"=>$password);
$start = $client->Authenticate($params);
if (is_soap_fault($start)) {
    trigger_error("SOAP Fault: (faultcode: {$start->faultcode}, faultstring: {$start->faultstring})", E_USER_ERROR);
    print "<br />";
} else {
    $response = $start->AuthenticateResult->Detail;
    $response_explode = explode(",",$response);
    $sessionid = $response_explode[1];


    //Body of the Soap Header. 
    $headerbody = array('UserId' => $userid,'ClientId' => $clientid, 'SessionId' => $sessionid); 
    //Create Soap Header.        
    $header = new SOAPHeader($ns, 'UserSessionCredentials', $headerbody);       
    //set the Headers of Soap Client. 
    $client->__setSoapHeaders($header); 

    $body = array(  'TemplateName'=>$someTemplateName,
                'PageNo'=>$somePageNo,
                'ItemsPerPage'=>$someItemsPerPage,
                'SearchClause'=>$someSearchClause,
                'FilterClause'=>$someFilterClause,
                'OrderBy'=>$someOrderBy,
                'Columns'=>$someColumns);

}
$params = array('getReportRequest' => $body);
$reply = $client->GetReportData($params); // ACTUAL MAGIC
var_dump($reply);
print_r($start);
echo "<br><br><br>ResponseT:\n" . $client->__getLastResponse() . "\n";
echo "<br><br><br>REQUEST:\n" . $client->__getLastRequest() . "\n";
?>