从JSON执行PHP函数


Executing PHP function from JSON

我正在尝试使用JSON和PHP做一些事情,但有些事情我找不到方法,尽管我不能100%确定是否有。但因为这看起来是一个不错的选择(如果可能的话),我决定在这里问。

我有来自jquery官方网站的这些例子。有两个文件,第一个是index.php,我在这里执行Ajax,它是:

<!DOCTYPE html>
<html>
<head>
<title>Simple form sending and receiving a JSON object to/from PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  var data = 
  { 
    "sales": [ 
      { "firstname" : "John", "lastname" : "Brown" },
      { "firstname" : "Marc", "lastname" : "Johnson" }
    ] // end of sales array
  }
  var dataString = JSON.stringify(data);
  $.post('simpleformSubmi.php', { data: dataString}, showResult, "text");
});
function showResult(res)
{
  $("#fullresponse").html("Full response: " +res);
}
</script>
<div id="fullresponse"></div>
</head>
<body>

一点也不复杂。我有我的simpleformSubmit.php,它是:

<?php
$logFile = 'logFile';
$res = json_decode(stripslashes($_POST['data']), true);
error_log("result: ".$_POST['data'].", res=".json_encode($res), 3, $logFile);
error_log("'n", 3, $logFile);
//header("Content-type: text/plain");
foreach ($res as $key=>$value)
{
    $str[] = $value;
}
$functionArray ="function(){ '$a = 10; echo '$a;}";
$jsStr = $str[0][1];
echo json_encode($jsStr['firstname']);
echo '<hr />';
echo json_encode($res);
echo '<hr />';
echo json_encode($functionArray);
?>

正如您所看到的,$functionArray-实际上是一个包含PHP函数的字符串,我想使用JSON返回该函数,然后执行它。那么,真的有办法做到这一点吗?现在,在执行文件之前,我在index.php中得到的是:

"function(){ $a = 10; echo $a;}"

谢谢Lern

似乎您正试图通过JavaScript执行PHP函数。由于PHP是在服务器端执行的,因此在该上下文中执行PHP函数的唯一方法是要求服务器为您执行该函数,例如,通过执行另一个ajax调用。

类似这样的东西:

index.php

$(document).ready(function(){ 
  var data = 
  { 
    "sales": [ 
      { "firstname" : "John", "lastname" : "Brown" },
      { "firstname" : "Marc", "lastname" : "Johnson" }
    ] // end of sales array
  }
  var dataString = JSON.stringify(data);
  //Change post() success callback function to executePHP()
  $.post('simpleformSubmi.php', { data: dataString}, executePHP, "text");
  //Let's define executePHP() outside post() call for clarity
  function executePHP()
  {
      //Ask the server to execute function foo(), and get the result
      $.get("example.com/somefile.php?function=foo", function(data)
      {
          //Success function, do whatever you want.
          alert(data);
      });
  }
});

然后,在somefile.php 中

<?php
//Condition(s), if any. You could even implement this interface using REST.
//Get function to execute
if($_GET["function"] == "foo")
{
    //Output function's result.
    echo foo();
}
//The function you want to run
function foo()
{
    //Do something
    $a = 10;
    return $a;
}
?>

如果一切顺利,当JavaScript到达alert(data);语句时,您将看到10

您不能在将PHP函数作为响应发送后执行它,因为响应是在客户端接收的,而PHP是一种服务器端语言。

通常,您只会返回值。在您的示例中,您只需要返回一个包含键值对a,10的关联数组。

您可以从PHP脚本返回javascript函数,并使用eval在客户端执行,但eval'ing打开了一个充满安全漏洞的潘多拉盒子。

您不能在PHP服务器之外执行PHP代码。所以你不能在浏览器中运行它。

但是,您可以传递一个JavaScript字符串并通过eval运行它。有些人会告诉你这很糟糕,但请记住,eval曾经是解析JSON的唯一方法。

为了将某些内容发送回PHP,您必须从表单中通过、p.e通过GET或POST操作调用服务器端。但是,不,您不能通过echo执行任何服务器端内容,因为echo输出到客户端。你可以一直使用eval(http://php.net/manual/es/function.eval.php)在服务器端执行POST消息中的某些内容,但不建议这样做,因为这会打开一个很大的安全漏洞。

您已经返回了一个函数(我假设您的意思是javascript),现在您需要调用它。这可以通过使用jQuery$.postsuccess回调来完成。

尝试更改此。。

$.post('simpleformSubmi.php', { data: dataString}, showResult, "text");

$.post('simpleformSubmi.php', { data: dataString}, function(data){eval(data)}, "text");

如果它的PHP(看起来像)而不是Javascript,那么这将需要从服务器上执行。因为这只是一种服务器端语言。