<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Simon&#039;s Wonder World &#187; xmlrpc</title>
	<atom:link href="http://blog.simonstahl.com/tag/xmlrpc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.simonstahl.com</link>
	<description>Just another European geek in Silicon Valley</description>
	<lastBuildDate>Tue, 19 Apr 2011 22:01:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP Webservices with the Zend Framework</title>
		<link>http://blog.simonstahl.com/2009/11/14/php-webservices-wth-the-zend-framework/</link>
		<comments>http://blog.simonstahl.com/2009/11/14/php-webservices-wth-the-zend-framework/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 19:10:45 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[xmlrpc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://simonstahl.com/wordpress/?p=6</guid>
		<description><![CDATA[With the Zend framework you can choose between Rest-, XmlRpc- and Soap-Webservices. I am going to describe here all of them with a simple ping service. For all of them you need first a function or a class which you like to expose as service. MyTestService.php &#60;?php class MyTestService { /** * A simple ping [...]]]></description>
			<content:encoded><![CDATA[<p>With the Zend framework you can choose between <strong>Rest</strong>-, <strong>XmlRpc</strong>- and <strong>Soap</strong>-Webservices. I am going to describe here all of them with a simple ping service.<span id="more-6"></span></p>
<p>For all of them you need first a function or a class which you like to expose as service.</p>
<p>MyTestService.php</p>
<pre class="brush:php">&lt;?php

class MyTestService {

	/**
	 * A simple ping service
	 *
	 * @param string $value
	 * @return string
	 */
	function ping($value) {
		return $value . ' back from server';
	}
}</pre>
<p>It is quite important to add the documentation with the exact datatype for all parameters as well as for the return value to the function. This information is used by the Zend framework to parse the values e.g. for the WSDL generation.</p>
<h2><strong>Rest Service</strong></h2>
<p>RestService.php</p>
<pre class="brush:php">&lt;?php

require 'Zend/Rest/Server.php';
require_once 'MyTestService.php';

$server = new Zend_Rest_Server();
$server-&gt;setClass('MyTestService');
$server-&gt;handle();</pre>
<h2>XmlRpc Service</h2>
<p>XmlRpcService.php</p>
<pre class="brush:php">&lt;?php

require 'Zend/XmlRpc/Server.php';
require_once 'MyTestService.php';

$server = new Zend_XmlRpc_Server();
$server-&gt;setClass('MyTestService');
echo $server-&gt;handle();</pre>
<p>Caution: the echo on the last line is here essential!</p>
<h2>Soap Service</h2>
<p>SoapWebService.php</p>
<pre class="brush:php">&lt;?php

require 'MyTestService.php';

if (isset ( $_GET ['wsdl'] )) {
	require 'Zend/Soap/AutoDiscover.php';

	$autodiscover = new Zend_Soap_AutoDiscover ( );
	$autodiscover-&gt;setClass ( 'MyTestService' );
	$autodiscover-&gt;handle ();
} else {
	require 'Zend/Soap/Server.php';

	$url = URL;
	$server = new Zend_Soap_Server ( "http://localhost/SoapWebService.php?wsdl" );
	$server-&gt;setClass ( 'MyTestService' );
	echo $server-&gt;handle ();
}</pre>
<p>What we can see here is the possibility to generate the WSDL file automaticaly. The class Zend_Soap_AutoDiscover is doing this for us. We have just to set the class that we use as service. The WSDL is now accessible under the URL <a href="http://localhost/SoapWebService.php?wsdl">http://localhost/SoapWebService.php?wsdl</a>.</p>
<h2>Client</h2>
<p>Accessing the services is as easy as creating them.</p>
<p>index.php</p>
<pre class="brush:php">&lt;?php

require 'Zend/Rest/Client.php';
require 'Zend/XmlRpc/Client.php';
require 'Zend/Soap/Client.php';

try {

	// Rest
	$client = new Zend_Rest_Client ( "http://localhost/RestService.php" );
	$result = $client-&gt;ping ( 'test' )-&gt;get ();
	echo 'Rest:&lt;br/&gt;';
	var_dump ( $result );

	// XmlRpc
	$client = new Zend_XmlRpc_Client ( "http://localhost/XmlRpcService.php" );
	$result= $client-&gt;call('ping', array('test'));
	echo '&lt;br/&gt;&lt;br/&gt;XmlRpc:&lt;br/&gt;';
	var_dump ( $result );

	// Soap
	$client = new Zend_Soap_Client ( "http://localhost/SoapService.php?wsdl" );
	$result = $client-&gt;ping ( 'test' );
	echo '&lt;br/&gt;&lt;br/&gt;Soap:&lt;br/&gt;';
	var_dump ( $result );
} catch ( Exception $e ) {
	echo '&lt;b style="color:#FF0000;"&gt;Error: ' . $e-&gt;getMessage () . '&lt;/b&gt;';
}</pre>
<p>As you can see, the Rest- and the Soap service are a bit more intuitive to use than the XmlRpc service. Which one is best depends on the requirements and the use of the service.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">
<div class="content_full">
<div class="bText">
<p>With the Zend framework you can choose between <strong>Rest</strong>-, <strong>XmlRpc</strong>- and <strong>Soap</strong>-Webservices. I am going to describe here all of them with a simple ping service.</p>
<p>For all of them you need first a function or a class which you like to expose as service.</p>
<p>MyTestService.php</p>
<pre class="brush:php">&lt;?php

class MyTestService {

<span style="color: #008000;">/**

* A simple ping service

*

* @param string $value

* @return string

*/</span>

function ping($value){

return $value .' back from server';

}

}

It is quite important to add the documentation with the exact datatype for all parameters as well as for the return value to the function. This information is used by the Zend framework to parse the values e.g. for the WSDL generation.

<span style="font-size: large;"><strong>Rest Service</strong></span>

<code>RestService.php</code>

<code><span style="color: #000080;">&lt;?php</span></code>

require 'Zend/Rest/Server.php';

require_once 'MyTestService.php';

$server = new Zend_Rest_Server();

$server-&gt;setClass('MyTestService');

$server-&gt;handle();

<span style="font-size: large;"><strong>XmlRpc Service</strong></span>

<code>XmlRpcService.php</code>

<code><span style="color: #000080;">&lt;?php</span></code>

require 'Zend/XmlRpc/Server.php';

require_once 'MyTestService.php';

$server = new Zend_XmlRpc_Server();

$server-&gt;setClass('MyTestService');

echo $server-&gt;handle();

<strong>Caution</strong>: the echo on the last line is here essential!

<span style="font-size: large;"><strong>Soap Service</strong></span>

<code>SoapWebService.php</code>

<code><span style="color: #000080;">&lt;?php</span></code>

require 'MyTestService.php';

if(isset($_GET['wsdl'])) {

require 'Zend/Soap/AutoDiscover.php';

$autodiscover = new Zend_Soap_AutoDiscover();

$autodiscover-&gt;setClass('MyTestService');

$autodiscover-&gt;handle();

} else {

require 'Zend/Soap/Server.php';

$url = URL;

$server = new Zend_Soap_Server("http://localhost/SoapWebService.php?wsdl");

$server-&gt;setClass('MyTestService');

echo $server-&gt;handle();

}

What we can see here is the possibility to generate the WSDL file automaticaly. The class Zend_Soap_AutoDiscover is doing this for us. We have just to set the class that we use as service. The WSDL is now accessible under the URL <a href="http://localhost/SoapWebService.php?wsdl">http://localhost/SoapWebService.php?wsdl</a>.

<span style="font-size: large;"><strong>Client</strong></span>

Accessing the services is as easy as creating them.

<code>index.php</code>

<span style="color: #000080;"><code>&lt;?php</code></span>

require 'Zend/Rest/Client.php';

require 'Zend/XmlRpc/Client.php';

require 'Zend/Soap/Client.php';

try {

<span style="color: #008000;">// Rest</span>

$client = new Zend_Rest_Client ( "http://localhost/RestService.php" );

$result = $client-&gt;ping('test')-&gt;get();

echo 'Rest:&lt;br/&gt;';

var_dump(<code>$result</code><code>);</code>

<span style="color: #008000;">// XmlRpc</span>

$client = new Zend_XmlRpc_Client ( "<code><a href="http://localhost/">http://localhost</a></code><code>/XmlRpcService.php" );

</code><code>$result</code><code>= $client-&gt;call(''ping'', array('test'));

echo '&lt;br/&gt;&lt;br/&gt;XmlRpc:&lt;br/&gt;';

var_dump(</code><code>$result</code><code>);</code>

<span style="color: #008000;">// Soap</span>

$client = new Zend_Soap_Client("<code><a href="http://localhost/">http://localhost</a></code><code>/SoapService.php?wsdl");

</code><code>$result </code><code>= $client-&gt;ping('test');

echo '&lt;br/&gt;&lt;br/&gt;Soap:&lt;br/&gt;';

var_dump(</code><code>$result</code><code><span style="color: #000080;">);

} catch ( Exception $e ) {

echo '&lt;b style="color:#FF0000;"&gt;Error: ' . $e-&gt;getMessage () . '&lt;/b&gt;';

}</span>

</code>

As you can see, the Rest- and the Soap service are a bit more intuitive to use than the XmlRpc service. Which one is best depends on the requirements and the use of the service.</pre>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.simonstahl.com/2009/11/14/php-webservices-wth-the-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

