Web Service Client using CakePHP & PHP SOAP

1 08 2007

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?


Actions

Information

4 responses

2 08 2007
Tigris

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.

10 06 2008
Dave Smith

This example is missing a closing bracket for the WsController class after line 15. Otherwise, works fine.

Fixed. Thanks. :)

27 08 2008
Hamdi(Bryan)

<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');
}

21 01 2009

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.