gpt4 book ai didi

php - 如何将带有数据的 http header 发送到 rest Api codeigniter?

转载 作者:搜寻专家 更新时间:2023-10-31 21:26:26 25 4
gpt4 key购买 nike

我想向 Api 发送 http header 并获得 json 响应。

我在 books 表中有所有书籍的详细信息,我想获取所有书籍。

但我有 5 个 http header 来访问它们。

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

获取详细信息的网址:

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

Controller 代码:

    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);
}
}
}
}

模型代码:

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

我想访问按钮单击访问权限,但如何将 http header 发送到 rest api 页面并使用 codeigniter 获取所有数据?

最佳答案

使用 CURL 设置 header 。请参见下面的示例。希望对你有帮助

$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);

关于php - 如何将带有数据的 http header 发送到 rest Api codeigniter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35306874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com