Welcome Guest, Not a member yet? Register   Sign In
How to use Phil Sturgeon's REST_SERVER
#11

[eluser]PaulDunn[/eluser]
This works:
$test = $this->rest->get('property');

But this doesn't:
$test = $this->rest->get('property', array('id' => '1'),'application/json');

Code:
=============================================
REST Test

=============================================
Request

http://localhost/index.php/api/property?winman_id=1
=============================================
Response

<html>
<head>
<title>404 Page Not Found</title>
<style type="text/css">

body {
background-color:    #fff;
margin:     40px;
font-family:     Lucida Grande, Verdana, Sans-serif;
font-size:     12px;
color:     #000;
}

#content {
border:     #999 1px solid;
background-color:    #fff;
padding:     20px 20px 12px 20px;
}

h1 {
font-weight:     normal;
font-size:     14px;
color:     #990000;
margin: 0 0 4px 0;
}
</style>
</head>
<body>
<div id="content">
<h1>404 Page Not Found</h1>
<p>The page you requested was not found.</p>    </div>
&lt;/body&gt;
&lt;/html&gt;
=============================================
Call details

Array
(
    [url] => http://localhost/index.php/api/property?winman_id=1
    [content_type] => text/html
    [http_code] => 404
    [header_size] => 158
    [request_size] => 112
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.528648
    [namelookup_time] => 0.468489
    [connect_time] => 0.468644
    [pretransfer_time] => 0.468651
    [size_upload] => 0
    [size_download] => 539
    [speed_download] => 1019
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 0.52543
    [redirect_time] => 0
)

It must be the query string causing the issue. Why doesnt it produce a URL like:
http://localhost/index.php/api/property/id/1

That URL works fine the browser, cannot the client library use that instead of query strings?

Also if I post xml to the server, how do I receive it in the server post function?

Many thanks
-Paul
#12

[eluser]smilie[/eluser]
Hi,

I do not know what it works like that Sad
Can't you use POST instead of GET?

Just change Api from _get() to _post() (function) and ofcourse when calling from client.

Regarding XML - no idea - never used it (not with REST - not with anything else) Smile
Maybe Phil will get here later on...

Cheers,
Smilie
#13

[eluser]PaulDunn[/eluser]
So how would you retrieve json when you post it to the server?

I think it will be the same process for XML.

Cheers
-Paul
#14

[eluser]smilie[/eluser]
Paul,

Here is an example of my code:

Api (REST Server):
Code:
/**
     * Check if client exists, based on the e-mail address
     * Input: e-mail address
     * @return    string    $result
     */
    function clientExists_post()
    {
        # Load clients model
        $this->load->model('client_model');
        $result = $this->client_model->clientExists($this->input->post('email'));
        $this->response($result, 200);
    }

Controller which calls this Api function:
Code:
$this->load->library('rest', array('server' => REST_SERVER));
$data_check = array('email' => $this->input->post('email'));
$clientExists = $this->rest->post('clientExists',$data_check,'json');
if($this->input->post('email') != '' && $clientExists == "found")
{
   # Do something...
}
else
{
# Do something else...
}

And the model which is called from Api function:
Code:
/**
     * Check if client exists
     * @param    string    $email
     * @return    boolean    $result
     */
    function clientExists($email='')
    {
        # Load database
        $dbwild = $this->load->database('blaat',TRUE);
        # Prepare query
        $dbwild->select('client_id');
        $dbwild->from('client');
        $dbwild->where('client_username',$email);
        $query = $dbwild->get();
        if($query->num_rows() > 0)
        {
            # We have a client in DB with the given e-mail
            $result = "found";
        }
        else
        {
            # No known client
            $result = "not found";
        }
        return $result;
    }

Instead of $result = "found" / "not found" you can also do:
return TRUE / FALSE.

You can also return arrays, in that case:

Code:
$return = array('result'=>'found');

and in the rest client:

Code:
if($this->input->post('email') != '' && $clientExists->result == "found")

JSON returns object, so you access array with $data->array_key;
In case of multiple array
$data->array_key1['array_key2'] etc...

Hope this helps :-)

Cheers,
Smilie
#15

[eluser]madrico[/eluser]
is there a way to keep friendly-urls (via htaccess) and to use phil's restful server code? i had inquired about this on nettuts, but i haven't seen a reply.

thanks in advance!!
#16

[eluser]Ritesh Thumar[/eluser]
i want to fetch data from the database and want to send that data to mobile application using url. than how can i do that...????
#17

[eluser]Phil Sturgeon[/eluser]
CodeIgniter by default will 404 if you try using a query string. You have to enable query string support before you can send GET parameters to CI or REST_Controller in this way.

Once that is done, you can populate $this->get('id') with /users/method/id/1 or /users/method?id=1

$this->post() is used to fetch post data.
$this->put() is used to fetch put data.
$this->delete() is used to fetch delete data.

You can send a body to REST_Server which will be contained in $this->request->body, but you will have to format that yourself.

Hope this clears some stuff up!
#18

[eluser]Davide Bellini[/eluser]
I found a small bug in Phil's REST_SERVER library ... at line 118, is not "statuc" but "status" :
Code:
$this->response( array('statuc' => false, 'error' => 'Only AJAX requests are accepted.'), 505 );

change to :
Code:
$this->response( array('status' => false, 'error' => 'Only AJAX requests are accepted.'), 505 );

Cheers
#19

[eluser]Phil Sturgeon[/eluser]
Done cheers.
#20

[eluser]Davide Bellini[/eluser]
speedy!!! :bug:




Theme © iAndrew 2016 - Forum software by © MyBB