【问题标题】:Consume a REST API in codeigniter controller在 codeigniter 控制器中使用 REST API
【发布时间】:2018-08-13 09:51:58
【问题描述】:

我已经创建了一个 REST API,并且想在 codeigniter 控制器中使用我自己创建的 API。

我创建的 REST API 控制器(example.php)

   class Example extends REST_Controller {

    public function __construct() { 
    parent::__construct();        
    $this->load->model('user');
   }

     public function user_fetch_post() {
    //returns all rows if the id parameter doesn't exist,
    //otherwise single row will be returned
    $id = $this->input->post('id');
    $users = $this->user->getRows($id);

    //check if the user data exists
    if(!empty($users)){
        //set the response and exit
        $this->response($users, REST_Controller::HTTP_OK);
    }else{
        //set the response and exit
        $this->response([
            'status' => FALSE,
            'message' => 'No user were found.'
        ], REST_Controller::HTTP_NOT_FOUND);
    }
    }

模型(user.php)

    function getRows($id = ""){
    if(!empty($id)){
        $query = $this->db->get_where('users', array('id' => $id));
        return $query->row_array();
    }else{
        $query = $this->db->get('users');
        return $query->result_array();
    }
    }

在这里我想调用我创建的 api(来自 example.php)以获取带有基本身份验证的welcome.php 控制器中的记录(uname-admin,pwd-1234)

我的控制器welcome.php

public function index()
{
}

任何人都可以帮助我如何使用基本身份验证在控制器welcome.php 中调用我的api。

【问题讨论】:

  • 你能告诉我你的rest api类完整代码
  • 现在我更新了我的帖子,请在上面查看。

标签: rest codeigniter codeigniter-query-builder codeigniter-restapi


【解决方案1】:

使用 CURL,您可以使用任何 API/网络调用。

<?php
    $headers = array(
        'Content-Type:application/json',
        'Authorization: Basic '. base64_encode("user:password") // place your auth details here
    );
    $payload = array(
        'id' => 1,
    );

    $process = curl_init($host); //your API url
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($process, CURLOPT_HEADER, 1);
    curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_POST, 1);
    curl_setopt($process, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    $return = curl_exec($process);
    curl_close($process);

    //finally print your API response
    print_r($return);
?>

但是你为什么要这样调用你自己的 API?您可以简单地调用您的 API 模型并执行您的操作

【讨论】:

  • 我使用你的代码如下 $headers = array( 'Content-Type:application/json', 'Authorization: Basic '.base64_encode("admin:1234") ); $payload = array('id' => 1, ); curl_setopt($process, CURLOPT_USERPWD, 'admin' . ":" . '1234');但它显示错误“无效的 API 密钥”我的 api 密钥是 admin@123,我如何将 api 密钥放在这里。
【解决方案2】:

在您的 curl 选项中添加以下内容

curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'APIKEY: admin@123',
  'Content-Type: application/json',
   ));

还有更新

$config['rest_key_name'] = 'APIKEY';

在您的 codeigniter 设置的 config 文件夹内的 rest.php 文件中。默认为“X-API-KEY”

如果 OP 自己解决了问题,这可能有助于其他人寻找解决方案。

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 2012-07-16
    • 2011-10-01
    • 1970-01-01
    • 2021-10-26
    • 2018-01-09
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多