Some progress with Amazon E-Commerce Service

Author Message

Geraint Edwards

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

Andrew Vorobyov

Saturday 28 May 2005 10:37:06 pm

cool

kracker (the)

Sunday 29 May 2005 1:07:04 pm

More than cool, <i>kickin'</i>

Where is Geraint now? We need more people like him in the community!

Wow, Wow...Wow!

//kracker

<i>sole : theme</i>

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 31 2025 06:23:23
Script start
Timing: Jan 31 2025 06:23:23
Module start 'layout'
Timing: Jan 31 2025 06:23:23
Module start 'content'
Timing: Jan 31 2025 06:23:23
Module end 'content'
Timing: Jan 31 2025 06:23:23
Script end

Main resources:

Total runtime0.0198 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0068 588.2578151.2266
Module start 'layout' 0.00690.0023 739.484436.6797
Module start 'content' 0.00910.0089 776.164194.1875
Module end 'content' 0.01810.0017 870.351637.9922
Script end 0.0198  908.3438 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002713.6132140.0002
Check MTime0.00125.8437140.0001
Mysql Total
Database connection0.00094.374310.0009
Mysqli_queries0.002914.729630.0010
Looping result0.00000.069910.0000
Template Total0.00125.910.0012
Template load0.00094.536910.0009
Template processing0.00031.309210.0003
Override
Cache load0.00063.262710.0006
General
dbfile0.00031.462180.0000
String conversion0.00020.981640.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs