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 />';
}
5 Comments
Neatly explained – thanks
Great little example. Quite handy. :)
this is fun, great example
So how to set for authentication when calling a SOAP method?
@John Doe: Sorry, I don’t know.
One Trackback/Pingback
Cake Baker’s Blog: A simple SOAP example…
…