Create Basic Web Service using PHP and MySQL

Web Services are getting more and more popularity. There are thousands of Web Services available for updating E-Commerce, schools, stock market database etc. Actually Web services are just Web APIs that can be accessed over a network, such as Internet, and executed on a remote system hosting the requested services. There are three basic platform for We Services, these are SOAP, WSDL and UDDI. So here in this tutorial, we will discuss how to create basic web service that provides an XML or JSON response using PHP and MySQL.

Also, read:

As we have covered this tutorial with live demo to create basic web service with PHP, MySQL, XML and JSON, so the file structure for the example is following.

  • index.php
  • result.php

Steps1: Create View Result HTML

In index.php, we will create HTML to view results as XML or JSON.

<div class="container">
	<h2>Create Basic Web Service with PHP, MySQL, XML and JSON</h2>	
	<br><br>
	<a 
href="http://localhost/phpzag/web-service-using-php-mysql-xml-and-json/
result.php?price=1000&num=10&format=xml" 
target="_blank">http://localhost/phpzag/web-service-using-php-mysql-xml-and-json/
result.php?price=1000&num=10&format=xml</a>
	<br><br>	
	<a href="http://localhost/phpzag
/web-service-using-php-mysql-xml-and-json/result.php?price=1000&num=10&format=json" 
target="_blank">http://localhost/phpzag/web-service-using-php-mysql-xml-and-json
/result.php?price=1000&num=10&format=json</a>	
</div>

Steps2: Create Basic Web Service with PHP and MySQL

Now in result.php, we will create basic web service using PHP and MySQL to fetch data into XML or JSON format. In this web service, we will fetch products details based on price.


<?php
include_once("db_connect.php");	
if(isset($_GET['price'])) {	
	$price = $_GET['price']; 
	$no_of_post = (isset($_GET['num'])?$_GET['num']:10);
	$format = (isset($_GET['format'])?$_GET['format']:"xml");	
	$sql_query = "SELECT id, name, description, price FROM items WHERE price <= $price ORDER BY price LIMIT $no_of_post";
	$resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));	
	$products = array();
	if(mysqli_num_rows($resultset)) {
		while($product = mysqli_fetch_assoc($resultset)) {
			$products[] = array('product'=>$product);
		}
	}
	/* output result in required format */
	if($format == 'json') {
		header('Content-type: application/json');
		echo json_encode(array('products'=>$products));
	} else if($format == 'xml') {
		header('Content-type: text/xml');
		echo '<products>';
		foreach($products as $index => $product) {
			if(is_array($product)) {
				foreach($product as $key => $value) {
					echo '<',$key,'>';
					if(is_array($value)) {
						foreach($value as $tag => $val) {
							echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
						}
					}
					echo '</',$key,'>';
				}
			}
		}
		echo '</products>';
	}	
	@mysql_close($link);
}
?>

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download