Web Service Using PHP, MySQL, XML, and JSON

May 24th, 2012 by Laeeq | 4 comments

Today Web Services gain much popularity in web world. There are thousands of Web Services availble 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.

Here we will discuss to create basic web service that provides an XML or JSON response using some PHP and MySQL.

 

The PHP / MySQL
  1. /* require the user as the parameter */
  2. if(isset($_GET['myuser']) && intval($_GET['myuser'])) {
  3. $number_of_posts = isset($_GET['number']) ? intval($_GET['number']) : 20;
  4. $format = strtolower($_GET['format']) == ‘json’ ? ‘json’ : ‘xml’;
  5. $user_id = intval($_GET['myuser']);
  6. /* connect to mysql database */
  7. $link = mysql_connect(‘localhost’,‘username’,‘password’) or die(‘Can not connect to the Database’);
  8. mysql_select_db(‘mydb_name’,$link) or die(‘Can not select the Database’);
  9. /* select records from the database */
  10. $query = “SELECT post_title, guid FROM posts WHERE wp_post_author = $user_id AND post_status = ’publish’ ORDER BY ID DESC LIMIT $number_of_posts”;
  11. $result = mysql_query($query,$link) or die(‘Errant query:  ’.$query);
  12. /* create array of the records */
  13. $posts = array();
  14. if(mysql_num_rows($result)) {
  15. while($post = mysql_fetch_assoc($result)) {
  16. $posts[] = array(‘post’=>$post);
  17. }
  18. }
  19. /* output in required format */
  20. if($format == ‘json’) {
  21. header(‘Content-type: application/json’);
  22. echo json_encode(array(‘posts’=>$posts));
  23. }
  24. else {
  25. header(‘Content-type: text/xml’);
  26. echo ‘<posts>’;
  27. foreach($posts as $index => $post) {
  28. if(is_array($post)) {
  29. foreach($post as $key => $value) {
  30. echo ‘<’,$key,‘>’;
  31. if(is_array($value)) {
  32. foreach($value as $tag => $val) {
  33. echo ‘<’,$tag,‘>’,htmlentities($val),‘</’,$tag,‘>’;
  34. }
  35. }
  36. echo ‘</’,$key,‘>’;
  37. }
  38. }
  39. }
  40. echo ‘</posts>’;
  41. }
  42. /* close database connection */
  43. @mysql_close($link);
  44. }

We will take the following sample URL for example:

http://mydomain.com/web-service.php?user=5&num=3

Below is the possible results of the above URL.

This is XML Output
  1. <posts>
  2. <post>
  3. <post_title>Now YouTube Video Player Uses HTML5</post_title>
  4. <guid>http://www.phpzag.com/?p=1568</guid>
  5. </post>
  6. <post>
  7. <post_title>PHP – Parse YouTube URLS</post_title>
  8. <guid>http://www.phpzag.com/?p=1473</guid>
  9. </post>
  10. <post>
  11. <post_title>What is HTML5?</post_title>
  12. <guid>http://www.phpzag.com/?p=1399</guid>
  13. </post>
  14. </posts>

Now We will go for next sample URL for example:

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

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

The JSON Output
  1. {“posts”:[{"post":{"post_title":"Now YouTube Video Player Uses HTML5","guid":"http:\/\/phpzag.com\/?p=1568"}},{"post":{"post_title":"PHP – Parse YouTube URLS","guid":"http:\/\/phpzag.com\/?p=1473"}},{"post":{"post_title":"What is HTML5?","guid":"http:\/\/phpzag.com\/?p=1399"}}]}

These are simple steps to create basic web service. So You can try to create Simple Web Service of your Website.

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. Stranet
    June 3rd, 2012 at 08:01 | #1

    Hi,
    how is possible to protect a web service access?
    Is possible to “authenticate” the access to the service?

  2. June 4th, 2012 at 23:41 | #2

    Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?

  3. July 24th, 2012 at 15:26 | #3

    thx a lot for the very good example
    greetings from cologne

  4. yogesh
    August 28th, 2012 at 12:49 | #4

    thanks it;s working

  1. No trackbacks yet.