Wednesday 18 May 2005 3:18:24 am
You can perform a simple search with the following code:
// Require the necessary libraries
require_once('lib/ezsoap/classes/ezsoapclient.php');
require_once('lib/ezsoap/classes/ezsoaprequest.php');
// Create a SOAP request
$client=new eZSOAPClient("soap.amazon.co.uk","/onca/soap?Service=AWSECommerceService");
$request=new eZSOAPRequest("ItemSearch","http://webservices.amazon.com/AWSECommerceService/2005-03-23");
$request->addParameter('SearchIndex','Books');
$request->addParameter('Keywords',"data on the web");
$request->addParameter("SubscriptionId","PUT YOUR ID HERE");
// look at the payload for test purposes
print $request->payload();
// Send the request to get back a response object
$response = $client->send($request);
if( $response->isFault() ) {
trigger_error($response->faultString());
return false;
}
else print ($response->DOMDocument->toString())
Looking at the WDSL the keywords and searchindex paramaters should be part of a complex type. This doesn't appear to be supported in ezsoap at present but is easily accomodated. If we add a "type" variable to ezsoapparameter.php
/*!
Sets the complextype name.
*/
function setType($type)
{
$this->Type = $type;
}
/*!
Returns the parameter complextype.
*/
function &type()
{
return $this->Type;
}
...
/// The type of parameter is its complex
var $Type;
}
We can use this in the encoding step of ezsoaprequest.
...
/*!
Adds a new parameter to the request. You have to provide a prameter name
and value - also a complex-type name
*/
function addComplexParameter( $name, $complextype, $value )
{
$param =& new eZSOAPParameter( $name, $value );
$param->setType($complextype);
$this->Parameters[] = $param;
}
...
encodeValue gets a new parameter (type) so I change the call to it in payload() to:
$param =& $this->encodeValue( $parameter->name(), $parameter->value(), $parameter->type() );
I then add a new case to the encodeValue function
// probably a complextype - needs more error checking!
case "object" :
{
$node =& eZDOMDocument::createElementNode( $name );
$attr =& eZDOMDocument::createAttributeNode( "type", "tns:".$type );
$attr->setPrefix( EZ_SOAP_XSI_PREFIX );
$node->appendAttribute( $attr );
// add the request parameters
foreach ($value->Parameters as $prm)
{
unset( $subNode );
$subNode =& $this->encodeValue( $prm->name(), $prm->value(), $prm->type() );
$node->appendChild( $subNode );
}
$returnValue =& $node;
} break;
I don't know much PHP so don't know how to do the error checking here - any suggestions welcome. Also note that the "tns" is hard coded here - not sure is this is important (I'm at the edges of my understanding at this stage)? Our original function call now changes to // Require the necessary libraries
require_once('lib/ezsoap/classes/ezsoapclient.php');
require_once('lib/ezsoap/classes/ezsoaprequest.php');
$client=new eZSOAPClient("soap.amazon.co.uk","/onca/soap?Service=AWSECommerceService");
$SubscriptionId="PUT YOUR ID HERE";
$SearchIndex="Books";
$Keywords="data on the web";
$complexRequest=&new eZSOAPRequest("ItemSearchRequest","http://webservices.amazon.com/AWSECommerceService/2005-03-23");
$complexRequest->addParameter('SearchIndex',$SearchIndex);
$complexRequest->addParameter('Keywords',$Keywords);
$request=new eZSOAPRequest("ItemSearch","http://webservices.amazon.com/AWSECommerceService/2005-03-23");
$request->addParameter("SubscriptionId",$SubscriptionId);
$request->addComplexParameter("Request","ItemSearchRequest",$complexRequest);
print $request->payload();
// Send the request to get back a response object
$response = $client->send($request);
if( $response==false) {
print "error";
}
else print ($response->DOMDocument->toString())
Both versions of the code produce the same results but thesecond is more inline with the WDSL's suggestions in my opinion. On a separate note: I took a look at the useful article on phpPatterns (http://www.phppatterns.com/index.php/article/articleview/40/1/2/) about using the WDSL. The generated functions don't appear to work since ezsoaprequest doesn't take SOAP_Value type parameters. Maybe something to incorporate at a future date - also the complex type issue arises again. Geraint
|