首页 > 解决方案 > PHP文件如何通过这个回调参数知道和处理一个函数?

问题描述

我正在寻找从 PHP 文件获取数据 JSON 的方法,而不知道 PHP 文件正在处理的函数。

<script>
function clickButton(){
    var s = document.createElement("script");
    s.src = "jsonp_demo_db.php?callback=myDisplayFunction";
    document.body.appendChild(s);
}
</script>

标签: phpjsonp

解决方案


使用 get 请求发送函数名称并检查 Get 值是否存在和使用call_user_func($functionName),如果有函数参数可以使用call_user_func_array($functionName , $pramaters)

`function test()
{
  echo "string";
}

if ($_GET['func']) {
  $func = $_GET['func'];
  call_user_func($func);
}`

您还可以使用 function_exists 来确保该函数是否存在并且此函数返回 False 或 True 此代码示例:

if(function_exists('my_function'))
{
throw new Exception("'my_function' is already defined!");
}

function my_function()
{
  // Do the work here
}

推荐阅读