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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决