views:

24

answers:

2

I;ve inherited some php SOAP code and due to changes in the service we are using, I need to modify to "add an authorization in the HTTP headers of all requests". I'm not sure what to do and if its even possible.

Part of the relevant code looks like this:

    function soap_connect() {
            $soap_options = array(
                    'soap_version' => SOAP_1_2,
                    'encoding' => 'UTF-8',
                    'exceptions' => FALSE
            );
            try {
                    $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
            } catch (SoapFault $fault) {
                    return FALSE;
            }
            return TRUE;
    }

I think, as I understand it, it should be just outputting the following (right now):

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Content-Length: 255
...

The documentatino says that the final HTTP request should look like:

Content-Type: application/soap+xml;charset=UTF-8;action="http://ws.testserver.net/nsp/client/hsserve/listHardware"
Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw
Content-Length: 255
A: 

It's quite difficult to figure out what type of structure the server is expecting without seeing the wsdl, but here are a few examples:

Simple HTTP auth

$soap_options = array(
                'soap_version'  =>  SOAP_1_2,
                'encoding'      =>  'UTF-8',
                'exceptions'    =>  FALSE,
                    'login'         =>  'username',
                    'password'      =>  'password'
        );
        try {
                $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
        } catch (SoapFault $fault) {
                return FALSE;
        }
        return TRUE;    

For the servers which implement a more advanced, custom method:

// Namespace for SOAP functions
$ns         =   'Namespace/Goes/Here';

// Build an auth array
$auth = array();
$auth['AccountName']    =   new SOAPVar($this->account['AccountName'], XSD_STRING, null, null, null, $ns);
$auth['ClientCode']     =   new SOAPVar($this->account['ClientCode'], XSD_STRING, null, null, null, $ns);
$auth['Password']       =   new SOAPVar($this->account['Password'], XSD_STRING, null, null, null, $ns);

// Create soap headers base off of the Namespace
$headerBody = new SOAPVar($auth, SOAP_ENC_OBJECT);
$header = new SOAPHeader($ns, 'SecuritySoapHeader', $headerBody);
$client->__setSOAPHeaders(array($header));
Kieran Allen
A: 

Add a stream context to provide the additional headers to the HTTP call.

function soap_connect() {
    $context = array('http' =>
        array(
            'header'  => 'Authorization: WRAP access_token=Z-H7SnqL49eQ2Qp5pLH8k-RVxHfewgIIDt4VCeI2CNnrS4-gBMzPWbfZuMhgvISVV-uTSikS1SqO0n2PRkH3ysQ-uWbvU9podPAm6HiiIS5W2mtpXUfN9ErBmkjF6hDw'
        )
    );
    $soap_options = array(
        'soap_version' => SOAP_1_2,
        'encoding' => 'UTF-8',
        'exceptions' => FALSE,
        'stream_context' => stream_context_create($context)
    );
    try {
        $this->soap_client = new SoapClient($this->configuration['wsdl'], $soap_options);
    } catch (SoapFault $fault) {
        return FALSE;
    }
    return TRUE;
}

Please see SoapClient::__construct() and HTTP context options for further information,

Stefan Gehrig