phpsoapsoapheaderscene7

PHP SoapHeader with array of namespaced entries


I'm fairly new to PHP's SOAP lib and am having problems creating a valid SoapHeader for the service I'm hitting. Here's the service wsdl:

http://s7sps1api.scene7.com/scene7/webservice/IpsApi-2010-01-31.wsdl

Here's my PHP script:

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

?>

When I run it I get the following:

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

Which is almost where I need it, but the user and password nodes don't have the scene7 namespace like I believe they should.

If I change the auth var to this:

$auth = (object)array(
    'ns2:user' => 'aahardy@adobe.com',
    'ns2:password' => 'lkjasdf1'
    );

it works but it seems hacky that I'm hard-coding ns2. What's the right way to do this?

Thanks!


Solution

  • @Aaronius nice Example!!

    Use this WSDL: https://s7sps3apissl.scene7.com/scene7/webservice/IpsApi-2012-02-14.wsdl