Create Web Service Using PHP

Create Web Service Using PHP:- Consist of protocol like XML, JSON,etc..that  interact between other web applications to get output in the form of XML  and JSON to the purpose of exchange data.

You can exchange data between two web applications built on different platform languages.To exchange data we need output in the form of XML and JSON. Which are protocols that are used to exchange information between web applications?

Here we start topic on Create Web Service Using PHP

 XML

XML stands for Extensible Markup Language is an important protocol for web services in PHP.It can be used to store and exchange data while communicating between web applications.XML was designed to be both human- and machine-readable.

JSON

JSON  (JavaScript Object Notation) is a lightweight data-interchange format.which can also store and exchange data.Output comes in the form of text and it is also human-readable and completely language independent.

Create Web Service Using PHP CODE

First of all, we have to create a web service file in which we have to write a code in PHP & MySQL to get a response in the form of JSON and XML.You have to create  a file name with webservice.php on server and copy the below code

The PHP / MySQL Web service Code

/* require the user as the parameter */
if(isset($_GET['userid']) && intval($_GET['userid'])) {

	/* here you can configure format of output,number of records you want to fetch */
	$number_of_posts = isset($_GET['number']) ? intval($_GET['number']) : 20; //20 is the default
	$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
	$user_id = intval($_GET['userid']); //no default

	/* connect to the mysql database with your own database setting */
	$link = mysql_connect('localhost','username','password') or die('Cannot connect to the DB');
	mysql_select_db('db_name',$link) or die('Cannot select the DB');

	/* grab the posts from the database you can customize your query on the basis of table */
	$query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
	$result = mysql_query($query,$link) or die('Errant query:  '.$query);

	/* create one master array of the records */
	$posts = array();
	if(mysql_num_rows($result)) {
		while($post = mysql_fetch_assoc($result)) {
			$posts[] = array('post'=>$post);
		}
	}

	/* output in JSON format */
	if($format == 'json') {
		header('Content-type: application/json');
		echo json_encode(array('posts'=>$posts));
	}
	else {
/*  XML FORMAT Output*/

		header('Content-type: text/xml');
		echo '<posts>';
		foreach($posts as $index => $post) {
			if(is_array($post)) {
				foreach($post as $key => $value) {
					echo '<',$key,'>';
					if(is_array($value)) {
						foreach($value as $tag => $val) {
							echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
						}
					}
					echo '</',$key,'>';
				}
			}
		}
		echo '</posts>';
	}

	/* disconnect from the db */
	@mysql_close($link);
}


In the above code, First of all, we have configured the input value to interact with the web application.Then we have to write a code to connect to the database after executing a query, we will get a result and that output result we have converted in a format like  JSON and XML in the form of the array.

Take the following example URL for Create Web Service Using PHP:

http://techieflair.com/webservice.php?userid=1&number=10


After Executing above URL you will get result in XML format

 XML Output

<posts>
<post>
<post_title>
How we can create a Login and Logout Form in Php and mysql
</post_title>
<guid>http://techieflair.com/?p=538</guid>
</post>
<post>
<post_title>
How we can create a Simple Registration form in php
</post_title>
<guid>http://techieflair.com/?p=530</guid>
</post>
<post>
<post_title>
How we can use Group By,Having and Order By Mysql Clauses using php script
</post_title>
<guid>http://techieflair.com/?p=510</guid>
</post>
<post>
<post_title>
How we can use Pattern matching in Mysql using php script
</post_title>
<guid>http://techieflair.com/?p=498</guid>
</post>
<post>
<post_title>
How we can retrieve data from Mysql using Php script
</post_title>
<guid>http://techieflair.com/?p=488</guid>
</post>

</posts>

JSON Output

Take this next  URL for Web Sevice  to get result in JSON Format:

http://techieflair.com/webservice.php?userid=2&number=10&format=json
{"posts":[{"post":{"post_title":"How we can create a Login and Logout Form in Php and mysql","guid":"http:\/\/techieflair.com\/?p=538"}}},{"post":{"post_title":"Sort Array Item using Selection Sort In C#","guid":"http:\/\/mindtrickpages.in\/?p=1232"}},{"post":{"post_title":"How to check a string is Polindrome or not in C#","guid":"http:\/\/mindtrickpages.in\/?p=1224"}},{"post":{"post_title":"Simple Example using Jquery DataTable in ASP.NET MVC","guid":"http:\/\/mindtrickpages.in\/?p=1216"}},{"post":{"post_title":"What is Visualforce?","guid":"http:\/\/mindtrickpages.in\/?p=1193"}},{"post":{"post_title":"DIFFERENCE BETWEEN  ABSOLUTE,FIXED ,STATIC & RELATIVE","guid":"http:\/\/mindtrickpages.in\/?p=1192"}},{"post":{"post_title":"MS word shortcut keys","guid":"http:\/\/mindtrickpages.in\/?p=1183"}},{"post":{"post_title":"clear dns cache","guid":"http:\/\/mindtrickpages.in\/?p=1174"}},{"post":{"post_title":"Machine Learning Techniques","guid":"http:\/\/mindtrickpages.in\/?p=1160"}},{"post":{"post_title":"Machine Learning","guid":"http:\/\/mindtrickpages.in\/?p=1157"}}]}

Demo Link

It is very easy to create  Web Service Using PHP. Share others to do same.Hope this article will help you.

Leave a Comment