我正在尝试创建一个可以读取字符串中的通配符的API请求处理程序。理想的情况是这样的。
$myClass->httpGet('/account/[account_id]/list-prefs', function ($account_id) {
// Do something with $account_id
});
其中[account_id]
是外卡。实际的URI看起来像:
http://api.example.com/account/123456/list-prefs
实际功能看起来像......
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.
}
...
}
我的问题是......
答案 0 :(得分:1)
我认为你错过了类似的东西:
tokens = array('[account_id]' => '/\[([a-zA-Z0-9_]+)\]/');
然后:
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.
}
}