A simple SOAP example

The code in this post is derived from the example presented in the article “Using PHP5′s SOAP Support”. You find explanations there, I will only describe some CakePHP specific things.

Most of the action happens in the model. As our model uses a webservice and not a table, we have to set “var $useTable = false;”.

// app/models/amazon.php
class Amazon extends AppModel
{
    var $useTable = false;
		
    function search($keyword)
    {
        $client = new SoapClient("
                     http://soap.amazon.com/schemas2/AmazonWebServices.wsdl");
			
        $params = array('keyword'     => $keyword,  
                        'page'        => 1,
                        'mode'        => 'books',
                        'tag'         => '',
                        'type'        => 'lite',
                        'devtag'      => 'YOUR_DEV_TAG');
    						
        return $client->KeywordSearchRequest($params); 
    }
}

The controller is simple and self-explanatory:

// app/controllers/amazon_controller.php
class AmazonController extends AppController
{
    function index()
    {
        $this->set('results', $this->Amazon->search('php pattern'));
    }
}

The same is true for the view: it is simple. Please notice that it doesn’t use the usual array syntax to access the data.

// app/views/amazon/index.thtml
foreach ($results->Details as $product)
{
    echo $product->ProductName . '<br />';
}
About these ads

5 Comments

  1. Posted July 18, 2006 at 5:54 am | Permalink

    Neatly explained – thanks

  2. Posted July 19, 2006 at 8:13 pm | Permalink

    Great little example. Quite handy. :)

  3. Posted July 26, 2006 at 8:17 pm | Permalink

    this is fun, great example

  4. John Doe
    Posted September 1, 2006 at 8:21 pm | Permalink

    So how to set for authentication when calling a SOAP method?

  5. Posted September 6, 2006 at 1:26 pm | Permalink

    @John Doe: Sorry, I don’t know.


One Trackback/Pingback

  1. Cake Baker’s Blog: A simple SOAP example…

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: