dongsu3654 2016-02-10 04:31
浏览 22
已采纳

如何发送带有数据的http头来休息Api codeigniter?

I want to send http header to Api and get json response.

I have all book detail in books table and I want to get all books.

But i have 5 http header to get access them.

Client-Service,Auth-Key,Content-Type,User-ID,Authorization

Url to get details:

http://127.0.0.1/RestApi/index.php/book/

Controller Code:

    public function index() {
    $method = $_SERVER['REQUEST_METHOD'];
    if ($method != 'GET') {
        json_output(400, array('status' => 400, 'message' => 'Bad request.'));
    } else {
        $check_auth_client = $this->MyModel->check_auth_client();
        if ($check_auth_client == true) {
            $response = $this->MyModel->auth();
            if ($response['status'] == 200) {
                $resp = $this->MyModel->book_all_data();
                json_output($response['status'], $resp);
            }
        }
    }
}

Model Code:

public function book_all_data()
{
    return $this->db->select('id,title,author')->from('books')->order_by('id','desc')->get()->result();
}

I want to access to access on button click but how send http header to rest api page and get all data using codeigniter ?

  • 写回答

1条回答 默认 最新

  • douji5397 2016-02-10 05:59
    关注

    Use CURL to set headers. See below example. Hope it will help you

    $headers = array(
                     'Client-Service:CLIENT_SERVICE_DETAIL',
                     'Auth-Key:YOUR_AUTH_KEY',
                     'Content-Type:YOUR_CONTENT_TYPE',
                     'User-ID:YOUR_USER_ID',
                     'Authorization:YOUR_AUTHORIZATION',   
                     );
    
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, YOUR_URL);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_HEADER, 1); 
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
    
    $buffer = curl_exec($curl_handle);
    $header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
    $body = substr($buffer, $header_size);
    
    curl_close($curl_handle);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c