drkenap147751 2013-03-01 08:09
浏览 46
已采纳

在Codeigniter Restful服务器中加载模型功能

Recently i started width Codeigniter Restful server. I use the following script: https://github.com/philsturgeon/codeigniter-restserver.

If i trying to get a property of my model, the Rest server returns NULL. In a normal CI controller this function works. The code is:

require(APPPATH.'/libraries/REST_Controller.php'); 
class Api extends REST_Controller
{
    function user_post()  
    {  
        $username = $this->post('username');
        $password = $this->post('password');
        $company_code = $this->post('company_code');
        $key = $this->post('key');
        $this->CI =& get_instance();

        // Load user model
        $this->load->model('User_model');
        $this->load->model('Api_model');

        // Ip adres remote server
        $server_ip  = $_SERVER['REMOTE_ADDR'];

        // Ophalen data
        $user = $this->User_model->getApiLogin($username,$password);

The error eccors on the last line: $user = $this->User_model->getApiLogin($username,$password);

The Api is called through Curl:

             $key = 'Test';
         $company_code = 'econome';  
         $username = 'jellepals';  
         $password = 'test';  

         $curl_handle = curl_init();  
         curl_setopt($curl_handle, CURLOPT_URL, 'http://jelle.testapiserver.nl/api/user/format/json');
         //curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
         //curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);             
         curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);  
         curl_setopt($curl_handle, CURLOPT_POST, 1);  
         curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(  
             'key' => $key,  
             'username' => $username,
             'password' => $password));  
         $buffer = curl_exec($curl_handle);  
         curl_close($curl_handle);  
         $result = json_decode($buffer);  
         var_dump($result);

Does somebody know how i can call a model function in a Restserver? Thanks!

  • 写回答

2条回答 默认 最新

  • dongwen1935 2013-03-01 13:08
    关注

    I think the problem is in my db connexion. Someone build an extra layer betweeen db class and the model called Db_handler.php. When calling from the api this layer doesn,t work apparently. With this layer we can use multiple databases. When adding this:

       var $db;
    
        public function __construct() {
            parent::__construct();
            $this->db = $this->load->database($this->config->item('default', 'database'), TRUE);
        }
    

    To my model the connexxion works fine.

    Thanks for rhe comment MDeSilva.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作