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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料