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.

Read Also:  MySQL OR - Comparison Functions and Operators By Example

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 '<articles>';
		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),'</',$tag,'>';
						}
					}
					echo '</',$key,'>';
				}
			}
		}
		echo '</articles>';
	}
	@mysql_close($link);
}

sample URL for example

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

The XML Output

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

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 :

Read Also:  Laravel Clear Cache Commands Using PHP Artisan

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Model View Controller(MVC) in PHP Hello World Example
  2. Read Also:  MySQL SELECT DISTINCT, COUNT, ROWS EXAMPLE
  3. Read Also:  JQuery Include Header and Footer in HTML
  4. Read Also:  MySQL BETWEEN Operators with WHERE Condition
  5. Read Also:  Angular 9/8 Get Checked Radio Button on Change Event
  6. Read Also:  Extract XML Response in PHP Example
  7. Read Also:  Laravel 6 Join Clause Example Tutorial
  8. Read Also:  MySQL IN Operators with WHERE Condition
  9. Read Also:  Top 40 Laravel Interview Questions
  10. Read Also:  Laravel Increase Website Speed performance optimization
CLOSEX