Welcome Guest, Not a member yet? Register   Sign In
Problem W/ Phil's API on Live Server
#1

[eluser]sulli[/eluser]
Hello,

I've been trying to implement Phil's API in my web app. It works perfectly when I execute it on my local development server, but for some reason the api is not working properly on my production server.

When trying to execute the API in my web browser via http://mysite.com/api/example/user/id/1/format/json

I get the following:

&lt;xml&gt;&lt;status>0</status><error>Unknown method.</error>&lt;/xml&gt;

From my understanding this means that REST_Controller.php is not recognizing the GET request from the browser and prevents it from parsing the uri string information.

Is there a server setting that I should look at to allow the GET request to be recognized by REST_Controller.php?

Any suggestions?

This is really throwing me, because it works fine on my local development server.

Thanks for any suggestions or help.

Ryan
#2

[eluser]tpetrone[/eluser]


1. Can you post the function so we can look see.

2. Was it working before and this just started happening?

2.1 -> What REST client are you using to validate ? I personally use Firefox REST Client plugin.

3. I would say double check your function request method and make sure it's correct at the function level.

Example: www.yourservice.net/api/rest_server/id/1/format/json
Code:
function your_function_get(){

      $this->get("id")

     // do some other stuff here.

   }

vs.

Code:
function your_function_post(){

      $this->post("id")

      
     // do some other stuff here.
   }

Cheers.
#3

[eluser]sulli[/eluser]

For the controller, I'm using Phil's example.php code (just trying to get it to work):
Code:
&lt;?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Example
*
* This is an example of a few basic user interaction methods you could use
* all done with a hardcoded array.
*
* @package  CodeIgniter
* @subpackage Rest Server
* @category Controller
* @author  Phil Sturgeon
* @link  http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require (APPPATH.'/libraries/REST_Controller.php');

class Example extends REST_Controller
{
function user_get()
    {
        if(!$this->get('id'))
        {
         $this->response(NULL, 400);
        }

        // $user = $this->some_model->getSomething( $this->get('id') );
     $users = array(
   1 => array('id' => 1, 'name' => 'Some Guy', 'email' => '[email protected]', 'fact' => 'Loves swimming'),
   2 => array('id' => 2, 'name' => 'Person Face', 'email' => '[email protected]', 'fact' => 'Has a huge face'),
   3 => array('id' => 3, 'name' => 'Scotty', 'email' => '[email protected]', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
  );
  
     $user = @$users[$this->get('id')];
    
        if($user)
        {
            $this->response($user, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'User could not be found'), 404);
        }
    }
    
    function user_post()
    {
        //$this->some_model->updateUser( $this->get('id') );
        $message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function user_delete()
    {
     //$this->some_model->deletesomething( $this->get('id') );
        $message = array('id' => $this->get('id'), 'message' => 'DELETED!');
        
        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function users_get()
    {
        //$users = $this->some_model->getSomething( $this->get('limit') );
        $users = array(
   array('id' => 1, 'name' => 'Some Guy', 'email' => '[email protected]'),
   array('id' => 2, 'name' => 'Person Face', 'email' => '[email protected]'),
   3 => array('id' => 3, 'name' => 'Scotty', 'email' => '[email protected]', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
  );
        
        if($users)
        {
            $this->response($users, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any users!'), 404);
        }
    }


public function send_post()
{
  var_dump($this->request->body);
}


public function send_put()
{
  var_dump($this->put('foo'));
}
}

?&gt;



The header response that I get via the Firefox RESTClient is:

Code:
Status Code: 404 Not Found
    Connection: close
    Content-Length: 99
    Content-Type: application/xml
    Date: Thu, 28 Jun 2012 02:07:51 GMT
    MS-Author-Via: DAV
    Server: Apache
    Set-Cookie: ci_session=a:5:{s:10:"session_id";s:32:"fa59f7253449fd658cf7e1cef3743a64";s:10:"ip_addre ss";s:14:"67.171.202.197";s:10:"user_agent";s:83:"Mozilla/5.0+(Macintosh;+Intel+Mac+ OS+X+10.5;+rv:13.0)+Gecko/20100101+Firefox/13.0.1";s:13:"last_activity";i:1340849271;s:9:"user_data";s:0:"";}f74eb34e94f34b94d1761a105ec04c4b; expires=Thu, 28-Jun-2012 04:07:51 GMT; path=/
    X-Powered-By: PleskLin
    status: 404
#4

[eluser]sulli[/eluser]
This works on my local development server, but not my production server. Makes me wonder, if there is a server setting preventing this from working?
#5

[eluser]sulli[/eluser]
Okay, got it to work.

I was being stupid and forgot that last night I created a controller called api.php... duh

That interfered with my path of api/example/user/id/1/format/json

Thanks tpetrone for trying to help me.

Ryan
#6

[eluser]tpetrone[/eluser]
My next response was going to be ..

Quote: Are you using the correct url path???


Code:
server.com/path/to/api/controller/method/id/1


But you beat me to it !

Cheers.

~Tim




Theme © iAndrew 2016 - Forum software by © MyBB