我该如何执行该transaction(123)功能?
通过API的响应是: transaction(123)
我将它存储在$responsevarible中.
<?php
function transaction($orderid) {
return $orderid;
}
//api response
$response = "transaction(123)";
try {
$orderid = call_user_func($response);
echo $orderid;
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
根据手册页面call_user_func()应该在您的用例中使用两个参数调用.
$orderid = call_user_func('transaction', 123);
Run Code Online (Sandbox Code Playgroud)
这意味着您必须从$response变量中单独提取函数和参数:
preg_match('/([\w\_\d]+)\(([\w\W]*)\)/', $response, $matches);
Run Code Online (Sandbox Code Playgroud)
将导致$matches数组包含索引1处的函数名称和索引2处的参数.
所以你会这样做:
$orderid = call_user_func($matches[1], $matches[2]);
Run Code Online (Sandbox Code Playgroud)
显然,如果值来自不受信任的来源,则需要非常小心.