<?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>Roshan Bhattarai's Blog -  A blog about PHP, Ajax, JavaScript, CSS and Web 2.0 &#187; web services</title>
	<atom:link href="http://roshanbh.com.np/category/web-services/feed" rel="self" type="application/rss+xml" />
	<link>http://roshanbh.com.np</link>
	<description>Useful Tutorials, Scripts , Tips, and Resources for all PHP and Ajax beginners and experts .</description>
	<lastBuildDate>Thu, 10 Jun 2010 11:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating and Parsing JSON data with PHP</title>
		<link>http://roshanbh.com.np/2008/10/creating-parsing-json-data-php.html</link>
		<comments>http://roshanbh.com.np/2008/10/creating-parsing-json-data-php.html#comments</comments>
		<pubDate>Sat, 04 Oct 2008 18:10:51 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[how-to]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=293</guid>
		<description><![CDATA[Yesterday, I was in a party and a guy came near to me and asked me what is JSON and how can handle it via PHP. Today, I&#8217;m going to tell you something about JSON data and how we can handle them via PHP. Although, JSON stands JavaScript Object Notation, it is used by many [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F10%2Fcreating-parsing-json-data-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F10%2Fcreating-parsing-json-data-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">Yesterday, I was in a party and a guy came near to me and asked me what is JSON and how can handle it via PHP. Today, I&#8217;m going to tell you something about JSON data and how we can handle them via PHP. Although, JSON stands <strong><a href="http://www.json.org/" target="_blank">JavaScript Object Notation</a></strong>, it is used by many other technologies like PHP and Java for data interchange format over the Internet.</p>
<p><span id="more-293"></span></p>
<h4>What is JSON?</h4>
<p style="text-align: justify;">JSON is ultra-weight data interchange data format used over the internet for transferring the data. While XML is a dominant data interchange format over the internet but JSON is less complex and light-weight data.</p>
<p style="text-align: justify;">Though it was first made to be used with JavaScript for accessing remote data, it is now used by many other languages because JSON data is platform independent data format.</p>
<h4>Data Types and Example of JSON data</h4>
<p>JSON supports various kind of data types which included numbers, strings, booleans as well as array datas and obviously object (collection of key:value pairs, comma-separated and enclosed in curly brackets).</p>
<p>Now, let&#8217;s look at the example of simple format of JSON data for a detail of a employee,</p>
<pre class="prettyprint" style="overflow:auto">{"id":"1","name":"mike","country":"usa","office":["microsoft","oracle"]}</pre>
<h4>Creating and Parsing JSON data format in PHP</h4>
<p>To handle JSON data there is <a href="http://www.php.net/json" target="_blank">JSON extension</a> in PHP which is aviable after PHP 5.2.0. Two funcitons : <a href="http://www.php.net/manual/en/function.json-encode.php">json_encode()</a> and <a href="http://www.php.net/manual/en/function.json-decode.php">json_decode()</a> are very useful converting and parsing JSON data through PHP.</p>
<p>First of all, let&#8217;s look at the PHP code to create the JSON data format of above example using array of PHP.</p>
<pre class="prettyprint" style="overflow:auto">$json_data = array ('id'=&gt;1,'name'=&gt;"mike",'country'=&gt;'usa',"office"=&gt;array("microsoft","oracle"));
echo json_encode($json_data);</pre>
<p>The above code generates the JSON data exactly as above. Now, let&#8217;s decode above JSON data in PHP.</p>
<pre class="prettyprint" style="overflow:auto">$json_string='{"id":1,"name":"mike","country":"usa","office":["microsoft","oracle"]} ';
$obj=json_decode($json_string);</pre>
<p>Now, the $obj variable contains JSON data parsed in PHP object which you can display using code below.</p>
<pre class="prettyprint" style="overflow:auto">echo $obj-&gt;name; //displays mike
echo $obj-&gt;office[0]; //displays microsoft</pre>
<p>As you can guess,$obj-&gt;office is an array and you can loop through it using <a href="http://www.php.net/foreach" target="_blank">foreach</a> loop of PHP,</p>
<pre class="prettyprint" style="overflow:auto">foreach($obj-&gt;office as $val)
    echo $val;</pre>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=293&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/10/creating-parsing-json-data-php.html/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Web Services and PHP &#8211; SOAP vs XML-RPC vs REST</title>
		<link>http://roshanbh.com.np/2008/07/web-services-php-soap-vs-xml-rpc-vs-rest.html</link>
		<comments>http://roshanbh.com.np/2008/07/web-services-php-soap-vs-xml-rpc-vs-rest.html#comments</comments>
		<pubDate>Thu, 31 Jul 2008 12:23:35 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[XML-RPC]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=183</guid>
		<description><![CDATA[What is web services? In a typical web surfing scenario, a visitor visits a website and use the functionality provided by that particular website.HTTP request is send to server from web browsers and server responses are translated by browser to display the desired result of the visitor. But, this scenario has been changed in the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F07%2Fweb-services-php-soap-vs-xml-rpc-vs-rest.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F07%2Fweb-services-php-soap-vs-xml-rpc-vs-rest.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h4>What is web services?</h4>
<p style="text-align: justify;">In a typical web surfing scenario, a visitor visits a website and use the functionality provided by that particular website.HTTP request is send to server from web browsers and server responses are translated by browser to display the desired result of the visitor. But, this scenario has been changed in the recent days. You don&#8217;t need to visit the particular website to use their service and functionality if they are providing web services. <strong>Web services</strong> are set of platform independent exposed APIs(functions) which can be used used from remote server over the Internet. There are basically two parties involved in this, one which provides a set of exposed APIs and the another one ,commonly know as web services consumers,is the party which uses the functionality and services provided by web services providing party.</p>
<p><span id="more-183"></span></p>
<p style="text-align: justify;">There are different method for providing web services but the most common are<a href="http://en.wikipedia.org/wiki/SOAP" target="_blank"> SOAP</a>, <a href="http://en.wikipedia.org/wiki/Xml-rpc" target="_blank">XML-RPC</a> and <a href="http://en.wikipedia.org/wiki/REST">REST</a> .</p>
<h4>SOAP</h4>
<p style="text-align: justify;"><a href="http://en.wikipedia.org/wiki/SOAP" target="_blank">SOAP</a> was the acronym of <strong>Simple Object Access Protocal</strong> but this acronym was dropped in the version of 1.2 of SOAP. It is method for exchanging XML based message over the Internet for providing and consuming web services. SOAP message are transferred forming the SOAP-Envelope.You can view the typical <a href="http://www.w3schools.com/soap/soap_syntax.asp" target="_blank">SOAP Message articture from here</a>. SOAP is widely criticized for it&#8217;s design complexity.</p>
<p style="text-align: justify;">In PHP 5, there is built-in extension for the providing and consuming web services. But, I personally prefer <a href="http://sourceforge.net/projects/nusoap/" target="_blank">Nusoap</a> toolkit of PHP for providing and consuming web services using SOAP in PHP.</p>
<h4>XML-RPC</h4>
<p><a href="http://en.wikipedia.org/wiki/Xml-rpc" target="_blank">XML-RPC (remote procedure call)</a> another way of providing and consuming web services. It uses XML to encode and decode the remote procedure call along with it&#8217;s parameter. Compared to the articture of SOAP, it has simpler architecture. You can even define data type of parameters of procedure in XML-RPC. You can visit the official website <a href="www.xmlrpc.com">www.xmlrpc.com </a> to know more about XML-RPC.</p>
<p style="text-align: justify;">In PHP, there is extension <a href="http://www.php.net/xmlrpc" target="_blank">which contain various functions</a> for facilating XML-RPC request and response. But the functions <a href="http://www.php.net/xmlrpc_encode_request" target="_blank">xmlrpc_encode_request()</a> and <a href="http://www.php.net/xmlrpc_decode_request">xmlrpc_decode_request()</a> available in PHP is very useful for when it comes to encode and decode XML-RPC request and response.</p>
<p style="text-align: justify;">I&#8217;ve built  <a href="http://roshanbh.com.np/tools/nepali-currency-converter/" target="_blank">Nepali Currency Converter</a> using XML-RPC web services provided by <a href="http://foxrate.org">foxrate.org</a>.</p>
<h4>REST</h4>
<p style="text-align: justify;">Representational State Trasfer(REST)  is comparatively simpler method for providing and consuming web services. Nowadays, this method is becoming popular in the arena of web services. Unlike above two method, it is not necessary to use XML as a data interchange format in REST. REST architecture is basically focused on two things : <strong>Resources</strong> and <strong>Interface</strong>.RESTful  is another term to define REST web services .</p>
<p style="text-align: justify;"><strong>Resources</strong> are application&#8217;s state and functionality which is represented by a unique URL.  The resources share a uniform <strong>interface</strong> to transfer the state between the client and server.</p>
<p style="text-align: justify;">For example the URL, http://example.com/product/11 can be a <strong>resource</strong>.Suppose, GET method is used to retrieve product detail from that URL, POST method is used to modify the production information and DELETE method can be used to delete the product from the same URL. Here, the HTTP methods works as a <strong>interface</strong> to access the resources.</p>
<p style="text-align: justify;">Talking about  PHP, the format of information(representation) returned can be in XML, JSON or even in HTML format. <a href="http://www.php.net/dom" target="_blank">DOM functions</a>, <a href="http://www.php.net/simplexml">SimpleXML functions</a> and <a href="http://www.php.net/json" target="_blank">JSON functions</a> comes handy when you are handling RESTful interfaces in PHP.</p>
<p style="text-align: justify;">I also want to know your view. Please participate in the poll below.</p>
<p style="text-align: justify;">[poll id="3"]</p>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=183&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/07/web-services-php-soap-vs-xml-rpc-vs-rest.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Getting country , city name from IP address in PHP</title>
		<link>http://roshanbh.com.np/2008/07/getting-country-city-name-from-ip-address-in-php.html</link>
		<comments>http://roshanbh.com.np/2008/07/getting-country-city-name-from-ip-address-in-php.html#comments</comments>
		<pubDate>Thu, 17 Jul 2008 10:41:18 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[how-to]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips and technique]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[api]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/?p=164</guid>
		<description><![CDATA[Yesterday, miaki asked me how can we get the country name from the IP address in PHP. Today, I&#8217;ve come up with the answer of this question. I&#8217;ve used the API from hostip.info to fetch the country name , city name and country code from the given IP address. I&#8217;ve mad this function in PHP [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F07%2Fgetting-country-city-name-from-ip-address-in-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F07%2Fgetting-country-city-name-from-ip-address-in-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">Yesterday,  <a rel="nofollow" href="http://rowebsoft.com">miaki</a> asked me how can we get the country name from the IP address in PHP. Today, I&#8217;ve come up with the answer of this question. I&#8217;ve used the API from <a href="http://www.hostip.info/use.html">hostip.info</a> to fetch the country name , city name and country code from the given IP address. I&#8217;ve mad this function in PHP which uses XML response from  hostip.info and extracted country name, city name and country code using regular expression.</p>
<p><span id="more-164"></span></p>
<h4>Function to return country, city name from IP address in PHP</h4>
<pre class="prettyprint" style="overflow:auto;">function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai http://roshanbh.com.np

//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array

//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

//get the city name inside the node &lt;gml:name&gt; and &lt;/gml:name&gt;
preg_match("@&lt;Hostip&gt;(\s)*&lt;gml:name&gt;(.*?)&lt;/gml:name&gt;@si",$xml,$match);

//assing the city name to the array
$ipDetail['city']=$match[2]; 

//get the country name inside the node &lt;countryName&gt; and &lt;/countryName&gt;
preg_match("@&lt;countryName&gt;(.*?)&lt;/countryName&gt;@si",$xml,$matches);

//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];

//get the country name inside the node &lt;countryName&gt; and &lt;/countryName&gt;
preg_match("@&lt;countryAbbrev&gt;(.*?)&lt;/countryAbbrev&gt;@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array

//return the array containing city, country and country code
return $ipDetail;

}</pre>
<p style="text-align: justify; font-weight: bold;"><a href="http://roshanbh.com.np/codes/country-ip.phps">Download Source Code</a></p>
<p style="text-align: justify;">As you can see, I&#8217;ve documented all the PHP code and I don&#8217;t think I need explain anymore about that code. Just notice that, this function returns the array containg three key elements &#8220;country&#8221; , &#8220;city&#8221; and &#8220;country_code&#8221;. Each elements have the value of city, country and country code.</p>
<p style="text-align: justify;">Now, look the the how we can use the above function in PHP,</p>
<pre class="prettyprint" style="overflow:auto;">$IPDetail=countryCityFromIP('12.215.42.19');
echo $IPDetail['country']; //country of that IP address
echo $IPDetail['city']; //outputs the IP detail of the city</pre>
<p style="text-align: justify;">Notice that the above PHP function returns the array containing the country , city and country code from IP Address and we can use them in PHP. If you want to know how to get IP address in PHP, you can check this post how you can get <a href="http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html">real IP address in PHP</a>.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=164&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/07/getting-country-city-name-from-ip-address-in-php.html/feed</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/16 queries in 0.158 seconds using disk: basic
Object Caching 593/618 objects using disk: basic

Served from: roshanbh.com.np @ 2012-02-08 21:09:42 -->
