unlimix/yii2-json-rpc

v1.1.4 2014-11-14 13:11 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:51:48 UTC


README

Yii2 的 JsonRpc 服务器和客户端

##服务器用法

  1. 使用 Composer 安装
"require": {
    "unlimix/yii2-json-rpc": "dev-master",
},

php composer.phar update
  1. 将操作添加到控制器
public function actions()
{
    return array(
        'index' => array(
            'class' => '\unlimix\jsonRpc\Action',
        ),
    );
}

public function sum($a, $b) {
	return $a + $b;
}
  1. 测试
function sendRPC(){
		$.ajax({
			url: 'YOUR URL',
			data: JSON.stringify({
				"jsonrpc": "2.0",
				"id": '<?php echo md5(microtime()); ?>',
				"method": "sum",
				"params": [1, 2]
			}),
			type: 'POST',
			dataType: 'JSON',
			contentType: 'application/json-rpc',
			complete: function (xhr, status) {
				console.log(xhr);
				console.log(status);
			}
		});
	}
  1. 享受!