<?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>Dimuthu's Blog &#187; php</title>
	<atom:link href="http://www.dimuthu.org/catagory/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>WSO2 Web Services Framework for PHP 2.1.0 Released</title>
		<link>http://www.dimuthu.org/blog/2010/07/07/wso2-web-services-framework-for-php-2-1-0-released/</link>
		<comments>http://www.dimuthu.org/blog/2010/07/07/wso2-web-services-framework-for-php-2-1-0-released/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 12:42:52 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=1099</guid>
		<description><![CDATA[WSO2 just released a newer version of the popular web services framework for php (WSF/PHP). It has lot of bug fixes + performance improvement.  And it now support PHP 5.3. So you can use wsf/php with the latest php version. You can download the new release from http://wso2.org/downloads/wsf/php.]]></description>
			<content:encoded><![CDATA[<p>WSO2 just released a newer version of the popular web services framework for php (WSF/PHP). It has lot of bug fixes + performance improvement.  And it now support PHP 5.3. So you can use wsf/php with the latest php version.</p>
<p>You can download the new release from <a href="http://wso2.org/downloads/wsf/php">http://wso2.org/downloads/wsf/php</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2010/07/07/wso2-web-services-framework-for-php-2-1-0-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WSF/PHP Code First Approach: Returning an Array of String</title>
		<link>http://www.dimuthu.org/blog/2010/01/08/wsfphp-code-first-approach-returning-an-array-of-string/</link>
		<comments>http://www.dimuthu.org/blog/2010/01/08/wsfphp-code-first-approach-returning-an-array-of-string/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 02:05:12 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml schema]]></category>
		<category><![CDATA[code first]]></category>
		<category><![CDATA[wsdl generation]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=1060</guid>
		<description><![CDATA[Here is a problem that many people have asked me how to do it. &#8220;Returning an array of string&#8221; with the code first approach. The API or WSDL generation annotation guide, http://wso2.org/project/wsf/php/2.0.0/docs/wsdl_generation_api.html contain all the things required in details. Here is an example of a service that return an array of string. &#60;?php /** splitMe [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a problem that many people have asked me how to do it. &#8220;Returning an array of string&#8221; with the code first approach. The API or WSDL generation annotation guide, <a href="http://wso2.org/project/wsf/php/2.0.0/docs/wsdl_generation_api.html">http://wso2.org/project/wsf/php/2.0.0/docs/wsdl_generation_api.html</a> contain all the things required in details. Here is an example of a service that return an array of string.</p>
<pre><span style="color: #000000; font-weight: bold;">&lt;?php</span>

<span style="color: #808080; font-style: italic;">/** splitMe function
 * @param string $string_to_split string to split
 * (maps to the xs:string XML schema type )
 * @return array of string $result split to array
 *(maps to the xs:double XML schema type )
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> splitMe<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$string_to_split</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"result"</span> =&gt; <a href="http://www.php.net/split"><span style="color: #000066;">split</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">":"</span>, <span style="color: #0000ff;">$string_to_split</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #66cc66;">}</span>

<span style="color: #0000ff;">$operations</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"splitMe"</span>=&gt;<span style="color: #ff0000;">"splitMe"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$opParams</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"splitMe"</span>=&gt;<span style="color: #ff0000;">"MIXED"</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span> = <span style="color: #000000; font-weight: bold;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"operations"</span>=&gt;<span style="color: #0000ff;">$operations</span>,
                           <span style="color: #ff0000;">"bindingStyle"</span>=&gt;<span style="color: #ff0000;">"doclit"</span>,
                           <span style="color: #ff0000;">"opParams"</span>=&gt;<span style="color: #0000ff;">$opParams</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$svr</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
</pre>
<p>Note that the annotation corresponding to the return value.</p>
<pre> * @return array of spring $result split to array
</pre>
<p>This will generate an schema with an element of maxOccurs=&#8217;unbounded&#8217;. Note that you can get the wsdl from the &#8216;serviceurl?wsdl&#8217;.</p>
<pre><span style="font-weight: bold; color: black;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"splitMeResponse"</span><span style="font-weight: bold; color: black;">&gt;</span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xsd:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xsd:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"result"</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">"unbounded"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"xsd:string"</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xsd:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xsd:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xsd:element<span style="font-weight: bold; color: black;">&gt;</span>
</span></span></pre>
<p>Now just generate a client for this service using wsdl2php and try invoke it. You will get an array of string as the response.</p>
<pre>    <span style="color: #0000ff;">$input</span> = <span style="color: #000000; font-weight: bold;">new</span> splitMe<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$input</span>-&gt;<span style="color: #006600;">string_to_split</span> = <span style="color: #ff0000;">"a:b:c:d"</span>;

 
    <span style="color: #808080; font-style: italic;">// call the operation</span>
    <span style="color: #0000ff;">$response</span> = <span style="color: #0000ff;">$proxy</span>-&gt;<span style="color: #006600;">splitMe</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$input</span><span style="color: #66cc66;">)</span>;
    <a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$response</span><span style="color: #66cc66;">)</span>;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2010/01/08/wsfphp-code-first-approach-returning-an-array-of-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting the size of BLOB in MySql</title>
		<link>http://www.dimuthu.org/blog/2010/01/06/getting-the-size-of-blob-in-mysql/</link>
		<comments>http://www.dimuthu.org/blog/2010/01/06/getting-the-size-of-blob-in-mysql/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 02:53:32 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[OCTET_LENGTH]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=1049</guid>
		<description><![CDATA[If you want to store binary in database, you can use BLOB as the data type of that column. In Mysql you can use TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB depending on your space requirement. Here is an example of database table using BLOB as a column type. CREATE TABLE BloBTest ( id INT NOT NULL AUTO_INCREMENT, [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to store binary in database, you can use <a href="http://dev.mysql.com/doc/refman/5.0/en/blob.html">BLOB</a> as the data type of that column. In Mysql you can use TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB depending on your space requirement. Here is an example of database table using BLOB as a column type.</p>
<pre><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> BloBTest <span style="color: #66cc66;">(</span>
    id INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>,
    filename VARCHAR<span style="color: #66cc66;">(</span> <span style="color: #cc66cc;">32</span> <span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
    content BLOB <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
    <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">(</span> id <span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span></pre>
<p><strong>Storing Data</strong></p>
<p><strong>PHP:</strong></p>
<pre><span style="color: #0000ff;">$filename</span> = <span style="color: #ff0000;">"myimage.png"</span>;
<span style="color: #0000ff;">$filecontent</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$filecontent_escaped</span> = <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #000066;">mysql_real_escape_string</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$filecontent</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"INSERT INTO BloBTest(filename, content) "</span> +
       <span style="color: #ff0000;">"VALUES('$filename','$filecontent_escaped')"</span>;
<a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span>, <span style="color: #0000ff;">$link</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong> Java:</strong></p>
<pre><span style="color: #aaaadd; font-weight: bold;">String</span> filename = <span style="color: #ff0000;">"myimage.png"</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> filecontent = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">FileInputStream</span></a><span style="color: #66cc66;">(</span>filename<span style="color: #66cc66;">)</span>;

<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> sql = <span style="color: #ff0000;">"INSERT INTO BloBTest(filename, content) VALUES(?, ?)"</span>;

<span style="color: #993333;">int</span> size = filecontent.<span style="color: #006600;">available</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PreparedStatement</span></a> ps = conn.<span style="color: #006600;">prepareStatement</span><span style="color: #66cc66;">(</span>sql<span style="color: #66cc66;">)</span>;
ps.<span style="color: #006600;">setString</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">1</span>, filename<span style="color: #66cc66;">)</span>;
ps.<span style="color: #006600;">setBinaryStream</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">2</span>, filecontent, size<span style="color: #66cc66;">)</span>;
ps.<span style="color: #006600;">executeUpdate</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<p><strong>Retrieving Data</strong></p>
<p><strong>PHP</strong></p>
<pre><span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"SELECT filename, content FROM BloBTest"</span>;
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span>, <span style="color: #0000ff;">$link</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #000066;">mysql_fetch_assoc</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

    <span style="color: #0000ff;">$filename</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">"filename"</span><span style="color: #66cc66;">]</span>;
    <span style="color: #0000ff;">$content</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">"content"</span><span style="color: #66cc66;">]</span>;
    <span style="color: #0000ff;">$new_filename</span> = <span style="color: #ff0000;">"new_"</span> . <span style="color: #0000ff;">$filename</span>;
    file_put_contents<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$new_filename</span>, <span style="color: #0000ff;">$content</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span></pre>
<p><strong>Java</strong>:</p>
<pre><span style="color: #aaaadd; font-weight: bold;">String</span> sql = <span style="color: #ff0000;">"SELECT filename, content FROM BloBTest"</span>;

PrepareStatement ps  = conn.<span style="color: #006600;">prepareStatement</span><span style="color: #66cc66;">(</span>resourceContentSQL<span style="color: #66cc66;">)</span>;
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ResultSet</span></a> result = ps.<span style="color: #006600;">executeQuery</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;

<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>result.<span style="color: #006600;">next</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">{</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> filename = result.<span style="color: #006600;">getString</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"filename"</span><span style="color: #66cc66;">)</span>;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> contentStream = result.<span style="color: #006600;">getBinaryStream</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"content"</span><span style="color: #66cc66;">)</span>;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> newFilename = <span style="color: #ff0000;">"new_"</span> + filename;
    <span style="color: #808080; font-style: italic;">// storing the input stream in the file</span>

    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">OutputStream</span></a> out=<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AFileOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">FileOutputStream</span></a><span style="color: #66cc66;">(</span>newFilename<span style="color: #66cc66;">)</span>;
    <span style="color: #993333;">byte</span> buf<span style="color: #66cc66;">[</span><span style="color: #66cc66;">]</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">1024</span><span style="color: #66cc66;">]</span>;
    <span style="color: #993333;">int</span> len;
    <span style="color: #b1b100;">while</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span>len=contentStream.<span style="color: #006600;">read</span><span style="color: #66cc66;">(</span>buf<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>&gt;<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span>

    out.<span style="color: #006600;">write</span><span style="color: #66cc66;">(</span>buf,<span style="color: #cc66cc;">0</span>,len<span style="color: #66cc66;">)</span>;
    out.<span style="color: #006600;">close</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span></pre>
<p><strong>Retrieving the Size of the Blob</strong></p>
<p>After you store your data as a blob, you can manipulate or query the data with some of the in-built <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html">String functions</a> in mysql. For an example if you want to query the size of the blob you just stored, you can use <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_octet-length">OCTET_LENGTH</a> function. Here is an example,  (this will give you the size in bytes.)</p>
<pre><span style="color: #993333; font-weight: bold;">SELECT</span> OCTET_LENGTH<span style="color: #66cc66;">(</span>content<span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">FROM</span> BloBTest <span style="color: #993333; font-weight: bold;">WHERE</span> filename=<span style="color: #ff0000;">'myimage.png'</span></pre>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 228px; width: 1px; height: 1px;">
<pre>.</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2010/01/06/getting-the-size-of-blob-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Case Study &#8211; PHP Data Services To Extract Content from Drupal Database</title>
		<link>http://www.dimuthu.org/blog/2009/02/13/case-study-php-data-services-to-extract-content-from-drupal-database/</link>
		<comments>http://www.dimuthu.org/blog/2009/02/13/case-study-php-data-services-to-extract-content-from-drupal-database/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 16:40:11 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[case study]]></category>
		<category><![CDATA[data services]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[mashup server]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=930</guid>
		<description><![CDATA[In this case study &#8220; PHP Data Services Extract Content from Drupal Database&#8220;, I intended to present how Data Service concepts can be applied to extract data with marketing value from  a CMS database and publish it as web services.  I used the drupal instance deployed at http://wso2.org as the CMS for the use case. [...]]]></description>
			<content:encoded><![CDATA[<p>In this case study &#8220;<a href="http://wso2.org/library/case-studies/case-study-php-data-services-extract-drupal-database"> PHP Data Services Extract Content from Drupal Database</a>&#8220;, I intended to present how Data Service concepts can be applied to extract data with marketing value from  a CMS database and publish it as web services.  I used the drupal instance deployed at <a href="http://wso2.org">http://wso2.org</a> as the CMS for the use case. And as the data service framework, I used <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> data services library, as it requires minimum changes to the existing infrastructure (the LAMP stack).</p>
<p>The case study also talks about how to consume the data service by any third party mashup to present textual/ graphical views of analyzed data. These mashups can be extended up to intergreate with social networks like facebook, twitter and etc to communicate back and forth a wider community and may be it can be used to track the distribution of active users using Google maps. Simply it makes easier to analyze business data + engagement with the community.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/02/13/case-study-php-data-services-to-extract-content-from-drupal-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Article Introducing PHP Data Services</title>
		<link>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 13:06:55 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[WS-*]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=900</guid>
		<description><![CDATA[Now you can view the article I wrote titling &#8220;Introduction to PHP Data Services&#8220;. There I explain how you can design and implement Data Services in PHP using WSF/PHP Data Services Library. This article covers, Designing your Data Service API. Writing the Data Service. Deploying and Testing Data Service. Make the Data Service available in [...]]]></description>
			<content:encoded><![CDATA[<p>Now you can view the article I wrote titling &#8220;<a href="http://wso2.org/library/articles/introduction-php-data-services">Introduction to PHP Data Services</a>&#8220;. There I explain how you can design and implement Data Services in PHP using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> Data Services Library.</p>
<p>This article covers,</p>
<ol>
<li>Designing your Data Service API.</li>
<li>Writing the Data Service.</li>
<li>Deploying and Testing Data Service.</li>
<li>Make the Data Service available in both SOAP and RESTful form.</li>
<li>Use of WS-* features in your Data Service.</li>
</ol>
<p>If you are thinking of adapting SOA in to your database backed PHP applications, this article will be a good starting point.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RESTful PHP Web Services &#8211; Book Review</title>
		<link>http://www.dimuthu.org/blog/2009/01/01/restful-php-web-services-book-review/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/01/restful-php-web-services-book-review/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 05:22:46 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[clients]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=865</guid>
		<description><![CDATA[PHP is one of the famous choice, when it comes to develop a web site. As the web evolve with the emerge of web service, REST (REpresentational State Transfer) concepts, the PHP language is also adapted to the new requirements specially with the availability of new SOA (Service Oriented Architecture), REST frameworks and libraries. Anyway [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is one of the famous choice, when it comes to develop a web site. As the web evolve with the emerge of web service, REST (REpresentational State Transfer) concepts, the PHP language is also adapted to the new requirements specially with the availability of new SOA (Service Oriented Architecture), REST frameworks and libraries. Anyway there were hardly any guides, references or samples that properly describe the methodologies of developing REST applications using PHP.</p>
<p>The book &#8220;<a href="http://www.packtpub.com/restful-php-web-services/book">RESTful PHP Web Services</a>&#8216; by <a href="http://samisa-abeysinghe.blogspot.com/">Samisa Abeysinghe</a> certainly fill this gap. It can be used as a step by step guideline for newbies to learn the concepts and write simple RESTful PHP applications and Mashups. And even experienced developers would find this a great reference to keep nearby while working with RESTful Web Services in PHP. And it has lot of code samples, utility functions that developers can use it in their applications.</p>
<p><a href="http://www.packtpub.com/restful-php-web-services/book"><img title="RESTful PHP Web Services - Samisa Abeysinghe" src="http://images.packtpub.com/images/full/1847195520.jpg" alt="RESTful PHP Web Services - Samisa Abeysinghe" width="200" /></a></p>
<p><strong>About the Author</strong></p>
<p><a href="http://samisa-abeysinghe.blogspot.com/">Samisa Abeysinghe</a> is a well recognized name in the web services world. He lead the development of <a href="http://ws.apache.org/axis2/c/">Apache Axis2/C</a> and <a href="http://wso2.org/projects/wsf/php">WSO2 WSF/PHP</a>, two famous open source web service frameworks for &#8216;C&#8217; and PHP. In addition to his deep knowledge in the subject, his experience in involving with the community and the enterprise for years and working as a lecturer in universities, should have influenced a lot in writing this book.</p>
<p><strong>The Arrangement and the Content</strong></p>
<p>The <a href="http://www.packtpub.com/article/restful-php-web-services-table-of-contents">arrangement of the book</a> is done really well to make sure the reader can go through it in the right sequence. All the content is bundled just within 200 pages. So you don&#8217;t need to allocate a lot of time to go through the whole book. It is organized into 7 chapters and two appendixes which are mostly independent from each other.</p>
<p>The first chapter is completely devoted to explain the concepts of RESTful web services. It basically explains what is RESTful web service and why it is needed. And it briefly mentions about the currently available REST Frameworks for PHP.</p>
<p>The second chapter introduce some PHP codes that do REST web service requests and handles the XML responses using both DOM and SimpleXML APIs. And in the third chapter it shows more code samples specially about consuming real world web services like BBC, Yahoo and an earthquakes information service. Theses codes are written as mashups mostly combining two services to produce more meaningful information.</p>
<p>The forth chapter is about  designing and writing web service providers. Its counterpart, writing web service consumers is described in the chapter five. There it demonstrate a library system that operate using RESTful webservices. You can map this example to any system that you may like to develop to run with RESTful web services.  The chapter five of the book is available as a free download, <a href="http://www.dimuthu.org/wp-content/uploads/2008/12/restful-php-webservices_sample-chapter.pdf">RESTful PHP Web Services &#8211; Chapter 5</a>.</p>
<p>The forth and fifth chapters are not using any framework to write the sample codes on consuming and providing web services. But in the sixth chapter it shows the use of <a href="http://framework.zend.com/">Zend framework</a> to do write them. There it rewrites the same example (The RESTful library system) in MVC (Model -View &#8211; Controller) approach using the functionalities of Zend framework. (In fact the View in the service is omitted).</p>
<p>The seventh chapter is about debugging web services. Debugging is a much needed step in any software development cycle. So if you are a newbie, you should read this chapter before start writing any of your own code. This introduces tools and methodologies to make your debugging easy and effective.</p>
<p>The book contains two appendixes. They are too really useful as the chapters of the book. In the first appendix it explains another REST web service framework, <a href="http://wso2.org/projects/wsf/php">WSO2 Web Services Framework for PHP (WSF/PHP)</a>. To demonstrate it uses, some selected functionalities of the example library system (that is mentioned in chapters 4, 5, 6) is re-implemented using WSF/PHP. And it shows you how you can convert this RESTful system to a SOAP system in a minute. The second appendix provides you a code of a class (RESTClient), that you can use in consuming web services very effectively.</p>
<p><strong>Recommended Readers<br />
</strong></p>
<p>This book assume you have some knowledge in PHP. But it doesn&#8217;t require you to know anything related to web services, REST or XML. As you read the first few chapters, you will have a good understanding on the concepts and the basic applications of REST and XML using PHP. And the later chapters will guide to get deeper knowledge in writing complex and real world applications.</p>
<p>If you are a professional developer, you can skip the introduction chapters and jump directly to where you need to refer. For an example, if you use this book as a reference in designing and developing RESTful web service providers, you can directly read the chapter4 &#8211; Resource Oriented Services, chapter6- Resource Oriented Clients and Services with Zend Framework and probably the chapter 7 &#8211; Debugging Web Services.</p>
<p>This book contains the same example system (the library system) written in three different approaches, first without using any framework support, second using the Zend Framework, third using WSF/PHP. Each of them has its own pros and cons. So if you want to determine the approach more suitable to your requirements, or thinking of migrating from one to another, this book will be an ideal resource for you.</p>
<p>As you may have already noticed, this book contains lot of code samples. All the concepts are followed by simple code samples that explain the concept. In appendix it gives you a complete code for RESTClient class that you can use to call any REST service. Apart from the code of the example library system written using different frameworks, it has lot of codes for calling public web service APIs. And the explanation of the code is also done really well.</p>
<p>So it is clear this book is more targetting readers who like to implement PHP RESTful Systems in practice. And it covers enough concepts that you needed to know in writing practicle applications. So this book can take you from the zero knowlege to a deeper knowlege of RESTful PHP Web Services.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/01/restful-php-web-services-book-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Web Services &#8211; Authentication Based on Client&#8217;s IP</title>
		<link>http://www.dimuthu.org/blog/2008/12/27/php-web-services-authentication-based-on-clients-ip/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/27/php-web-services-authentication-based-on-clients-ip/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 10:34:10 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[$_SERVER]]></category>
		<category><![CDATA[authenticate]]></category>
		<category><![CDATA[IP]]></category>
		<category><![CDATA[REMOTE_ADDR]]></category>
		<category><![CDATA[REMOTE_HOST]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=862</guid>
		<description><![CDATA[Explains with code samples how a PHP web service can be written to authenticate clients based on their IPs.]]></description>
			<content:encoded><![CDATA[<p>Same as web pages, web services also sometime require  client authentication. The most frequent way of authentication is the use of WS-Security Username token which authenticate clients based on the username and passwords. There can be situations where clients need to be authenticated based on its IP or its domain.</p>
<p>If you are writing web services from PHP (Using some PHP web service framework like <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a>), You can use the PHP variables, <em>$_SERVER["REMOTE_ADDR"]</em> and <em>$_SERVER["REMOTE_HOST"]</em> to find the clients ip within the service logic code. If the client&#8217;s IP is static you can directly use the $_SERVER["REMOTE_ADDR"] and if it is dynamic you can use the $_SERVER["REMOTE_HOST"] which will be derived by reverse DNS look of the clients IP.</p>
<p>Here is one example of the use of these $_SERVER[] variables inside service logic.</p>
<pre class="php"> 
<span style="color: #000000; font-weight: bold;">function</span> members_only_func<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$in_message</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

<span style="color: #808080; font-style: italic;">    // getting the clients IP.</span>
    <span style="color: #0000ff;">$remote_addr</span> = <span style="color: #0000ff;">$_SERVER</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">"REMOTE_ADDR"</span><span style="color: #66cc66;">]</span>;

    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$remote_addr</span> == <span style="color: #ff0000;">"67.205.26.154"</span> ||
       <span style="color: #0000ff;">$remote_addr</span> == <span style="color: #ff0000;">"124.43.59.95"</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
       <span style="color: #808080; font-style: italic;">// generates the message for authenticated clients.</span>

       <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$valid_out_message</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #808080; font-style: italic;">// otherwise throw an exception</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> WSFault<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Sender"</span>, <span style="color: #ff0000;">"Failed to Authenticate"</span><span style="color: #66cc66;">)</span>;
<span style="color: #66cc66;">}</span>

<span style="color: #0000ff;">$operations</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"membersOnlyOp"</span> =&gt; <span style="color: #ff0000;">"members_only_func"</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$service</span> = <span style="color: #000000; font-weight: bold;">new</span> WSService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$service</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/27/php-web-services-authentication-based-on-clients-ip/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>WordPress Database &#8211; Relationship with Tags, Catagories and Posts</title>
		<link>http://www.dimuthu.org/blog/2008/12/20/wordpress-database-relationship-with-tags-catagories-and-posts/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/20/wordpress-database-relationship-with-tags-catagories-and-posts/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 16:11:39 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[catgoary]]></category>
		<category><![CDATA[posts]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[taxonomy]]></category>
		<category><![CDATA[term]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=825</guid>
		<description><![CDATA[WordPress has a very simple database schema. And it is well documented. You can access the complete description of the wordpress core database from here, http://codex.wordpress.org/Database_Description. Anyway first time I looked at the database I was confused with the term and the term_taxonomy table, why we need two tables for term and taxonomies. In fact [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has a very simple database schema. And it is well documented. You can access the complete description of the wordpress core database from here, <a href="http://codex.wordpress.org/Database_Description">http://codex.wordpress.org/Database_Description</a>.</p>
<p>Anyway first time I looked at the database I was confused with the term and the term_taxonomy table, why we need two tables for term and taxonomies. In fact in wordpress, the table &#8216;posts&#8217; is associated with the table &#8216;term_taxonomy&#8217; and not the table &#8216;term&#8217; itself. In the term taxonomy table the terms are associated to a link category, post category or a tag. So the associations of posts to a tag or category is something like this.</p>
<div id="attachment_828" class="wp-caption alignnone" style="width: 510px"><a href="http://www.dimuthu.org/wp-content/uploads/2008/12/wp_term_post_association.jpeg"><img class="size-full wp-image-828" title="wp_term_post_association" src="http://www.dimuthu.org/wp-content/uploads/2008/12/wp_term_post_association.jpeg" alt="wp_term_post association" width="500" height="136" /></a><p class="wp-caption-text">wp_term_post association</p></div>
<p>So in a case you try querying for posts with a given tag it will be like this. (Note that I have skipped the optional database table prefix which is by default &#8216;wp_&#8217;)</p>
<pre class="sql"><span style="font-weight: bold; color: #993333;">SELECT</span> post_title,
       post_content,
       post_date
<span style="font-weight: bold; color: #993333;">FROM</span> posts p,
     terms t,
     term_relationships r,
     term_taxonomy tt
<span style="font-weight: bold; color: #993333;">WHERE</span> p.post_status=<span style="color: #ff0000;">'publish'</span> <span style="font-weight: bold; color: #993333;">AND</span>
      tt.taxonomy = <span style="color: #ff0000;">'post_tag'</span> <span style="font-weight: bold; color: #993333;">AND</span>
      p.id=r.object_id <span style="font-weight: bold; color: #993333;">AND</span>
      r.term_taxonomy_id=tt.term_taxonomy_id <span style="font-weight: bold; color: #993333;">AND</span>
      tt.term_id = t.term_id <span style="font-weight: bold; color: #993333;">AND</span> t.name <span style="font-weight: bold; color: #993333;">LIKE</span> ?</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/20/wordpress-database-relationship-with-tags-catagories-and-posts/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Data Services Best Practices</title>
		<link>http://www.dimuthu.org/blog/2008/12/18/data-services-best-practices/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/18/data-services-best-practices/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 17:39:48 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[data services]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[best practises]]></category>
		<category><![CDATA[coupling]]></category>
		<category><![CDATA[granularity]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[wsf/php]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=803</guid>
		<description><![CDATA[In simple term, data services are exposing data as web services. Anyway it is not a complete definition. Actually there are situations where we use data services not only to read data, but also to create, update or delete data. So it is better say data services are doing CRUD (Create, Read, Update, Delete) operations [...]]]></description>
			<content:encoded><![CDATA[<p>In simple term, data services are exposing data as web services. Anyway it is not a complete definition. Actually there are situations where we use data services not only to read data, but also to create, update or delete data. So it is better say data services are doing CRUD (Create, Read, Update, Delete) operations for data through web services. Simply it is like providing a web service interface for the database.</p>
<p>Anyway exposing  a database directly as a web service is like violating the first principles of software engineering. It will tightly couple the database structure with the interface, so whenever you do a simple change to the database schema, you will have to change the web service interface which will no doubt break all the clients depending on it.</p>
<p>So first of all, you have to design the service interface independent of the database schema you have. Most of the time you will able to find some query that would map the service interface to the database schema.</p>
<p>For an example think of publishing data in database table (say for table name &#8220;Games&#8221;) like this.</p>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="7">Teams</th>
</tr>
<tr style="background:#cccccc">
<td>GameID</td>
<td>Venue</td>
<td>Date</td>
<td>Team1</td>
<td>Team2</td>
<td>Team1Score</td>
<td>Team2Score</td>
</tr>
<tr>
<td>1</td>
<td>xxx stadium</td>
<td>2008-12-18</td>
<td>Italy</td>
<td>Sweden</td>
<td>34</td>
<td>33</td>
</tr>
<tr>
<td>2</td>
<td>yyy stadium</td>
<td>2008-12-19</td>
<td>France</td>
<td>Spain</td>
<td>51</td>
<td>50</td>
</tr>
</tbody>
</table>
<p>You will directly able to map these data to data service. So the response payload for a &#8220;getGames&#8221; operation would be something like,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;getGamesResponse<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         xxx stadium
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         2008-12-18
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Italy
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Sweden
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          34
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          33
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         yyy stadium
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         2008-12-19
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          France
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Spain
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          51
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          50
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/</span></span><span><span><span style="color: #009900;"><span style="font-weight: bold; color: black;">getGamesResponse</span></span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You can get this done with a SQL query simply as this,</p>
<pre class="sql"><span style="font-weight: bold; color: #993333;">SELECT</span> * <span style="font-weight: bold; color: #993333;">FROM</span> <span style="color: #ff0000;">`Games`</span></pre>
<p>Say later if you decided to restructure the database table so the new database schema would be like this,</p>
<pre>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="3">Games</th>
</tr>
<tr style="background:#cccccc">
<td>GameId</td>
<td>Venue</td>
<td>Date</td>
</tr>
<tr>
<td>1</td>
<td>xxx stadium</td>
<td>2008-12-18</td>
</tr>
<tr>
<td>1</td>
<td>yyy stadium</td>
<td>2008-12-19</td>
</tr>
</tbody>
</table>
</pre>
<pre>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="3">GamesTeams</th>
</tr>
<tr style="background:#cccccc">
<td>GameId</td>
<td>TeamId</td>
<td>score</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>70</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>33</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>51</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>50</td>
</tr>
</tbody>
</table>
</pre>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="3">Teams</th>
</tr>
<tr style="background:#cccccc">
<td>TeamId</td>
<td>Name</td>
<td>Coach</td>
</tr>
<tr>
<td>1</td>
<td>Italy</td>
<td>Mr. ABC</td>
</tr>
<tr>
<td>1</td>
<td>Canada</td>
<td>Mr. PQR</td>
</tr>
<tr>
<td>2</td>
<td>Spain</td>
<td>Mr. XYZ</td>
</tr>
</tbody>
</table>
<p><em>(Note here the Games and Teams are associated in the GamesTeams table.)</em></p>
<p>You can still use a query like the one in following to provide the same service interface, because it returns the same result set as the earlier one.</p>
<pre class="sql">   <span style="font-weight: bold; color: #993333;">SELECT</span> Games.Venue,
              Games.Date,
              Team1.Name <span style="font-weight: bold; color: #993333;">AS</span> Team1,
              Team2.Name <span style="font-weight: bold; color: #993333;">AS</span> Team2,
              GameTeam1.Score <span style="font-weight: bold; color: #993333;">AS</span> Score1,
              GameTeam2.Score <span style="font-weight: bold; color: #993333;">AS</span> Score2
         <span style="font-weight: bold; color: #993333;">FROM</span> Teams Team1,
              Teams Team2,
              GamesTeams GameTeam1,
              GamesTeams GameTeam2,
              Games
        <span style="font-weight: bold; color: #993333;">WHERE</span> GameTeam1.gameId = Games.gameId  <span style="font-weight: bold; color: #993333;">AND</span>
              GameTeam2.gameId = Games.gameId <span style="font-weight: bold; color: #993333;">AND</span>
              GameTeam1.teamId = Team1.teamId <span style="font-weight: bold; color: #993333;">AND</span>
              GameTeam2.teamId = Team2.teamId <span style="font-weight: bold; color: #993333;">AND</span>
              Team1.teamId &lt;&gt; Team2.teamId <span style="font-weight: bold; color: #993333;">AND</span>
              Team1.name=?</pre>
<p>This allows you to keep the service interface unchanged, regardless of the changes you done to the database schema.</p>
<p>In addition to that, we can uses the features of the data service libraries to give meaningful names for the response elements. If we take above example itself, say you want to rename &#8216;Score1&#8242; to &#8216;Team1-Score&#8217; and &#8216;Score2&#8242; to &#8216;Team2-Score&#8217;. But the dash character (&#8216;-&#8217;) cannot be used as a variable in a database query. But you can provide that in the map of sql name to element name when you are writing the data service.</p>
<p>If you are using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> php data services Here is how you provide that mapping,</p>
<pre class="php"><span style="color: #0000ff;">$outputFormat</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"resultElement"</span> =&gt; <span style="color: #ff0000;">"getGamesResponse"</span>,
                      <span style="color: #ff0000;">"rowElement"</span> =&gt; <span style="color: #ff0000;">"game"</span>,  <span style="font-style: italic; color: #808080;">// this is the repeating wrapper element for each game</span>
                      <span style="color: #ff0000;">"elements"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span> <span style="color: #ff0000;">"Venue"</span> =&gt; <span style="color: #ff0000;">"Venue"</span>, <span style="font-style: italic; color: #808080;">// this is the mapping of xml name =&gt; sql name</span>
                                           <span style="color: #ff0000;">"Date"</span> =&gt; <span style="color: #ff0000;">"Date"</span>,
                                           <span style="color: #ff0000;">"Team1"</span> =&gt; <span style="color: #ff0000;">"Team1"</span>,
                                           <span style="color: #ff0000;">"Team2"</span> =&gt; <span style="color: #ff0000;">"Team2"</span>,
                                           <span style="color: #ff0000;">"Team1-score"</span> =&gt; <span style="color: #ff0000;">"Score1"</span>, <span style="font-style: italic; color: #808080;">// we are using different names for sql and xml</span>
                                           <span style="color: #ff0000;">"Team2-score"</span> =&gt; <span style="color: #ff0000;">"Score2"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>If you write it using <a href="http://wso2.org/projects/solutions/data-services/java">Java Data Services</a>, you can use do this with the following configuration xml,</p>
<pre class="xml">        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;result</span> <span style="color: #000066;">element</span>=<span style="color: #ff0000;">"getGamesResponse"</span> <span style="color: #000066;">rowName</span>=<span style="color: #ff0000;">"game"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Venue"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Venu"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Date"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Date"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team1"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Team1"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team2"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Team2"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team1-Score"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Score1"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team2-Score"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Score2"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/result<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Another consideration, when talking about the best practices of designing web services or data services is the granularity of the service interface. We say a web service is fine grained if the service contains tons of small operations. The opposite of that is the coarse grained services, which contains large but few operations. And it is recommended to use the later approach, you can see why from the following example.</p>
<p>Lets say you are developing a web service to upload some information. In order to upload the information first the user have to be authenticated and then the content should be validated. Then only he can submit the actual information. Say you design a fine grained web service for that. So it has three operations.</p>
<ol>
<li>authenticateUser</li>
<li>validateContent</li>
<li>submitInformation</li>
</ol>
<p>If you design a coarse grained service interface it will be just the &#8216;uploadInformation&#8217; operation. And all the three operation defined earlier will be called within the service logic and the clients will not be aware of that.</p>
<p>So here are some disadvantages that I see in the fine grained interface design compared with coarse grained design.</p>
<ol>
<li>The coupling is too high. Since the client is linked with the service in three adapters. Say you decided to change the service so that you first validate the content and depending on the content it sometime bypasses the authentication. You can&#8217;t do this with the former approach (fine grained approach) without changing the clients. But if it were designed as a coarse grained service (just one uploadInformation operation), the client need not to be changed.</li>
<li>Two much time consumed for the transmission. Since we used three web service calls to do a single task the latency of the operation will largely depend on the network latency which will no doubt is comparatively very low. So the performance of the operation is degraded.</li>
<li>The clients can bypass some steps!, Say in your fine grained service, some client bypass the authentication and validateContent steps and jump directly to the sumbitContent operation. In fact you have to write special code to make sure the clients call the service in the correct sequence, otherwise it will be a big security hole.</li>
</ol>
<p>So I think these three points will be enough to explain why you should try to design a coarse grained interface for your web service.</p>
<p>You can apply these principle when you write data services as well. Lets take the same example explained above. Say there is another table that keep the scoring shots of each game like the one in following.</p>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="7">ScoringShots</th>
</tr>
<tr style="background:#cccccc">
<td>GameID</td>
<td>Time</td>
<td>ScoringTeam</td>
<td>ScoringPlayer</td>
</tr>
</tbody>
</table>
<p>Here also you can keep the operation like &#8216;getGames&#8217; which we defined in the above section. That operation only provide the basic information like the winner and the scores. So if the clients want to know about scoring shots as well, he have to call another operation, say getScoringShots(game) that will return the scoring shots results for a given game. If in practice the clients only need to know about scoring shots of few selected games, then this approach is ok.<br />
But say normally clients need to know about the scoring shots of each and every games. Then they have to call the operation &#8216;getScoringShots&#8217; multiple times. That&#8217;s when the lesson we just learn about the granularity can be applied. We can actually provide another operation, say &#8216;getGamesDetailed&#8217; that actually bundle the details of scoring shots for all games with other information about the game. Here is snip of the response XML, I&#8217;m talking about.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;getGamesDetailedResponse<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         xxx stadium
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         2008-12-18
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Italy
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Sweden
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          34
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          33
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- additionally we have ScoringShots element --&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringShots<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Time<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Time<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Score<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Time<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Time<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Score<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringShots<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>

   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- Another game details are mentioned here --&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>

   <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- More Games --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/getGamesDetailedResponse<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You can generate this kind of response using the nested query support of the data services libraries.<br />
You can checkout more details about nested queries in php data services from <a href="http://www.dimuthu.org/blog/2008/08/04/nested-queries-with-php-data-services/">my old post about php data service API.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/18/data-services-best-practices/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Sending Encrypted Binary Messages With PHP Web Services</title>
		<link>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 17:51:04 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[Attachment]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[MIME]]></category>
		<category><![CDATA[mtom]]></category>
		<category><![CDATA[policy]]></category>
		<category><![CDATA[secuirty token]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=797</guid>
		<description><![CDATA[Web services has made the communication between heterogeneous environments (say PHP with .NET  or Java) a reality. It has defines standards for communicate not only with texts but also with binaries. And more importantly you can keep these communication confidential using encrypted messages according to your requirement. In this post, we will look at how [...]]]></description>
			<content:encoded><![CDATA[<p>Web services has made the communication between heterogeneous environments (say PHP with .NET  or Java) a reality. It has defines standards for communicate not only with texts but also with binaries. And more importantly you can keep these communication confidential using encrypted messages according to your requirement. In this post, we will look at how we can implement such a system with PHP in one side.</p>
<p>In web services we can send/receive binary messages in two basic forms.</p>
<ol>
<li>Setting the binary inside the SOAP message. &#8211; Binary should be converted to base64 to make sure the SOAP body contains only texts. Since base64 converted data span longer than the binary data, we call this form as non-optimized way of sending binaries.</li>
<li>Setting the binary outside the SOAP message &#8211; Binary would be sent as a MIME part in the message. And some element inside SOAP body keeps a reference to the binary using the MIME id. MTOM is a standard for referencing the MIME from inside the SOAP body. Since the binary is encoded, this will keep the message optimum with the binaries.</li>
</ol>
<p>In <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> you can use any of these methods as you prefer. Lets forget about the encryption for now. We will check how we can send binaries in both of the above mentioned forms.</p>
<pre class="php"><span style="font-style: italic; color: #808080;">// first the request xml. Note tht xop:Include element that is referring the attachment with the id "myid1".</span>
<span style="color: #0000ff;">$reqPayloadString</span> = &lt;&lt;&lt;XML
&lt;ns1:upload xmlns:ns1=<span style="color: #ff0000;">"http://wso2.org/wsfphp/samples/mtom"</span>&gt;
               &lt;ns1:fileName&gt;test.jpg&lt;/ns1:fileName&gt;
               &lt;ns1:image xmlmime:contentType=<span style="color: #ff0000;">"image/jpeg"</span> xmlns:xmlmime=<span style="color: #ff0000;">"http://www.w3.org/2004/06/xmlmime"</span>&gt;
                  &lt;xop:<span style="color: #b1b100;">Include</span> xmlns:xop=<span style="color: #ff0000;">"http://www.w3.org/2004/08/xop/include"</span> href=<span style="color: #ff0000;">"cid:myid1"</span>&gt;&lt;/xop:Include&gt;
               &lt;/ns1:image&gt;
&lt;/ns1:upload&gt;
XML;

try <span style="color: #66cc66;">{</span>
    <span style="color: #0000ff;">$f</span> = <a href="http://www.php.net/file_get_contents"><span style="color: #000066;">file_get_contents</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"my_binary_file.jpg"</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// here in the attachments option we define the binaries</span>
    <span style="font-style: italic; color: #808080;">// corresponding to the id defined in the above XML</span>
    <span style="color: #0000ff;">$reqMessage</span> = <span style="font-weight: bold; color: #000000;">new</span> WSMessage<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqPayloadString</span>,
                                <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"to"</span> =&gt; <span style="color: #ff0000;">"http://localhost/simple_upload_service.php"</span>,
                                      <span style="color: #ff0000;">"action"</span> =&gt; <span style="color: #ff0000;">"http://wso2.org/upload"</span>,
                                      <span style="color: #ff0000;">"attachments"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"myid1"</span> =&gt; <span style="color: #0000ff;">$f</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// creating the WSClient</span>
    <span style="font-style: italic; color: #808080;">// here the option useMTOM will decide whether the</span>
    <span style="font-style: italic; color: #808080;">// attachment is set MTOM or base64</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// sending the message and retrieving the response</span>
    <span style="color: #0000ff;">$resMessage</span> = <span style="color: #0000ff;">$client</span>-&gt;<span style="color: #006600;">request</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$reqMessage</span><span style="color: #66cc66;">)</span>;

    <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Response = %s <span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$resMessage</span>-&gt;<span style="color: #006600;">str</span><span style="color: #66cc66;">)</span>;

<span style="color: #66cc66;">}</span> catch <span style="color: #66cc66;">(</span>Exception <span style="color: #0000ff;">$e</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$e</span> instanceof WSFault<span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Soap Fault: %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>, <span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">Reason</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">{</span>
        <a href="http://www.php.net/printf"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Message = %s<span style="font-weight: bold; color: #000099;">\\</span>n"</span>,<span style="color: #0000ff;">$e</span>-&gt;<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span></pre>
<p>As mentioned in the inline comment we can choose the preferred form of sending binary using the &#8220;useMTOM&#8221; option. if it is true, the binary is set as a MTOM, (referencing from the body) or if it is set false, the binary will be set as a base64 binary within the SOAP body.<br />
To encrypt the message you only need to write few additional lines. First you define your policy that you need to encrypt this message using a WSPolicy object. Then the security token including the service public key and your private key. You need to give these two option as a constructor argument in WSClient. Here is that little additional code you need to write to add the encryption.</p>
<pre class="php">    <span style="font-style: italic; color: #808080;">// loading the keys</span>
    <span style="color: #0000ff;">$rec_cert</span> = ws_get_cert_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"receiving_server.cert"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #0000ff;">$pvt_key</span> = ws_get_key_from_file<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"my_private_key.pem"</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// here we defines the policies and create WSPolicy object</span>
    <span style="color: #0000ff;">$sec_array</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"encrypt"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                       <span style="color: #ff0000;">"algorithmSuite"</span> =&gt; <span style="color: #ff0000;">"Basic256Rsa15"</span>,
                       <span style="color: #ff0000;">"securityTokenReference"</span> =&gt; <span style="color: #ff0000;">"IssuerSerial"</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$policy</span> = <span style="font-weight: bold; color: #000000;">new</span> WSPolicy<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"security"</span> =&gt; <span style="color: #0000ff;">$sec_array</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// defining Security Tokens</span>
    <span style="color: #0000ff;">$sec_token</span> = <span style="font-weight: bold; color: #000000;">new</span> WSSecurityToken<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"privateKey"</span> =&gt; <span style="color: #0000ff;">$pvt_key</span>,
                                           <span style="color: #ff0000;">"receiverCertificate"</span> =&gt; <span style="color: #0000ff;">$rec_cert</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

    <span style="font-style: italic; color: #808080;">// modifing WSClient with adding WSPolicy and WSSecurityToken object</span>
    <span style="color: #0000ff;">$client</span> = <span style="font-weight: bold; color: #000000;">new</span> WSClient<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"useWSA"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"useMTOM"</span> =&gt; <span style="font-weight: bold; color: #000000;">TRUE</span>,
                                 <span style="color: #ff0000;">"policy"</span> =&gt; <span style="color: #0000ff;">$policy</span>,
                                 <span style="color: #ff0000;">"securityToken"</span> =&gt; <span style="color: #0000ff;">$sec_token</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>You can implement the receiving side of the message similar to the sending side that we just described above. The most important thing is it doesn&#8217;t need to be written in PHP. It can be a Java code or .NET code.If you already have web services that use encrypted binary messaging, the above php code can be use out of the box to communicate with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/14/sending-encrypted-binary-messages-with-php-web-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

