ajax交互php class,AJAX request and PHP class functions

For what it is worth, I have used a PHP proxy file that accepts an object as a post -- I will post it here. It works by providing class name, method name, parameters (as an array) and the return type. This is limited as well to only execute classes specified and a limited set of content types to return.

// =======================================================================

$allowedClasses = array("lnk","objects"); // allowed classes here

// =======================================================================

$raw = file_get_contents("php://input"); // get the complete POST

if($raw) {

$data = json_decode($raw);

if(is_object($data)) {

$class = $data->class; // class: String - the name of the class (filename must = classname) and file must be in the include path

$method = $data->method; // method: String - the name of the function within the class (method)

@$params = $data->params; // params: Array - optional - an array of parameter values in the order the function expects them

@$type = $data->returntype; // returntype: String - optional - return data type, default: json || values can be: json, text, html

// set type to json if not specified

if(!$type) {

$type = "json";

}

// set params to empty array if not specified

if(!$params) {

$params = array();

}

// check that the specified class is in the allowed classes array

if(!in_array($class,$allowedClasses)) {

die("Class " . $class . " is unavailable.");

}

$classFile = $class . ".php";

// check that the classfile exists

if(stream_resolve_include_path($classFile)) {

include $class . ".php";

} else {

die("Class file " . $classFile . " not found.");

}

$v = new $class;

// check that the function exists within the class

if(!method_exists($v, $method)) {

die("Method " . $method . " not found on class " . $class . ".");

}

// execute the function with the provided parameters

$cl = call_user_func_array(array($v,$method), $params );

// return the results with the content type based on the $type parameter

if($type == "json") {

header("Content-Type:application/json");

echo json_encode($cl);

exit();

}

if($type == "html") {

header("Content-Type:text/html");

echo $cl;

exit();

}

if($type == "text") {

header("Content-Type:text/plain");

echo $cl;

exit();

}

}

else {

die("Invalid request.");

exit();

}

} else {

die("Nothing posted");

exit();

}

?>

To call this from jQuery you would then do:

var req = {};

var params = [];

params.push("param1");

params.push("param2");

req.class="MyClassName";

req.method = "MyMethodName";

req.params = params;

var request = $.ajax({

url: "proxy.php",

type: "POST",

data: JSON.stringify(req),

processData: false,

dataType: "json"

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值