码迷,mamicode.com
首页 > Web开发 > 详细

jsonRPC

时间:2014-09-01 12:11:03      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   strong   ar   div   

__call

php内置函数:

当请求一个对象的方法不存在是会调用此方法:

/*
*@param string $method 请求方法名称。
*@param array $params 请求方法的参数。
*/
function __call ($method,$params);

 

stream_context_create

$opts = array (‘http‘ => array (
    ‘method‘  => ‘POST‘,
    ‘header‘  => ‘Content-type: application/json‘,
    ‘content‘ => $request
));
$context  = stream_context_create($opts);
if ($fp = fopen($url, ‘r‘, false, $context))
{
    while($row=fgets($fp)){
        echo $row;
    }
}

这个函数主要是对 fopen以及其他系统函数进行设置。 

如上图 规定了提交的请求类型为json格式,方式为 POST,内容为$request。(json对象);

 

 

$request = json_decode(file_get_contents(‘php://input‘),true);

这个就是拿到上面提交的 $request。 

file_get_contents(‘php://input‘)其实得到就是 $_POST,只是格式不一样罢了;

 

 

其实 jsonRPC 只是利用了 file_get_contents(‘php://input‘) 来获取,如同 ajax请求一样。 请求RPC服务器上的方法,获取该方法得到的值或对象。

 

jsonRPC

标签:style   blog   http   color   os   io   strong   ar   div   

原文地址:http://www.cnblogs.com/canbefree/p/3948769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!