Sunday, April 3, 2011

Sending a Soap Header with a WSDL Soap Request with PHP

I'm extremely new to SOAP and I'm trying to implement a quick test client in PHP that consumes a ASP.NET web service. The web service relies on a Soap Header that contains authorization parameters.

Is it possible to send the auth header along with a soap request when using WSDL?

My code:

php

$service = new SoapClient("http://localhost:16840/CTI.ConfigStack.WS/ATeamService.asmx?WSDL");
$service->AddPendingUsers($users, 3); // Example

webservice

[SoapHeader("AuthorisationHeader")]
[WebMethod]
public void AddPendingUsers(List<PendingUser> users, int templateUserId)
{
    ateamService.AddPendingUsers(users, templateUserId, AuthorisationHeader.UserId);
}

How would the auth header be passed in this context? Or will I need to do a low lever __soapCall() to pass in the header? Also, am I invoking the correct soap call within PHP?

From stackoverflow
  • You should be able to create a header and then add it to the client so it is sent for all subsequent requests. You will probably need to change the namespace parameter.

    $service = new SoapClient("http://localhost:16840/CTI.ConfigStack.WS/ATeamService.asmx?WSDL");
    //                        Namespace               Header Name          value   must-understand
    $header = new SoapHeader('http://tempuri.org/', 'AuthorisationHeader', $value, false);
    $service->__setSoapHeaders(array($header));   
    
    $service->AddPendingUsers($users, 3); // Example
    

    More information here

  • $client = new SoapClient(PassportWebService);
    $apiauth =array('userName'=>HeaderName,'password'=>HeaderPassport,'ip'=>$onlineip);
    
    $authvalues = new SoapVar($apiauth, SOAP_ENC_OBJECT,'ReqHeader',"SoapBaseNameSpace");
    $header =  new SoapHeader("SoapBaseNameSpace","ReqHeader", $authvalues, true);
    $client->__setSoapHeaders(array($header));
    
  • I am having a problem getting a custom soap header to work with PHP5. What I require is something like this:

    <SOAP-ENV:Header> 
    <USER>myusername</USER> 
    <PASSWORD>mypassword</PASSWORD> 
    </SOAP-ENV:Header> 
    

    What I get is :

    <SOAP-ENV:Header> 
    <ns2:auth> 
    <USER>myusername</USER> 
    <PASSWORD>mypassword</PASSWORD> 
    </ns2:auth> 
    </SOAP-ENV:Header> 
    

    I would like to remove the namespace tags. The code I use to get this is:

    class Authstuff { 
    public $USER; 
    public $PASSWORD; 
    public function __construct($user, $pass) { 
    $this->USER = $user; 
    $this->PASSWORD = $pass; 
    } 
    } 
    
    $auth = new Authstuff('myusername', 'mypassword'); 
    $param = array('Authstuff' => $auth); 
    $authvalues = new SoapVar($auth,SOAP_ENC_OBJECT); 
    $header = new 
    SoapHeader('http://soapinterop.org/echoheader/',auth,$authvalues); 
    
    John Saunders : @Dees: welcome to StackOverflow. Please read the FAQ (http://stackoverflow.com/faq). You'll find this is not a discussion forum. You should not "reply to an old thread". If you have a question that hasn't already been answered, then ask your own question, supplying your own details. Thanks.

0 comments:

Post a Comment