Reputation: 2747
I am trying to create an API request handler that can read wildcards in a string. The ideal situation is something like this.
$myClass->httpGet('/account/[account_id]/list-prefs', function ($account_id) {
// Do something with $account_id
});
Where [account_id]
is the wild card. The actual URI would look like:
http://api.example.com/account/123456/list-prefs
The actual function looks like...
function httpGet($resource, $callback) {
$URI = urldecode(str_replace('/'.$this->API_VERSION, '', $_SERVER['REQUEST_URI']));
$match = preg_match_all('/\[([a-zA-Z0-9_]+)\]/', $resource, $array);
if ($resource /*matches with wildcards*/ $URI) {
// Do something with it.
}
...
}
My problem is...
Upvotes: 1
Views: 909
Reputation: 8885
I think you are missing something like:
tokens = array('[account_id]' => '/\[([a-zA-Z0-9_]+)\]/');
Then:
function replaceTokens($resource) {
# get uri with tokens replaced for actual regular expressions and return it
}
function httpGet($resource, $callback) {
$URI = urldecode(str_replace('/'.$this->API_VERSION, '', $_SERVER['REQUEST_URI']));
$uriRegex = replaceTokens($resource);
$match = preg_match_all($uriRegex, $URI, $array);
if ($match) {
// Do something with it.
}
}
Upvotes: 1