php - header 中的 AuthKey

标签 php .htaccess rest authentication

所以我的客户需要一个使用 PHP 的 REST API,它根据 URL 参数的条件提供输出

现在基本上需要三个 URL,它们已经完成。

他们是

localhost/newapi/client/<AuthKey> - for authorizing

localhost/newapi/client/<clientid>/categories/ - to get all the categories

localhost/newapi/client/<clientid>/categories/<categoryid> - to get all items in a category

将 .htaccess 用于精美的 URL

所以现在他要求需要将 AuthKey 添加到 HTTP header 而不是 URL。所以 AuthKey 必须作为 header 传递,其余作为 URL 参数传递

所以我的问题是如何做到这一点。以及如何从请求中检索 AuthKey?

欢迎任何关于这个问题的教程或评论

最佳答案

您可以在客户请求您的 api 时告诉他添加如下 header :

AuthKey:你的-api-auth-key
或者
token :您的 API token

然后在你的 php 代码中做

$headers = getallheaders();
$token = $headers['Token'] or ['AuthKey'];

然后你检查数据库中的 key 然后处理你的代码

注意:
您的客户可以使用 PHP cURL 添加 header

curl_setopt($curl-handle, CURLOPT_HEADER, array(
'Token' => 'client-auth-token', //or
'AuthKey' => 'client-auth-token'
));

关于php - header 中的 AuthKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30432132/

相关文章:

node.js - 查询 "AND"Sqlite 3 REST

php - JavaScript 元素样式随动态 id 变化

.htaccess - 如何在 litespeed 服务器上的 url 中隐藏 index.php

每个页面上的 PHP 函数导致重定向循环

php - .htaccess 网址 : rewrite missing css javascript and images file at local host Xampp

rest - 微服务间通信使用 Kafka 代替 Rest

php - 在 PHP 中,如何在插入 MySQL 表之前转义字符串中的单引号?

php - TinyMCE : How to insert snippets in editor which will be replaced by plain PHP?

php - 使用 DOMDocument,是否可以获取某个 DOM 中存在的所有元素?

javascript - Google Data Studio 连接器和应用脚本