Welcome Guest, Not a member yet? Register   Sign In
Porblem in using REST server
#1

[eluser]sheri.nust[/eluser]
Hi guys i have made REST client with curl library.

From client , i have sent request to REST server, but don't know how to handle request on server side,

On client , my code is bellow

Code:
<?
class service_REST extends Controller
{
     protected $rest_format = NULL;
    
    private $_url;
    private $_method;
    private $_format;
    
    private $_headers;
    private $_data;
    private $_args;
    
    
    /**
     * Back office service_REST Constructor
     *
     * @return service_REST
     */
    function service_REST()
    {
        parent::Controller();
        
    }
    
    /**
     * Main Page to handle requests
     * @param Page Name
     */
    function index()
    {
        this->distributeItemToPOS(1);
     }
    
    function distributeItemToPOS($storeid)
    {
        
         $_format="json";
        $rest_format=$_format;
        
    $_url = 'http://localhost/~khurram/exxilco/index.php/service/service_REST/';
        //where service is sub folder in controller folder, where i placed my controller
         //service_REST is the name of controller and by default its index function will be called
            $_method = 'GET';
            $_headers = array(
            'Accept: application/json',
            'Content-Type: application/json',
            'somekey: somevalue'
            );
            $_data = array(
            'firstName'=> 'John',
            'lastName'=> 'Doe'
            );
            
            
            # json encode _data (depends on the API you're working with)
            $_data = json_encode($_data);
            $handle = curl_init();
            curl_setopt($handle, CURLOPT_URL, $_url);
            curl_setopt($handle, CURLOPT_HTTPHEADER, $_headers);
            curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
            
            switch($_method)
            {
                case 'POST':
                curl_setopt($handle, CURLOPT_POST, true);
                curl_setopt($handle, CURLOPT_POSTFIELDS, $_data);
                break;
                
                case 'PUT':
                curl_setopt($handle, CURLOPT_PUT, true);
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                break;
                
                case 'DELETE':
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
              
            }
            
            $response = curl_exec($handle);
            $response = json_decode($response, true);
            
            echo "response:".$response.":";
                 //if I echo response , then its empty

            $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);    
            
            echo "<br>".$code;
               // but then I echo code , then it is 200 , then means that request
               //  has been sent successfully
            
            $response_info = curl_getinfo( $handle );
            
            echo "<br>response_info:<br>";
            
            print_r($response_info);

/*
the result is as follows            
Array ( [url] => http://localhost/~khurram/exxilco/index.php/service/service_REST/ [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 552 [request_size] => 165 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 15.000211 [namelookup_time] => 0.000224 [connect_time] => 0.000344 [pretransfer_time] => 0.00043 [size_upload] => 0 [size_download] => 695 [speed_download] => 46 [speed_upload] => 0 [download_content_length] => 695 [upload_content_length] => 0 [starttransfer_time] => 15.000138 [redirect_time] => 0 )            
        
*/    

}
    

}
?&gt;


And on server side , till now what i did is as follows

Code:
&lt;?

class service_REST extends Controller
{
    
    
    /**
     * POS service_REST Constructor
     *
     * @return service_REST
     */
    function service_REST()
    {
        parent::Controller();
    }
    
    /**
     * Main Page to handle requests
     * @param Page Name
     */
    function index()
    {
            
        $_data = array(
            'POSuser'=> 'John',
            'POSpass'=> 'Doe'
            );
        
        return $_data;
        
    }
    
    
    

}
?&gt;

So kindly if anyone tell me how i handle request coming from client at server end using REST protocol
.
#2

[eluser]Phil Sturgeon[/eluser]
Have you had a look at my REST server code? I have already created a very sturdy REST client library but looks like I forgot to ever release the damn thing!

I'll have to have another pop at them soon.
#3

[eluser]sheri.nust[/eluser]
ya i have seen your server code,

That was little confusing for me as i am new baby in CI,

So i started using CRUL in PHP,

Can u help me in writing server code using above CURL client,

OR what you suggest to create REST webservice and what needs to be done on server side?

Me stucked in my work.

Thanks
#4

[eluser]Phil Sturgeon[/eluser]
cURL is used to fetch content from and interactive with another server. You cannot create a REST service with cURL, only fetch.

First you need to read up on how REST works, then look at some examples, then read my blog post on it. Although REST is simple, you should not be diving right into this with no understanding in how REST should work and hardly any CodeIgniter knowledge.

Some research now will save you a LOT of time in development problems. ;-)
#5

[eluser]sheri.nust[/eluser]
Opps..

Thanks for CURL advise....

Actually i am searching on REST creating web service in PHP,
And getting with the time that how REST works.

found very usefull links like

http://www.slideshare.net/greghines/rest...el=1130184

http://www.slideshare.net/mgirouard/crea...s-in-php-5




Theme © iAndrew 2016 - Forum software by © MyBB