标题中的AuthKey

时间:2015-05-25 06:05:27

标签: 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用于花哨的网址

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

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

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

2 个答案:

答案 0 :(得分:3)

你可以告诉客户当他请求你的api他添加一个标题如下:

AuthKey:你的api-auth-key

令牌:你的api-token

然后在你的php代码中生成

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

然后你检查数据库中的密钥是否然后处理你的代码

注意:
您的客户端可以使用PHP cURL

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

答案 1 :(得分:1)

您可以使用此代码连接php中的rest

$url = 'localhost/newapi/client/';
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => "Content-Type: application/json\r\n"."Authorization: Basic ".<authkey>."\r\n",
            'content' => $data,
            'timeout' => 60
            )
        );

    $context  = stream_context_create($opts);
    $result = file_get_contents($url, false, $context, -1, 40000);

    return $result;