2017-08-02 18 views
0

我是CodeIgniter和Rest API的新手。我试图在CodeIgniter中实现REST API,并使用了Phil Sturgeon的rest-client和rest-server。我看了几个教程,并成功实施了Rest-Server部分(使用Chrome的Rest Client APP进行检查)。但是,为了实施Rest-Client,我遇到了一些麻烦。如何使用codeigniter中的rest_client调用

我需要有卷曲和CodeIgniter的curl库? 如果是,我应该如何设置它?

I watched this tutorial too by Phil Sturgeon但在本教程中,他只用了REST的客户端函数来调用服务器。但没有定义放在哪里。这里的代码

function rest_client($id){ 
    $this->load->library('rest', array(
     'server' => 'http://localhost/rest/index.php/restgetcontroller/', 

    )); 

    $user = $this->rest->get('user', array('id' => $id), 'json'); 

    echo $user->name; 
} 

如果太简单,我很抱歉。

谢谢

编辑:我做了一个客户控制器,并把有一个方法来调用它。但是当我加载页面时,我得到这个错误。

Call to undefined method CI_Loader::spark() 

回答

0

无论你需要找回您的API值可以使用。

$user将有你可以使用你的目的的值。

基本上,你可以使用,你用惯了一个型号的API,因为现在与数据库的交互与API的,而不是直接从您的控制器。

0

要叫你需要卷曲RESTful API中,有一个名为Guzzlehttp,以更有效地使用curl库。

您可以使用composer安装库或只是下载zip并要求它在你的控制器。

实例应用:

try { 
     $guzzleHttp = new GuzzleHttp\Client([ 
      'verify' => false 
     ]); 

     $http_response = $guzzleHttp->request('GET', 'http://localhost/rest/index.php/restgetcontroller/'); 
     $response = json_decode($http_response->getBody()->getContents()); 

     return $data; 
    } catch (Exception $e) { 
     log_message('error', $e->getMessage()); 
     return false; 
    }