慕雪9262066
我开发了一个jQuery插件,允许您调用任何核心PHP函数,甚至用户定义的PHP函数作为插件的方法:jquery.php在文档头中包含jQuery和jquery.php并将requesthandler.php放在服务器上之后,我们将以下面描述的方式开始使用该插件。为了便于使用,请以一种简单的方式引用该功能: var P = $.fn.php;然后初始化插件:P('init', {
// The path to our function request handler is absolutely required
'path': 'http://www.YourDomain.com/jqueryphp/request_handler.php',
// Synchronous requests are required for method chaining functionality
'async': false,
// List any user defined functions in the manner prescribed here
// There must be user defined functions with these same names in your PHP
'userFunctions': {
languageFunctions: 'someFunc1 someFunc2'
}});现在有一些使用场景:// Suspend callback mode so we don't work with the DOMP.callback(false);// Both .end() and .data return data to variablesvar strLenA = P.
strlen('some string').end();var strLenB = P.strlen('another string').end();var totalStrLen = strLenA + strLenB;console.log( totalStrLen );
// 25// .data Returns data in an arrayvar data1 = P.crypt("Some Crypt String").data();console.log( data1 );
// ["$1$Tk1b01rk$shTKSqDslatUSRV3WdlnI/"]演示PHP函数链接:var data1 = P.strtoupper("u,p,p,e,r,c,a,s,e").strstr([], "C,A,S,E").explode(",", [], 2).data();var data2 = P.strtoupper("u,p,p,e,r,c,a,s,e").
strstr([], "C,A,S,E").explode(",", [], 2).end();console.log( data1, data2 );演示如何发送PHP伪代码的JSON块:var data1 =
P.block({
$str: "Let's use PHP's file_get_contents()!",
$opts:
[
{
http: {
method: "GET",
header: "Accept-language: en\r\n" +
"Cookie: foo=bar\r\n"
}
}
],
$context:
{
stream_context_create: ['$opts']
},
$contents:
{
file_get_contents: ['http://www.github.com/', false, '$context']
},
$html:
{
htmlentities: ['$contents']
}}).data();
console.log( data1 );后端配置提供了一个白名单,因此可以限制可以调用哪些函数。还有其他一些使用PHP的模式,这些模式也是由插件描述的。