Thursday, May 14, 2015

How to create webservices in PHP tutorial


Here  I am showing one example of creating  the webservices  in php

First  you need to download the  soap library   to create the  webservies  on the server
You can download it from the link  http://sourceforge.net/projects/nusoap/
After that  you need to create  server.php file on you server
Server.php



<?php
//call library
require_once ('lib/nusoap.php');   // include the soap liberary here  

function getProd($category) {
    if ($category == "movies") {    //  here I am checking the  coming value from category is movies or not
        return join(",", array(
            "The is webservices test",
            " Writing  webservice  I am here ",
            "Build dynamic website with me"));          //  if  category is movies  then it will send this result to  your server
    }
    else {
            return "No products listed under that category";   // if there is other category then movies it will show this message
    }
}
 $server = new soap_server();
$server->register("getProd");
$server->service($HTTP_RAW_POST_DATA);

?> 



Need to create the files  newservice.php



<?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('database name,$link) or die('Cannot select the DB');

                /* grab the posts from the db */
                $query = "SELECT post_title, guid FROM table  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);
}
?>




Now we need to create file   client.php

if(isset($_REQUEST['get_data']))
  {
   
$userid = $_REQUEST['userid'];
$count  = $_REQUEST['count'];

$url = “Api file path/newservice.php?user=$userid&num=$count&format=json";           

$client = curl_init($url);  
                 
                curl_setopt($client,CURLOPT_RETURNTRANSFER,1);
// get responce
$response = curl_exec($client);               
// decode
//echo "<pre>";
//print_r($response);
$result = json_decode($response);         //  to decode the json result   
//echo "<pre>";
//print_r($result);
}




<form method="post">
    User ID <input type="text" name="userid" value="">
                Number <input type="text" name="count" >
                <input type="submit" name="get_data" value="Get Data">
</form>



 Incoming results will show here   on your file .

foreach($result as $rr)
   {
  
   foreach($rr as $kk)
    {

       echo $kk->post->post_title."<br/>";
                                 //echo $k;
                                // echo $rr;
                                //echo "<pre>";
                                //print_r($kk);
                               
                                 //echo $rr[$k]->post->post_title ."<br/>";
                 }
   

  }

No comments:

Post a Comment