banner



How To Create Webservices In Php Step By Step

Create a Basic Web Service Using PHP, MySQL, XML, and JSON

Web services are taking over the world. I credit Twitter's epic rise to the availability of a simple but rich API. Why not use the same model for your own sites? Here's how to create a basic web service that provides an XML or JSON response using some PHP and MySQL.

The PHP / MySQL

/* require the user as the parameter */ if(isset($_GET['user']) && intval($_GET['user'])) {  	/* soak in the passed variable or set our own */ 	$number_of_posts = 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  	/* connect to the db */ 	$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 db */ 	$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 necessary format */ 	if($format == 'json') { 		header('Content-type: application/json'); 		echo json_encode(array('posts'=>$posts)); 	} 	else { 		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); }

With the number of persons hitting your web service (hopefully), you'll need to do adequate validation before attempting to connect to the database to avoid injection attacks. Once we get the desired results from the database, we cycle through the results to populate our return results array. Depending upon the response type desired, we output the proper header and content in the desired format.

Take the following sample URL for example:

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

Now, we can take a look at the possible results of the URL.

The XML Output

<posts> 	<post> 		<post_title>SSLmatic SSL Certificate Giveaway Winners</post_title> 		<guid>https://davidwalsh.name/?p=2304</guid> 	</post> 	<post> 		<post_title>MooTools FileManager</post_title> 		<guid>https://davidwalsh.name/?p=2288</guid> 	</post> 	<post> 		<post_title>PHPTVDB: Using PHP to Retrieve TV Show Information</post_title> 		<guid>https://davidwalsh.name/?p=2266</guid> 	</post> 	<post> 		<post_title>David Walsh: The Lost MooTools Plugins</post_title> 		<guid>https://davidwalsh.name/?p=2258</guid> 	</post> 	<post> 		<post_title>Create Short URLs Using U.Nu</post_title> 		<guid>https://davidwalsh.name/?p=2218</guid> 	</post> 	<post> 		<post_title>Create Bit.ly Short URLs Using PHP</post_title> 		<guid>https://davidwalsh.name/?p=2194</guid> 	</post> 	<post> 		<post_title>Represent Your Repositories Using the GitHub Badge!</post_title> 		<guid>https://davidwalsh.name/?p=2178</guid> 	</post> 	<post> 		<post_title>ZebraTable</post_title> 		<guid>https://davidwalsh.name/?page_id=2172</guid> 	</post> 	<post> 		<post_title>MooTools Zebra Table Plugin</post_title> 		<guid>https://davidwalsh.name/?p=2168</guid> 	</post> 	<post> 		<post_title>SSLmatic: Quality, Cheap SSL Certificates and Giveaway!</post_title> 		<guid>https://davidwalsh.name/?p=2158</guid> 	</post> </posts>

Take this next sample URL for example:

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

Now, we can take a look at the possible results of the URL.

The JSON Output

{"posts":[{"post":{"post_title":"SSLmatic SSL Certificate Giveaway Winners","guid":"http:\/\/davidwalsh.name\/?p=2304"}},{"post":{"post_title":"MooTools FileManager","guid":"http:\/\/davidwalsh.name\/?p=2288"}},{"post":{"post_title":"PHPTVDB: Using PHP to Retrieve TV Show Information","guid":"http:\/\/davidwalsh.name\/?p=2266"}},{"post":{"post_title":"David Walsh: The Lost MooTools Plugins","guid":"http:\/\/davidwalsh.name\/?p=2258"}},{"post":{"post_title":"Create Short URLs Using U.Nu","guid":"http:\/\/davidwalsh.name\/?p=2218"}},{"post":{"post_title":"Create Bit.ly Short URLs Using PHP","guid":"http:\/\/davidwalsh.name\/?p=2194"}},{"post":{"post_title":"Represent Your Repositories Using the GitHub Badge!","guid":"http:\/\/davidwalsh.name\/?p=2178"}},{"post":{"post_title":"ZebraTable","guid":"http:\/\/davidwalsh.name\/?page_id=2172"}},{"post":{"post_title":"MooTools Zebra Table Plugin","guid":"http:\/\/davidwalsh.name\/?p=2168"}},{"post":{"post_title":"SSLmatic: Quality, Cheap SSL Certificates and Giveaway!","guid":"http:\/\/davidwalsh.name\/?p=2158"}}]}

Creating a basic web service is very simple and encourages your users to spread the word about your website or service. Want more traffic? Want your website to grow without you putting in all the effort? Create a web service!

Website performance monitoring

Website performance monitoring

Recent Features

  • Responsive Images: The Ultimate Guide

    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • LightFace:  Facebook Lightbox for MooTools

    LightFace: Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • Using Opacity to Show Focus with MooTools

    Using Opacity to Show Focus with MooTools

    I'm a huge fan of using subtle effects like link nudging (jQuery, MooTools) to enhance the user experience and increase the perceived dynamism of my websites. Trust me -- a lot of little things are what take websites to the next level.

  • PHP Woot Checker &#8211; Tech, Wine, and Shirt Woot

    PHP Woot Checker – Tech, Wine, and Shirt Woot

    If you haven't heard of Woot.com, you've been living under a rock. For those who have been under the proverbial rock, here's the plot: Every day, Woot sells one product. Once the item is sold out, no more items are available for purchase. You don't know how many...

How To Create Webservices In Php Step By Step

Source: https://davidwalsh.name/web-service-php-mysql-xml-json

Posted by: millercrummon.blogspot.com

0 Response to "How To Create Webservices In Php Step By Step"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel