标题中的AuthKey

Kar*_* Nk 6 php authentication rest .htaccess

所以我的客户端需要一个使用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
Run Code Online (Sandbox Code Playgroud)

用.htaccess用于花哨的URL

所以现在他要求将AuthKey添加到HTTP头而不是URL.因此,必须将AuthKey作为头部传递,其余部分作为URL参数传递

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

欢迎任何有关此问题的教程或评论

Moh*_*lal 6

你可以告诉客户,当他请求你的 api 时,他添加一个标头,如下所示:

AuthKey:your-api-auth-key

Token:your-api-token

然后在你的 php 代码中 make

$headers = getallheaders();
$token = $headers['Token'] or ['AuthKey'];
Run Code Online (Sandbox Code Playgroud)

然后检查数据库中的密钥,然后处理您的代码

注意:
您的客户端可以使用 PHP cURL 添加标头

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