Simple PHP MySQL Web Service with XML and JSON

Today, We want to share with you Simple PHP MySQL Web Service with XML and JSON.In this post we will show you Create Basic Web Service using PHP and MySQL, hear for web services tutorial in php for beginners we will give you demo and example for implement.In this post, we will learn about send and receive json between android and php web service with an example.

Simple PHP MySQL Web Service with XML and JSON

There are the Following The simple About Simple PHP MySQL Web Service with XML and JSON Full Information With Example and source code.

As I will cover this Post with live Working example to develop Create a Basic Web Service Using PHP, MySQL, XML, and JSON, so the some major files and Directory structures for this example is following below.

Simple PHP / MySQL

if(isset($_GET['user']) && intval($_GET['user'])) {

	$number_of_articles = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
	$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
	$user_id = intval($_GET['user']); //no default

	$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');

	$query = "SELECT blog_title, guid FROM wp_articles WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_articles";
	$result = mysql_query($query,$link) or die('Errant query:  '.$query);

	$articles = array();
	if(mysql_num_rows($result)) {
		while($post = mysql_fetch_assoc($result)) {
			$articles[] = array('post'=>$post);
		}
	}

	if($format == 'json') {
		header('Content-type: application/json');
		echo json_encode(array('articles'=>$articles));
	}
	else {
		header('Content-type: text/xml');
		echo '';
		foreach($articles 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),'';
						}
					}
					echo '';
				}
			}
		}
		echo '';
	}
	@mysql_close($link);
}

sample URL for example

http://mydomain.com/web-service.php?user=2&num=10

The XML Output


	
		Enable PHP Error Log reporting Display on Web Browser
		https://pakainfo.name/?p=2304
	
	
		Top Web Development Companies in the United Kingdom
		https://pakainfo.name/?p=2288
	
	
		Create Dynamic Web Pages using PHP-MYSQL
		https://pakainfo.name/?p=2266
	
	
		Browser Features of the Opera web browser Free VPN
		https://pakainfo.name/?p=2258
	
	
		Angular 6 and ASP.NET Core 2.0 Web API Example
		https://pakainfo.name/?p=2218
	
	
		vuejs Simple calculator web Application
		https://pakainfo.name/?p=2194
	
	
		PHP Webcam Capture Image Save from Camera using jQuery
		https://pakainfo.name/?p=2178
	
	
		Domadiya-DJ
		https://pakainfo.name/?page_id=2172
	
	
		iPhone Safari Web App opens links in new window
		https://pakainfo.name/?p=2168
	
	
		Laravel detect device and redirect to Mobile-tablet website in PHP
		https://pakainfo.name/?p=2158
	

JSON sample URL for example

http://mydomain.com/web-service.php?user=2&num=10&format=json

The JSON Output

{"articles":[{"post":{"blog_title":"Enable PHP Error Log reporting Display on Web Browser","guid":"http:\/\/pakainfo.name\/?p=2304"}},{"post":{"blog_title":"Top Web Development Companies in the United Kingdom","guid":"http:\/\/pakainfo.name\/?p=2288"}},{"post":{"blog_title":"Create Dynamic Web Pages using PHP-MYSQL","guid":"http:\/\/pakainfo.name\/?p=2266"}},{"post":{"blog_title":"Browser Features of the Opera web browser Free VPN","guid":"http:\/\/pakainfo.name\/?p=2258"}},{"post":{"blog_title":"Angular 6 and ASP.NET Core 2.0 Web API Example","guid":"http:\/\/pakainfo.name\/?p=2218"}},{"post":{"blog_title":"vuejs Simple calculator web Application","guid":"http:\/\/pakainfo.name\/?p=2194"}},{"post":{"blog_title":"PHP Webcam Capture Image Save from Camera using jQuery","guid":"http:\/\/pakainfo.name\/?p=2178"}},{"post":{"blog_title":"Domadiya-DJ","guid":"http:\/\/pakainfo.name\/?page_id=2172"}},{"post":{"blog_title":"iPhone Safari Web App opens links in new window","guid":"http:\/\/pakainfo.name\/?p=2168"}},{"post":{"blog_title":"Laravel detect device and redirect to Mobile-tablet website in PHP","guid":"http:\/\/pakainfo.name\/?p=2158"}}]}
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Simple PHP MySQL Web Service with XML and JSON.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment