简体   繁体   English

在codeigniter控制器中使用REST API

[英]Consume a REST API in codeigniter controller

I have create a REST API and want to consume my own created API in codeigniter controller. 我已经创建了一个REST API,并想在codeigniter控制器中使用自己创建的API。

My created REST API controller(example.php) 我创建的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);
    }
    }

model(user.php) 模型(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();
    }
    }

Here i want to call my created api(from example.php)for fetch record in welcome.php controller with basic authentication(uname-admin,pwd-1234) 在这里我想通过基本身份验证(uname-admin,pwd-1234)调用我创建的api(来自example.php)以在welcome.php控制器中获取记录

my controller welcome.php 我的控制器welcome.php

public function index()
{
}

Can anybody help to me that how to call my api in controller welcome.php with basic authentication. 任何人都可以帮助我,如何使用基本身份验证在控制器welcome.php中调用我的api。

Using CURL you can consume any API/network call. 使用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);
?>

But why are you calling your own API this way? 但是,为什么要这样调用自己的API? You can simply call your API model and perform your operations 您只需调用API模型并执行操作即可

Add below to your curl options 在下方添加您的卷曲选项

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

also update 也更新

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

in rest.php file inside config folder of your codeigniter settings. 在您的codeigniter设置的config文件夹内的rest.php文件中。 By default it is 'X-API-KEY' 默认情况下是“ X-API-KEY”

This may help to somebody else looking for a solution, if OP has resolved it himself/herself. 如果OP自己解决了问题,这可能对寻求解决方案的其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM