I'm trying to develop REST Api with php and I have problem to get api token in my php file but it is consider as folder directory when I requested the Url ! Is there any way to solve this problem ?
e.p. Url that I call :
note : this is api token : 6A4C426B70694E6D3831785155304F566C6359636167485079494C56624C4C524B686136374E6F6D4D51453D
inside my index.php in "v1" folder:
<?php
//get verify vals
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$url = "https://";
else
$url = "http://";
// Append the host(domain name, ip) to the URL.
$url.= $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$url.= $_SERVER['REQUEST_URI'];
echo $url;
var_dump(parse_url($url, PHP_URL_PATH));
?>
thanks
i tried to learn about headers !
CodePudding user response:
The cleanest way to achieve this would be to create a .htaccess file at the root of your server, and add this rewriting rule in it:
RewriteEngine On
RewriteRule ^v([0-1])\/([0-9A-Z] )\/?$ index.php?version=$1&token=$2 [QSA]
Then, in your index.php file, you will simply get the version number in a $_GET['version'] variable, and the token in a $_GET['token'] variable.
Because you apparently want to anticipate having different versions of your API, you could also do this:
RewriteEngine On
RewriteRule ^v1\/([0-9A-Z] )\/?$ index-v1.php?token=$1 [QSA]
RewriteRule ^v2\/([0-9A-Z] )\/?$ index-v2.php?token=$1 [QSA]
In that case, you will only have access to the token in a $_GET['token'] variable, the version number being useless to have in a get variable because you would separate the code of your v1, v2 (etc) or your API in two different files.
If you don't know what these special chains of characters are, they are called "Regex" and I encourage you to read/watch a tutorial about it, you can use them in Apache, PHP, JavaScript, ... many languages, and they are a good way to identify simple to complex patterns of strings.