1

我是 REST API 和 CURL 的新手,我一直在参考http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

如果我将错误代码从 403 更改为 200,我会得到输出: “发生错误”。

如果我将错误代码保留为 403,我会得到输出: 'Something has go wrong'

从做一些阅读看来,我给出某种错误代码是正确的,但我如何传回有关错误的更多细节?我应该如何回应?

我的 API

public function login_put() {
    $valid = $this->membership_model->validate_username($this->put('username'));
    if($valid !== TRUE){
        $this->response(array('status' => 'error', 'msg' => 'error_details'), 403);  
    } 
}

我的 CURL 测试仪

function curl_put()  
{  
    $this->load->library('curl');  
    $this->curl->create('http://localhost/api/login/format/json');  

    $this->curl->put(array(  
        'username' => 'my_username' 
    ));  

    $result = json_decode($this->curl->execute());  

    if( isset($result->status) && $result->status == 'error' ) { 
        echo 'Error occured';
    } else {  
        echo 'Something has gone wrong';  
    }
} 
4

1 回答 1

1

看起来您在 curl 网址中缺少参考。如果 login_put 所在的文件名为 login.php,则您的 url 应如下所示:

http://localhost/api/login/login/format/json  

首先,我们有您的域,然后是对 API 的引用。第一个登录是指 api 文件夹中的控制器,第二个是指您定义的函数名称(login_put)

于 2012-08-24T22:35:31.827 回答