1
我對PHP的SOAP庫相當陌生,並且在爲我打的服務創建有效的SoapHeader時遇到問題。這裏的服務WSDL:帶有命名空間條目陣列的PHP SoapHeader
http://s7sps1api.scene7.com/scene7/webservice/IpsApi-2010-01-31.wsdl
這裏是我的PHP腳本:
<?
try {
$options = array(
'exceptions'=>true,
'trace'=>1,
);
$ns = 'http://www.scene7.com/IpsApi/xsd';
$client = new SoapClient('http://s7sps1api.scene7.com/scene7/webservice/IpsApi-2010-01-31.wsdl', $options);
$auth = (object)array(
'user'=>'***',
'password'=>'***'
);
$header = new SoapHeader($ns, 'authHeader', $auth, false);
$client->__setSoapHeaders(array($header));
$client->getCompanyInfo(array('companyName' => '***'));
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";
}
catch(SoapFault $ex)
{
print "<pre>\n";
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "</pre>";
var_dump($ex->faultcode, $ex->faultstring, $ex->faultactor, $ex->detail, $ex->_name, $ex->headerfault);
}
?>
當我運行它,我得到以下幾點:
Request :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.scene7.com/IpsApi/xsd/2010-01-31" xmlns:ns2="http://www.scene7.com/IpsApi/xsd"><SOAP-ENV:Header><ns2:authHeader><user>***</user><password>***</password></ns2:authHeader></SOAP-ENV:Header><SOAP-ENV:Body><ns1:getCompanyInfoParam><ns1:companyName>***</ns1:companyName></ns1:getCompanyInfoParam></SOAP-ENV:Body></SOAP-ENV:Envelope>
string(14) "soapenv:Server" string(11) "ipsApiFault" NULL object(stdClass)#12 (1) { ["ipsApiFault"]=> object(stdClass)#13 (2) { ["code"]=> string(5) "30002" ["reason"]=> string(81) "Missing 'user' element for header '{http://www.scene7.com/IpsApi/xsd}authHeader'." } } NULL NULL
這幾乎是我需要它,但用戶和密碼節點沒有像我認爲他們應該那樣的scene7名稱空間。
如果我改變AUTH VAR這樣:
$auth = (object)array(
'ns2:user' => '[email protected]',
'ns2:password' => 'lkjasdf1'
);
它的工作原理,但它似乎哈克,我是硬編碼NS2。什麼是正確的方法來做到這一點?
謝謝!