3

我对 PHP 的 SOAP 库相当陌生,并且在为我正在访问的服务创建有效的 SoapHeader 时遇到问题。这是服务wsdl:

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' => 'aahardy@adobe.com',
    'ns2:password' => 'lkjasdf1'
    );

它可以工作,但我对ns2进行硬编码似乎很奇怪。这样做的正确方法是什么?

谢谢!

4

1 回答 1

2

@Aaronius 很好的例子!!

使用此 WSDL:https ://s7sps3apissl.scene7.com/scene7/webservice/IpsApi-2012-02-14.wsdl

于 2013-01-08T11:17:42.893 回答