<?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; PHP</title>
	<atom:link href="http://blog.simonstahl.com/tag/prog_php/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>Access a Zend Rest Service from C#</title>
		<link>http://blog.simonstahl.com/2009/11/14/access-a-zend-rest-service-from-c/</link>
		<comments>http://blog.simonstahl.com/2009/11/14/access-a-zend-rest-service-from-c/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 19:30:22 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://simonstahl.com/wordpress/?p=32</guid>
		<description><![CDATA[Accessing a Restful service is actually no big deal, but it took me quite a while to figure out some details. So here&#8217;s a short description of it. First of all, we build a small Zend Rest Service with two ping methods and save it as MyRestService.php. &#60;?php class MyRestService { /** * @param string [...]]]></description>
			<content:encoded><![CDATA[<p>Accessing a Restful service is actually no big deal, but it took me quite a while to figure out some details. So here&#8217;s a short description of it.<br />
<span id="more-32"></span><br />
First of all, we build a small Zend Rest Service with two ping methods and save it as MyRestService.php.</p>
<pre class="brush:php">&lt;?php

class MyRestService {
 /**
 * @param string $name
 * @return string
 */
 public function ping($name) {
 return 'Hello ' . $name;
 }
}

require 'Zend/Rest/Server.php';

$server = new Zend_Rest_Server ( );
$server-&gt;setClass ( 'MyRestService' );
$server-&gt;handle();

require 'Zend/Rest/Server.php';

$server = new Zend_Rest_Server ( );
$server-&gt;setClass ( 'MyRestService' );
$server-&gt;handle();</pre>
<p>We can now allready access our service with the browser by entering the URL <a href="http://localhost/MyRestService.php?method=ping&amp;name=Simon">http://localhost/MyRestService.php?method=ping&amp;name=Simon</a>. The service returns the answer as XML.</p>
<pre class="brush:xml">&lt;MyRestService generator="zend" version="1.0"&gt;
 &lt;ping&gt;
 &lt;response&gt;Hello Simon&lt;/response&gt;
 &lt;status&gt;success&lt;/status&gt;
 &lt;/ping&gt;
&lt;/MyRestService&gt;</pre>
<p>As you can see the call as well as the answer are really straightforward and easy to unserstand. We&#8217;re going now to access our service from C#.</p>
<pre class="brush:c#">public void CallRest() {
 string methodName = "ping";
 string name = "Simon";

 // Build the URL with the parameters
 StringBuilder sb = new StringBuilder();
 sb.Append("http://localhost/MyRestService.php");
 sb.Append(string.Format("?method={0}", HttpUtility.UrlEncode(methodName)));
 sb.Append(string.Format("&amp;name={0}", HttpUtility.UrlEncode(name)));

 // Download the service answer as string
 Uri address = new Uri(sb.ToString());
 WebClient serviceClient = new WebClient();
 serviceClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(serviceClient_DownloadStringCompleted);
 serviceClient.DownloadStringAsync(address);
}

private void serviceClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
 try {
 // Check for service error
 if (e.Error != null)
 throw e.Error;

 // Load the result as XML for easy Linq access
 XElement elem = XElement.Parse(e.Result);

 // Check the return status
 string status = (string)(from el in elem.Descendants("status") select el).First();

 if ("success".Equals(status)) {
 // Get the response
 string resp = (string)(from el in elem.Descendants("response") select el).First();
 MessageBox.Show(resp, "The Restservice says...", MessageBoxButton.OK);

 } else if ("failed".Equals(status)) {
 // Get the error message
 string errmsg = (string)(from el in elem.Descendants("message") select el).First();
 throw new Exception(errmsg);

 } else {
 throw new Exception(string.Format("Unknown Rest status: {0}", status));
 }
 } catch (Exception exp) {
 MessageBox.Show(exp.ToString(), "Error", MessageBoxButton.OK);
 }
}</pre>
<p>When we run the code a MessageBox with the service response should pop up.</p>
<p>So, this was the part I figured out quite fast. But what happens if you change the service function so somewhat like this:</p>
<pre class="brush:php">/**
* @param array $names
* @return string
*/
public function ping($names){
 return $names['p2'];
}</pre>
<p>It took me some time to figure out that the answer is quite easy. Let&#8217;s change the C# function to fit the array parameter.</p>
<pre class="brush:c#">public void CallRest() {
 string methodName = "ping";
 string[] names = { "Simon", "John", "Bill" };

 // Build the URL with the parameters
 StringBuilder sb = new StringBuilder();
 sb.Append("http://localhost/MyRestService.php");
 sb.Append(string.Format("?method={0}", HttpUtility.UrlEncode(methodName)));

 for (int i = 0; i &lt; names.Length; i++)
 sb.Append(string.Format("&amp;names[p{0}]={1}", i, HttpUtility.UrlEncode(names[i])));

 // Download the service answer as string
 Uri address = new Uri(sb.ToString());
 WebClient serviceClient = new WebClient();
 serviceClient.DownloadStringCompleted += new
 DownloadStringCompletedEventHandler(serviceClient_DownloadStringCompleted);
 serviceClient.DownloadStringAsync(address);
}</pre>
<p>As you can see I just add the parameter as everybody would access an array. And once again, you can also access the service direct with your browser unter the URL <a href="http://localhost/MyRestService.php?method=ping&amp;names[p0]=Simon&amp;names[p1]=John&amp;names[p2]=Bill">http://localhost/MyRestService.php?method=ping&amp;names[p0]=Simon&amp;names[p1]=John&amp;names[p2]=Bill</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.simonstahl.com/2009/11/14/access-a-zend-rest-service-from-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

