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条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类