我开发了一个jQuery插件,允许你调用任何核心PHP函数甚至用户定义的PHP函数作为插件的方法: jquery.php
将jquery和jquery.php包含在我们的文档头部并将request_handler.php放在我们的服务器上之后,我们将按照下面描述的方式开始使用插件。
为了便于使用,以简单的方式参考该function:
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 DOM P.callback(false); // Both .end() and .data return data to variables var 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 array var 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 );
后端configuration提供了一个白名单,所以你可以限制哪些function可以被调用。 还有一些与插件描述的PHP一起工作的其他模式。