Tuesday, 26 March 2013

Introduction to web-services

Web-service- A web service is a method of communication between two electronic devices over the WWW. So, in web service there are two devices, namely,

  • Server - Which has resource and shares it to the client.
  • Client - Requests the desired resource from server.
Server provides web API (application programming interface) that is typically a defined set of HTTP request messages along with a definition of the structure of response messages, typically expressed in JSON or XML.
Example of web-services,

  • Google Map.
  • Blogger - Client can create, edit, view, delete and search blog's content using Blogger Data API. 
  • Facebook
  • Amazon
  • So on...
So, requirements for web-services,

  • The Web services architecture is an interoperability (The ability of software and hardware on different machines from different vendors to share data is interoperability) architecture : because client can be any device. Server must provide resource that is understood by client.  So, mostly XML (most of languages including PHP, Java, DotNet, etc can create/ read xml files), JSON (JavaScript on Notation) etc are used. 
  • Server which gives resource
  • Client which requests resource
  • Communication to others (to application developer at client side) to use web-services. So, mostly documentation is provided by web-servers.

Web-services-JSON

The two types of web-services are,

  • REST (Representational State Transfer) which is mostly based on JSON.  
  • SOAP (Simple Object Access Protocol) which is mostly based on WSDL (web-site description language)

JSON 

JavaScript Object Notation is a lightweight data-interchange format. So, it's faster than SOAP (XML format). And hence, application runs faster using same web-service using JSON than XML. Example of JSON , 
{
  "Customer": [
    { "FullName":"Some name" , "Email":"email@example.com" }, 
    { "FullName":"Other name" , "Email":"other@example.com" }
  ]
}

Above entity contains details of Customer. As you can see, it stores Customer's FullName and Email. (Note: JSON is case-sensitive so email is different than Email or eMail ) If you know JavaScript multidimensional literal array, JSON will be easy for you (you can google it and learn javascript's array).
If we got above output, how do we access any particular value, say first Customer ' s Email,
var customer1Email = Customer[0].Email;//email@example.com
Similarly, we can access other values in javascript. 
PHP functions for JSON,

Monday, 11 March 2013

XML (Extensible Markup Language) tutorial

It required to know xml for using SOAP. 

Background of XML, html. 

  Both are markup language. Markup language have markup tags. Tags are special meaning. Suppose, you are reading following line,
I am learning XML. XML is easy language.
You paused at full stop (.), you don't spell out (full stop literally), you followed it's meaning not literal meaning. Full stop is like tag. They are commands not just strings like others. All tags are start with < and ends with > . Most tags are in pairs, (opening and closing tags). Other are called empty tags. Some tags have attributes. They are properties of tags. Like <img src="location"> here src is attributes. (In XML tags may have attributes like HTML).

XML is similar to html with few differences given below-

  • XML  must be well-formed. Even if you skip html closing tag, you will see output correctly in browser. But XML need to be well formed. Well formed XML have following requirements,
  •  There should be 1 and only 1 document root.
Wrong example, because there are two roots,
<?xml version="1.0" encoding="UTF-8"?>
<root>
     <child>Some text</child>
     <child2>More text</child2>
</root>
<root2>
    <child2>Some text</child2>
</root2>

It should be changed to,
<?xml version="1.0" encoding="UTF-8"?>
<uniqueRoot>
     <root>
        <child>Some text</child>
        <child2>More text</child2>
    </root>
    <root2>
        <child2>Some text</child2>
    </root2>
</uniqueRoot>
  • Proper nesting. Parent tag must end after child tag. The relation should be followed, 
Following is wrong XML as parent child relation is not maintained, 
<parentTag>
    <childTag>
        Some sring
    </parentTag>
</childTag>


Correct format,
<parentTag>
    <childTag>
        Some sring
    </childTag>
</parentTag>
  • Every tag must end even empty tag, example,
         <br></br>  or <br /> (any of these two ways)
  • Capital and small letter matters (not in html). <TagName> is different tag than <Tagname>.
  • HTML has limited pre-defined tags. XML  has unlimited tags. You can create tags of your own choice. As there is no pre-defined tags, sometimes DTD is given with XML. DTD (Document Type Definition) to define structure of XML document.
  •  In XML, special characters must be encoded. It should be encoded in html too , but browsers are intelligent and it don't show errors in HTML. Like if you use & , < , > , etc. But in XML, it will show you errors.  

Valid XML

  • Valid XML is well formed. (It should follow above all rules).
  • It should be according to DTD.  
More about DTD in next post.