A simple SOAP example

Published on and tagged with cakephp  webservices

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("
        $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 />';

10 comments baked

Bake a comment

(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License