Avectra/netForum web services integration Posted by Mischa S.

Back

Date: October 5, 2011

Part 1 of 3

I recently did a web services integration using PHP and the non-profit focused membership system netForum (Avectra).  The objectives were as follows:

 - Integrate a SSO for members

 - Pull and display membership and subscription information

 - Give access only to members or subscribers who have paid for access to the specific databases

 

Lets start at the beginning, authenticating to the web service.  First we'll make our SOAP object:

class Soap {
	private $_avectraServiceLocation;
	private $_authReqParams;
	private $_responseHeaders;
	private $_authToken;
	private $_xwebNamespace;
	
	function __construct(){
		$this->_avectraServiceLocation = 'https://netforum.avectra.com/xweb/netFORUMXMLONDemand.asmx?WSDL';
		//set the username and password
		$this->_authReqParams = array('userName' => 'YOURUSERNAME', 'password' => 'YOURPASSWORD');
		$this->_responseHeaders;
		$this->_authToken;
		$this->_xwebNamespace;
	}
	public function makeSoap($loc = ''){
    	
		if($loc == ''){
			$loc = $this->_avectraServiceLocation;
		}
		return new SoapClient(	$loc, 
			        			array('trace'=>true, //turning on trace=true will let us grab the headers and responses
			           	 		'exceptions'=>true )
								);
    }
    public function authenticate($soap){
    	
    	$response = $soap->__SoapCall("Authenticate", array('parameters'=>$this->_authReqParams), null,null, $this->_responseHeaders);
    	
    	$this->_xwebNamespace = $response->AuthenticateResult;
		$this->_authToken = $this->_responseHeaders['AuthorizationToken']->Token;
		$authHeaders = new SoapHeader($this->_xwebNamespace, 'AuthorizationToken', Array('Token'=>$this->_authToken), true);
		
		return $authHeaders;
    }
	public function authenticateSSO($soap){

		$response = $soap->__SoapCall("Authenticate", array('parameters'=>$this->_authReqParams), null,null, $responseHeaders);

		return $response->AuthenticateResult;
    }
}

First we instantiate the variables we'll need. 

Then we build the constructor. 

Next, we create a makeSoap function.  You can manually provide a $loc (location) or it will use the default one.  All this really does is make a soap object for us to use.

Next, we'll create an authenticate method.  Here is where the 'fun' begins.

$response = $soap->__SoapCall("Authenticate", array('parameters'=>$this->_authReqParams), null,null, $this->_responseHeaders);

You'll notice that we pass the authReqParams in.  But, what messed me up was that you pass an array for parameters, in an array with one parameter, called parameters.  Aren't .NET API's awesome?!?

Grab the xwebNamespace and the AuthorizationToken from the responseHeaders.  Now we create our authHeaders.  We pass these each time to make sure that the application knows who we are.

Here is a checkuser function.  It is in a class called Customer, that extends Soap.

class Customer extends Soap {
public function checkUser($username,$password) {
		
		$soap = $this->makeSoap();
		$authHeaders = $this->authenticate($soap);
		//build the arguments
		$arguments = Array('szEmail' => $username,'szPassword' => $password);
		//$arguments = array();
		$response = $soap->__soapCall("CheckEWebUser", array('parameters'=>$arguments), null, $authHeaders, $responseHeaders);
		
		$responseResultSet = $response->CheckEWebUserResult->any;
		//var_dump($responseResultSet ->any);
		$xmlResultSet = simplexml_load_string($responseResultSet, "SimpleXMLElement", LIBXML_NOCDATA);
		
		return array('numberOfRows' => $xmlResultSet->attributes()->recordReturn, 'xmlResultSet' => $xmlResultSet);
		
	}
}

To call this function:

//include the soap and customer classes
include "includes/classes/soap.php";
include "includes/classes/customer.php";

//instantiate our object
$customer = new Customer();

//make the call
$c = $customer->checkUser($username,$password);

//dump it
print_r($c);

Now, if there are any results, we will see them.  Next chapter, we will look at the results.

 

I borrowed heavily from an article that I can't find right now.  If you are the author, please let me know so I can give credit!