Advertisement
Guest User

webservice

a guest
Apr 20th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php  
  2. <pre>/* require the user as the parameter */
  3. <pre>//http://localhost:8080/sample1/webservice1.php?user=1
  4. if(isset($_GET['user']) && intval($_GET['user'])) {
  5.   /* soak in the passed variable or set our own */
  6.   $number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
  7.   $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
  8.   $user_id = intval($_GET['user']); //no default
  9.   /* connect to the db */
  10.   $link = mysql_connect('10.1.2.102','u170957296_fuad','super') or die('Cannot connect to the DB');
  11.   mysql_select_db('u170957296_task',$link) or die('Cannot select the DB');
  12.  
  13.   /* grab the posts from the db */
  14.   //$query = "SELECT post_title, guid FROM wp_posts WHERE post_author =
  15.   //  $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
  16.   $query = "SELECT * FROM task_table_3;";
  17.   $result = mysql_query($query,$link) or die('Errant query:  '.$query);
  18.  
  19.   /* create one master array of the records */
  20.   $posts = array();
  21.   if(mysql_num_rows($result)) {
  22.     while($post = mysql_fetch_assoc($result)) {
  23.       $posts[] = array('post'=>$post);
  24.     }
  25.   }
  26.   /* output in necessary format */
  27.   if($format == 'json') {
  28.     header('Content-type: application/json');
  29.     echo json_encode(array('posts'=>$posts));
  30.  
  31.   }
  32.   else {
  33.     header('Content-type: text/xml');
  34.     echo '';
  35.     foreach($posts as $index => $post) {
  36.       if(is_array($post)) {
  37.         foreach($post as $key => $value) {
  38.           echo '<',$key,'>';
  39.           if(is_array($value)) {
  40.             foreach($value as $tag => $val) {
  41.               echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
  42.             }
  43.           }
  44.           echo '</',$key,'>';
  45.         }
  46.       }
  47.     }
  48.     echo '';
  49.   }
  50.   /* disconnect from the db */
  51.   @mysql_close($link);
  52. }
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement