<?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>Joel Hainley &#187; soap server</title>
	<atom:link href="http://www.joelhainley.com/index.php/tag/soap-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joelhainley.com</link>
	<description>my thoughts and adventures</description>
	<lastBuildDate>Mon, 19 Dec 2011 19:34:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Soap &#8211; A Simple PHP Web Service Example</title>
		<link>http://www.joelhainley.com/index.php/2008/03/03/php-soap-a-simple-php-web-service-example/</link>
		<comments>http://www.joelhainley.com/index.php/2008/03/03/php-soap-a-simple-php-web-service-example/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 18:50:14 +0000</pubDate>
		<dc:creator>joelhainley</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[php soap example]]></category>
		<category><![CDATA[php web services]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[soap client]]></category>
		<category><![CDATA[soap server]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://www.joelhainley.com/index.php/2008/03/03/php-soap-a-simple-php-web-service-example/</guid>
		<description><![CDATA[There&#8217;s no doubt that today web services are hot, all of the commercial platforms ( J2EE, .Net, etc ) have tools that make the development of web services very easy. There are also a bunch of tools that are out there that help people generate their WSDL&#8217;s and can generally ease all of the difficult [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s no doubt that today web services are hot, all of the commercial platforms ( J2EE, .Net, etc ) have tools that make the development of web services very easy. There are also a bunch of tools that are out there that help people generate their WSDL&#8217;s and can generally ease all of the difficult parts of developing web services. The great thing is that with the release of PHP 5 we now have tools that allow us to quickly develop web services in PHP. The great thing about this is that you can stub out api&#8217;s and start to have different solutions speak easily among themselves, share business logic to languages and technologies that are &#8220;not native&#8221;.</p>
<p>Within this article I&#8217;m just going to hit some highlights about creating your own web services using SOAP and PHP. I will also provide a URL to a web service that I have written for people to fiddle around with their first SOAP client. Please note that I&#8217;m not providing an exhaustive example of all of the details of wsdl&#8217;s/SOAP and the like, this is the SOAP equivalent of a &#8220;hello world&#8221; program, but with parameter passing included since most  &#8220;Hello Worlds&#8221; don&#8217;t have parameters passed into them.  We&#8217;re going to be building a little service that will support 2 operations, rot13 of a supplied string, and the mirroring(reversing) of a supplied string. Nothing terribly fancy, but certainly more interesting than &#8220;hello world&#8221; <img src='http://www.joelhainley.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>You can see the<a href="http://www.joelhainley.com/examples/soap/scramble.wsdl" > WSDL for this service here</a> . There isn&#8217;t much to note with the WSDL it is a simple service and I&#8217;m not trying to get too crazy here. So take a look at it and see if it makes sense, if not, try to compare it against the code samples below and see if you can determine how it all relates.</p>
<p>Now that we have the WSDL defined it&#8217;s time to build the SOAP server to support this WSDL. Here&#8217;s the full listing for the server :</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">function</span> getRot13<span class="br0">&#40;</span><span class="re0">$pInput</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
<span class="re0">$rot</span> = <a href="http://www.php.net/str_rot13" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">str_rot13</span></a><span class="br0">&#40;</span><span class="re0">$pInput</span><span class="br0">&#41;</span>;return<span class="br0">&#40;</span><span class="re0">$rot</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw2">function</span> getMirror<span class="br0">&#40;</span><span class="re0">$pInput</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
<span class="re0">$mirror</span> = <a href="http://www.php.net/strrev" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">strrev</span></a><span class="br0">&#40;</span><span class="re0">$pInput</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">return</span><span class="br0">&#40;</span><span class="re0">$mirror</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="co1">// turn off the wsdl cache</span><br />
<a href="http://www.php.net/ini_set" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">ini_set</span></a><span class="br0">&#40;</span><span class="st0">&#8220;soap.wsdl_cache_enabled&#8221;</span>, <span class="st0">&#8220;0&#8243;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$server</span> = <span class="kw2">new</span> SoapServer<span class="br0">&#40;</span><span class="st0">&#8220;scramble.wsdl&#8221;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$server</span>-&amp;gt;addFunction<span class="br0">&#40;</span><span class="st0">&#8220;getRot13&#8243;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$server</span>-&amp;gt;addFunction<span class="br0">&#40;</span><span class="st0">&#8220;getMirror&#8221;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$server</span>-&amp;gt;handle<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</div>
<p>Pretty easy to understand, you&#8217;ll notice that we are just defining the functions at the top, then once we have created the soap server using the WSDL we simply associate our php functions with the functions defined in the WSDL by passing the function name to the addFunction message of the newly created SOAP server. Note that we have turned off the caching of the WSDL, this is something you&#8217;ll wanna do while you&#8217;re developing. When you don&#8217;t do this and make changes to your WSDL and they never seem to take, it can get frustrating <img src='http://www.joelhainley.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>Ok with the server out of the way let&#8217;s turn our attention to the client. Here&#8217;s the listing for a simple client :</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co1">// turn off the WSDL cache</span><br />
<a href="http://www.php.net/ini_set" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">ini_set</span></a><span class="br0">&#40;</span><span class="st0">&#8220;soap.wsdl_cache_enabled&#8221;</span>, <span class="st0">&#8220;0&#8243;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$client</span> = <span class="kw2">new</span> SoapClient<span class="br0">&#40;</span><span class="st0">&#8220;http://www.joelhainley.com/examples/soap/scramble.wsdl&#8221;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$origtext</span> = <span class="st0">&#8220;mississippi&#8221;</span>;</p>
<p><a href="http://www.php.net/print" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">print</span></a><span class="br0">&#40;</span><span class="st0">&#8220;The original text : $origtext<br />
&#8220;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$scramble</span> = <span class="re0">$client</span>-&amp;gt;getRot13<span class="br0">&#40;</span><span class="re0">$origtext</span><span class="br0">&#41;</span>;</p>
<p><a href="http://www.php.net/print" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">print</span></a><span class="br0">&#40;</span><span class="st0">&#8220;The scrambled text : $scramble<br />
&#8220;</span><span class="br0">&#41;</span>;</p>
<p><span class="re0">$mirror</span> = <span class="re0">$client</span>-&amp;gt;getMirror<span class="br0">&#40;</span><span class="re0">$scramble</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/print" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.php.net');"><span class="kw3">print</span></a><span class="br0">&#40;</span><span class="st0">&#8220;The mirrored text : $mirror<br />
&#8220;</span><span class="br0">&#41;</span>;</div>
</div>
<p>I&#8217;m sure you could write a fancier example, but it gets the point across. First, You turn off the WSDL caching on your client as well as the server. Then you create a new soap client using the wsdl. Then you just start calling methods that you defined in the server. That&#8217;s about all there is to it.</p>
<p>Note, I&#8217;m going to keep the Soap Server referenced in this article up for you all to use. Please feel free to use it for testing your clients as you&#8217;re getting up and running. Eventually you&#8217;ll wanna start using your own servers for more interesting things though <img src='http://www.joelhainley.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.joelhainley.com/index.php/2008/03/03/php-soap-a-simple-php-web-service-example/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

