Putting the Pieces Together

The NewsGator API uses a few SOAP features that aren't well supported in many SOAP stacks: digest authentication and SOAP headers. I've been messing with PHP a bit, in particular the NuSOAP library, and documentation on those features is a bit hard to come by, though Google did a pretty good job coming up with relevant links - not always relevant to NuSOAP, but it turns out that the various libraries are not too different. My personal favorites: Zeev Suraski on accessing SalesForce.com via SOAP covers creating SOAP headers, and Accessing MapPoint via PHP covers authentication, in particular, HTTP digest.

PHP is sort of a funny beast to me. It's fairly straightforward to produce something that works, but it still has the hangup about statements vs. functions, print being a case in point: it's a statement, so it doesn't require parentheses. To be fair, a lot of other languages have this hangup. I've just really become aware of what an impediment this is to learning a new language after spending a lot of time with Lisps, which have an extremely consistent syntax.

<?php
require_once('nusoap/lib/nusoap.php');
# have to authenticate to get the WSDL, this seems to be the only way
$client   = new soapclient('http://username:password@services.newsgator.com/ngws/svc/Location.asmx?wsdl',true);
$err = $client->getError();
if ($err) {
    // Display the error
    echo $err;
    // At this point, you know the call that follows will fail
}
$client->setCredentials('username','password','basic');  # hmm, 'digest' doesn't seem to work here
$params   = array();
$locations    = $client->call('GetLocations',$params,'','',"<NGAPIToken xmlns='http://services.newsgator.com/svc/Location.asmx'><Token>YOURDEVELOPERTOKENHERE</Token></NGAPIToken>");

$err = $client->GetError();
if ($err) {
	echo $err;
}

foreach ($locations as $loc) {
	foreach ($loc as $p) {
		foreach ($p as $y) {
			var_dump ($y);
			print "\n";
		}
	}
}
?>

— Gordon Weakliem at permanent link