有 Javascript 和 PHP AJAX 接口生成建议吗,比如 thrift?
我基本上是在寻找 Apache Thrift,但要在 Ajax 上的 JavaScript 和 PHP 之间进行讨论。 我知道 Thirft 会生成这两者,但据我所知,JavaScript 代码必须通过 JSONProtocol 进行通信,而该协议尚未用 PHP 编写。
还有其他可以建议的替代方案吗?
如果您不熟悉 Thrift,这是我需要的一个简单的定义:
将其视为通用接口定义语言 (IDL),我在其中设置一个 User 对象、一个 AuthenticationResult 结果对象和名为 UserCommands.Authenticate 的方法();
struct User {
1: number id,
2: string firstName,
3: string lastName
}
struct AuthenticationResult {
1: number currentTime,
2: User user
}
service UserCommands {
AuthenticationResult Authenticate(1:string username, 2:string password)
}
我运行一个程序或其他东西,它根据上面的内容创建 JS 和 PHP 库。
然后,在 JS 中,我可以调用(通过有用的类型提示)。
var myAuthResult = UserCommands.Authenticate('myUser', 'myPass');
alert ("My first name is : " + myAuthResult.user.firstName);
在 PHP 中,我会在 UserCommands 类中设置一个方法,如下所示:
function Authenticate($username, $password) {
$myUser = new User();
$myUser->firstName = "Fred";
$myUser->lastName = "Thompson";
$myAuthResult = new AuthenticationResult ();
$myAuthResult->currentTime = date("U");
$myAuthResult->user = $myUser;
return $myAuthResult;
}
好处是 PHP 可以返回本机对象,而 JS 可以期望接收自己的本机对象。 整个过程中提供了可用方法的类型提示,以及预期的参数和返回结果。
任何想法将不胜感激!
I am basically looking for Apache Thrift, but to talk between JavaScript over Ajax and PHP.
I know Thirft generates both, but to my knowledge the JavaScript code must talk over JSONProtocol, of which the protocol isn't yet wrote in PHP.
Are there any other alternatives that can suggested?
If you are unfamiliar with Thrift, this is a simple(ish) definition of what i need:
Consider this as the generic interface definition language (IDL), where I setup a User object, an AuthenticationResult result object, and method named UserCommands.Authenticate();
struct User {
1: number id,
2: string firstName,
3: string lastName
}
struct AuthenticationResult {
1: number currentTime,
2: User user
}
service UserCommands {
AuthenticationResult Authenticate(1:string username, 2:string password)
}
I run a program or something, it creates JS and PHP libraries based on the above.
Then, in JS, I could call (with helpful typehinting).
var myAuthResult = UserCommands.Authenticate('myUser', 'myPass');
alert ("My first name is : " + myAuthResult.user.firstName);
And in PHP, I would setup a method in a UserCommands class like this:
function Authenticate($username, $password) {
$myUser = new User();
$myUser->firstName = "Fred";
$myUser->lastName = "Thompson";
$myAuthResult = new AuthenticationResult ();
$myAuthResult->currentTime = date("U");
$myAuthResult->user = $myUser;
return $myAuthResult;
}
The benefits are that PHP can return native objects and JS can expect to receive its own native objects.
Type hinting for available methods are provided through out, with expected params and return results.
Any thoughts would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,php中有
json_encode
和json_decode
函数。其次,有一个针对本机 php 类型的序列化/反序列化
不过,我不明白你的意思是“......其中协议尚未用 PHP 编写。”
另外,还有一个 Haxe 语言,可以“编译”为 PHP 和 JavaScript(以及其他一些语言)
First of all, there're
json_encode
andjson_decode
functions in php.Secondly, there's a serialize/unserialize for native php types
I don't understand, though, which you mean under "... of which the protocol isn't yet wrote in PHP."
Also, there's a Haxe langauge, which can be "compiled" into both PHP and JavaScript (and some other languages)