Web Service Using PHP, MySQL, XML, and JSON

Web Service

Web Service Using PHP, MySQL, XML, and JSON

Web service in PHP Consists 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?

 XML

XML stands for Extensible Markup Language is an important protocol of 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 a Basic Web Service Using PHP

First of all, we have to create a web service file in which we have to write a code in PHP & MySQL to get the 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 with the database after executing the query, we will get the result and that output result we have converted in the format like  JSON and XML in the form of an array.

Take the following example URL for web service :

http://mindtrickpages.in/webservice.php?userid=1&number=10


After Executing above URL you will get result in XML format

 XML Output

<posts>
<post>
<post_title>Armstrong In C#</post_title>
<guid>http://mindtrickpages.in/?p=1243</guid>
</post>

<post>
<post_title>Web Service Using PHP, MySQL, XML, and JSON</post_title>
<guid>http://mindtrickpages.in/?p=1235</guid>
</post>

<post>
<post_title>Sort Array Item using Selection Sort In C#</post_title>
<guid>http://mindtrickpages.in/?p=1232</guid>
</post>

<post>
<post_title>How to check a string is Polindrome or not in C#</post_title>
<guid>http://mindtrickpages.in/?p=1224</guid>
</post>

<post>
<post_title>
Simple Example using Jquery DataTable in ASP.NET MVC
</post_title>
<guid>http://mindtrickpages.in/?p=1216</guid>
</post>

<post>
<post_title>What is Visualforce?</post_title>
<guid>http://mindtrickpages.in/?p=1193</guid>
</post>
<post>
<post_title>
DIFFERENCE BETWEEN ABSOLUTE,FIXED ,STATIC & RELATIVE:
</post_title>
<guid>http://mindtrickpages.in/?p=1192</guid>
</post>
<post>
<post_title>MS word shortcut keys</post_title>
<guid>http://mindtrickpages.in/?p=1183</guid>
</post>
<post>
<post_title>clear dns cache</post_title>
<guid>http://mindtrickpages.in/?p=1174</guid>
</post>
<post>
<post_title>Machine Learning Techniques</post_title>
<guid>http://mindtrickpages.in/?p=1160</guid>
</post>
</posts>

 

 

JSON Output

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

http://mindtrickpages.in/webservice.php?userid=1&number=10&format=json
{"posts":[{"post":{"post_title":"Armstrong In C#","guid":"http:\/\/mindtrickpages.in\/?p=1243"}},{"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"}}]}

It is very easy to create a web service in PHP. Share others to do same.Hope this article will help you.

Thank You

Leave a Comment