Page 1 of 1

Attaching SOAP Header

Posted: Wed Feb 24, 2010 9:27 am
by JNettles
Having some small trouble accessing a protected web service, hopefully someone can help me out - I'm sure I'm just missing something simple here.

Basically, I need to construct the following SOAP message in PHP. I have no trouble with the bottom part, the actual request, its attached the auth header that's tripping me up. Anybody know how to do this?

Code: Select all

<soapenv:Envelope>
    <soapenv:Header>
        <q0:AuthHeader>
            <q0:username>JED</q0:username>
            <q0:password>JEDI2003</q0:password>
        </q0:AuthHeader>
    </soapenv:Header>
    <soapenv:Body>
        <q0:SendString>
            <q0:thestring>Hello there</q0:thestring>
        </q0:SendString>
    </soapenv:Body>
</soapenv:Envelope>

Code: Select all

 
$client = new soapclient($wsdl);
//attach header somehow here
$client->SendString("hello there");

Re: Attaching SOAP Header

Posted: Wed Feb 24, 2010 9:59 am
by JNettles
Annnnd nevermind. As usual, fifteen minutes after I post I stumble across the answer anyway.

For those interested here's the solution.

Code: Select all

class AuthHeader
{
    public $username;
    public $password;
    
    public function __construct()
    {
        $this->username = "JED";
        $this->password = "JEDI2003";
    }
}
 
$client = new soapclient('$wsdl');
 
$ns = 'http://tempuri.org/';
 
$AuthHeader = new AuthHeader;
$header =  new SoapHeader($ns, "AuthHeader", new AuthHeader, false);
$client->__setSoapHeaders(array($header));
$client->login(array("thestring" => "hellloooo nurse"));
 
var_dump($client);