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 '<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 :

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.   How to Check check php version command line linux
  2.   what does pairwise distinct mean?
  3.   Editing .docx files and update docx files using PHP
  4.   Custom Pagination Using WooCommerce REST API
  5.   How To Create Pdf Using MPDF Codeigniter 3?
  6.   C# Named and Optional Arguments Tutorial with Examples
  7.   Yoast Wordpress Seo Settings
  8.   Laravel 6 Inner Join Query Example - MySQL
  9.   Laravel eloquent join with condition
  10.   laravel Add cascade on delete (ON DELETE CASCADE)