Reputation: 101
I'm trying to create a restful api the only problem is that I'm unable to get $_REQUEST value. the same code work fine on localhost but not on to server. This is probably happen due to my htaccess code. When api call by url processApi
is the function that is called and find out which function of this api is called by url
For example http://www.domain.com/file.php/api/test
Now I'm trying to call test
function that is in api
class
htaccess code redirect my url
The first function inside api
class is processApi
that check wether test function exist or not
But inside processApi
function $_REQUEST value is null
This is happen on server but on localhost it work fine
This is htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ file.php?request=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ file.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ file.php [QSA,NC,L]
</IfModule>
This is the api code
class API extends REST
{
private $db = NULL;
public function __construct()
{
parent::__construct();
}
public function processApi()
{
$func = strtolower(trim(str_replace("api/","",$_REQUEST['request'])));
if((int)method_exists($this,$func) > 0)
{
$this->$func();
}
else
{
$error = array('status' => '402', "msg" => 'function not exist');
$this->response($this->json($error), 402);
}
}
}
$api = new API;
$api->processApi();
By running this code onto server i got
{"status":"402","msg":"function not exist"}
Just because $_REQUEST['request'] is null
Please point me why I'm get $_REQUEST['request']
= null. Is my htaccess code giving right value in $_REQUEST['request']
Upvotes: 1
Views: 913
Reputation: 786146
That is probably happening due to your last rule that runs after your first rule. Change your code like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# skip after rewriting first time
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ file.php?request=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule . file.php [L]
</IfModule>
Upvotes: 0
Reputation: 108
You should probably be looking at PHP's $_SERVER
variable instead of $_REQUEST
.
$_SERVER['REQUEST_URI'] should give you the correct URI for /api/test
Running var_dump
on $_SERVER gives you more information about the current request (and the server environment that is processing it)
Hope this helps!
Upvotes: 1