Set SoapHeaders with multiple namespace in PHP

二次信任 提交于 2019-12-22 10:14:10

问题


I'm using SOAP to grab and update data from a 3rd party and I'm having trouble setting the namespace when setting the connection via __setSoapHeaders.

Here's my code (example):

$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); 
}

I'm connecting fine and getting the session ID out which is great. However, whenever I make a call to the API, I'm being told my session ID is not valid. Looking further into it, I can see there's two namespaces in the SOAP Request and only ns2 is the value of $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>

How do I change ns1 to be the same namespace as ns2?


回答1:


I do know that possibly THE COMPANY sorted that problem out, but there is no definitive answer on requesting data to this Companies API.

I've included a finished code that i was struggling with, anyone using this Companies API would just need to read their GITHUB documentation and use this below code accordingly :

<?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";
?>


来源:https://stackoverflow.com/questions/33038361/set-soapheaders-with-multiple-namespace-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!