0

im using CodeIgniter 1.7 framework to make my Website. Another team do the mobile version for iPhone. So the other team ask me to do a web service for authentification. They tell that they will send me :

POST REQUEST 
Input : login , password, push_token
if failure authentification -> Output HTTP : error code 0
if success -> Output XML :
<DATA>
 <User id='id_user' title='..' first_name='' last_name='' email='' postal='' country_id=''/> 
</DATA>

Basically i have this function for authentification:

function check_login($username="", $password="")
{
$username = $_POST['username'];
$password = $_POST['password'];
...
...
}

im looking into this post Codeigniter web services but i don't understand how its really work because im new in webservice.

Community
  • 1
  • 1
Wassim Sboui
  • 1,692
  • 7
  • 30
  • 48

2 Answers2

5

I would consider using json as a sole output with one value being TRUE or FALSE depending on success. It's easier to handle, and all it requires is for your site to be in utf8.

If your check_login() returns TRUE, use that as the condition for success.

Example of reply:

/* Class etc. */

function do_login() {
    $arr_json = array('success' => FALSE);
    if ($this->login_library->check_login()) {
        $arr_json['success'] = TRUE;
        $arr_json['user_id'] = $this->login_library->user_id;
    }
    echo json_encode($arr_json);
}

login_library is just an example for your library which does the login. The reply they receive only needs to be decoded through json_decode(). And then simply check if the key success is TRUE or not.

If needed, have an extra key called error_message or similiar, and you can run all post-values through form_validation. Then you can also have $arr_json['error_message'] = trim(validation_errors('', "\n")); before the echo aswell.

Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
2

You should definately look into Phil Sturgeons CodeIgniter Rest Server:

https://github.com/philsturgeon/codeigniter-restserver

There's a pretty good documentation. He also made a screencast about it: http://philsturgeon.co.uk/blog/2011/03/video-set-up-a-rest-api-with-codeigniter

Furthermore you could have a look at this article, where Adam Whitney explains his approach:

Building a RESTful Service using CodeIgniter

http://adamwhitney.net/blog/?p=707

Community
  • 1
  • 1
nielsstampe
  • 1,336
  • 1
  • 15
  • 25