AuthKey in title

Thus, my client needs a REST API using PHP, which provides output according to the conditions of the URL parameters

So now there are three URLs that are currently needed, and they are done.

therefore they

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

used .htaccess for trendy URL

So now he asked AuthKey to be added to the HTTP header, and not to the URL. Thus, AuthKey should be passed as a header, and the rest of the URL parameters

So my question is how can this be done. and how to get AuthKey from the request?

Any tutorials or comments on this subject are welcome.

+4
source share
2 answers

You can tell the client when he asks your api to add a header, as shown below:

AuthKey: -api-auth-key

: -api-

php-

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

, ,

:
PHP cURL

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

php

$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;
+1

All Articles