I’m using PHP version 5.2.1 and CakePHP version 1.1.16-5421.
In this example we’ll create a basic web service client using native PHP SOAP function and CakePHP. The service we will consume is a “fortune”, you can find its info at xmethods.net.
Here is the complete code:
<?php
class WsController extends AppController {
var $uses = array();
var $layout = '';
function fortune() {
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$client = new SoapClient("http://www.doughughes.net/WebServices/fortune/fortune.cfc?wsdl",
array('proxy_host' => "10.1.89.231",'proxy_port' => 8080)
); // I'm using proxy to connect Internet from my LAN
try {
echo $client->getFortune();
} catch (SoapFault $exception) {
echo $exception;
}
}
}
?>
Don’t forget to create empty file for the view (I’m wondering how to create an action without a view in CakePHP). Then access it at http://<url to your cake>/ws/fortune.
The key in that code is the WSDL, and function “getFortune()” which is defined in the WSDL.
Really simple isn’t it?
I looked into cakephp and webservices too and I really think that getting information from a webservice should be in a Model. Beside that its a good example.
This example is missing a closing bracket for the WsController class after line 15. Otherwise, works fine.
<blockquote cite=”Don’t forget to create empty file for the view (I’m wondering how to create an action without a view in CakePHP). Then access it at http:///ws/fortune.”>
If you are using Cakephp 1.2 you can define your action “fortune” into the controller with as a public function and then use a redirect directive:
public function fortune() {
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$client = new SoapClient("http://www.doughughes.net/WebServices/fortune/fortune.cfc?wsdl",
array('proxy_host' => "10.1.89.231",'proxy_port' => 8080)
); // I'm using proxy to connect Internet from my LAN
try {
echo $client->getFortune();
} catch (SoapFault $exception) {
echo $exception;
}
$this->redirect('controller'=>'somecontrollers', 'action'=>'someaction');
}
this is bad design. go here: http://www.pagebakers.nl/2008/12/18/soapsource-a-soap-client-datasource-for-cakephp/