1 like 0 dislike
3.3k views
How to create a basic Web Service Using PHP, MySQL, XML, and JSON
in Education & Reference by (630 points) | 3.3k views

1 Answer

0 like 0 dislike

The PHP / MySQL

In webservice.php

/* 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 name 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);

}

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://domain/webservice.php?user=2&num=10
by
edited

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
2 like 0 dislike
3 answers
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Jul 7, 2014 in Education & Reference by Bhoomika (50 points) | 13.7k views
1 like 0 dislike
1 answer
asked Apr 15, 2014 in Education & Reference by Yogi (180 points) | 99 views

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,227 questions
1,373 answers
559 comments
2,099 users