Thursday 15 July 2010

ajax - PHP POST to RESTful API implemented with apache mod_rewrite -



ajax - PHP POST to RESTful API implemented with apache mod_rewrite -

i created php restful api next this example (code @ bottom). understand, api converts non-existing uri components parameters in address via .htaccess file this:

<ifmodule mod_rewrite.c> rewriteengine on rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule api/v1/(.*)$ api/v1/api.php?request=$1 [qsa,nc,l] </ifmodule>

so if do, e.g.:

http://mysite/api/v1/endpoint1/param1

endpoint1/param1 can parsed in api implementation phone call php function endpoint1(param1).

now issue is, param1 long, , want utilize ajax post param1, post doesn't go through

for example, restful api:

http://mysite/api/v1/endpoint1

i posted data using ajax follows:

$.post('http://mysite/api/v1/endpoint1/',data,callback,'json');

in api.class.php, $_server['reqeuest_method'] 'post', $_post empty array.

my questions are:

what happening when post virtual url handled mod_rewrite. end result get request mode_rewrite or post request ajax call?

how can modify code posted info through? (is possible inquire mod_rewrite utilize post method instead?)

i confused here, pointers appreciated.

the interface relevant rest api (api.class.php, please see the example finish code):

<?php abstract class api { protected $method = ''; protected $endpoint = ''; protected $args = array(); protected $file = null; public function __construct($request) { header("access-control-allow-orgin: *"); header("access-control-allow-methods: *"); header("content-type: application/json"); $this->args = explode('/', rtrim($request, '/')); $this->endpoint = array_shift($this->args); $this->method = $_server['request_method']; if ($this->method == 'post' && array_key_exists('http_x_http_method', $_server)) { if ($_server['http_x_http_method'] == 'delete') { $this->method = 'delete'; } else if ($_server['http_x_http_method'] == 'put') { $this->method = 'put'; } else { throw new exception("unexpected header"); } } switch($this->method) { case 'delete': case 'post': $this->request = $this->_cleaninputs($_post); $this->args[] = ???; //problem line: how add together post args break; case 'get': $this->request = $this->_cleaninputs($_get); break; case 'put': $this->request = $this->_cleaninputs($_get); $this->file = file_get_contents("php://input"); break; default: $this->_response('invalid method', 405); break; } } public function processapi() { if ((int)method_exists($this, $this->endpoint) > 0) { homecoming $this->_response($this->{$this->endpoint}($this->args)); } homecoming $this->_response("no endpoint: $this->endpoint", 404); } private function _response($data, $status = 200) { header("http/1.1 " . $status . " " . $this->_requeststatus($status)); homecoming json_encode($data); } private function _cleaninputs($data) { $clean_input = array(); if (is_array($data)) { foreach ($data $k => $v) { $clean_input[$k] = $this->_cleaninputs($v); } } else { $clean_input = trim(strip_tags($data)); } homecoming $clean_input; } private function _requeststatus($code) { $status = array( 200 => 'ok', 404 => 'not found', 405 => 'method not allowed', 500 => 'internal server error', ); homecoming ($status[$code])?$status[$code]:$status[500]; } } ?>

i believe issue running into: .htaccess - possible redirect post data?

you should alter line in .htaccess file from: rewriterule api/v1/(.*)$ api/v1/api.php?request=$1 [qsa,nc,l]

to: rewriterule api/v1/(.*)$ api/v1/api.php?request=$1 [qsa,nc,p]

php ajax .htaccess mod-rewrite

No comments:

Post a Comment