gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:34 24 4
gpt4 key购买 nike

我正在使用 SOAP 从第 3 方获取和更新数据,但在通过 __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);
}

我的连接很好,并且得到了 session ID,这很棒。但是,每当我调用 API 时,我都会被告知我的 session 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 相同的命名空间?

最佳答案

我知道可能是 THE COMPANY 解决了这个问题,但是对于向这个 Companies 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";
?>

关于php - 在 PHP 中设置具有多个命名空间的 SoapHeaders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com