Attaching SOAP Header

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Attaching SOAP Header

Post 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");
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Attaching SOAP Header

Post 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);
Post Reply