从 RPC 对象 php5.5 获取 JSON 数据

Posted

技术标签:

【中文标题】从 RPC 对象 php5.5 获取 JSON 数据【英文标题】:get JSON data from RPC object php5.5 【发布时间】:2015-07-09 08:06:36 【问题描述】:

我有一个 RPC 服务器类,它使用 cURL 接受来自未指定的给定第 3 方的 JSON 数据。

我可以看到数据命中了我的班级,我可以将 var_dump 存储到似乎是服务器请求的 a 中,但输出看起来像是接收到包含对入站对象的引用的网络服务器信息。 但我没有看到我的 JSON 数据 'foo:bar'

<?php
class jsonRPCServer 
        public static function handle($object) 
                if (
                        $_SERVER['REQUEST_METHOD'] != 'POST' ||
                        empty($_SERVER['CONTENT_TYPE']) ||
                        $_SERVER['CONTENT_TYPE'] != 'application/json'
                        ) 
                        return false;
                
                $request = json_decode(file_get_contents('php://input'),true);
                $args=func_get_args();
                ob_start();
                var_dump($_SERVER);
                $result2 = ob_get_clean();
                $file = 'stripedump.txt';
                $current = file_get_contents($file);
                $current .= $result2;
                file_put_contents($file, $current);
                try 
                        if ($result = @call_user_func_array(array($object,$request['method']),$request['params'])) 
                                $response = array (
                                                                        'id' => $request['id'],
                                                                        'result' => $result,
                                                                        'error' => NULL
                                                                        );
                         else 
                                $response = array (
                                                                        'id' => $request['id'],
                                                                        'result' => NULL,
                                                                        'error' => 'unknown method or incorrect parameters'
                                                                        );
                        
                 catch (Exception $e) 
                        $response = array (
                                                                'id' => $request['id'],
                                                                'result' => NULL,
                                                                'error' => $e->getMessage()
                                                                );
                
                if (!empty($request['id']))  // notifications don't want response
                        header('content-type: text/javascript');
                        echo json_encode($response);
                
                return true;
        

?>


<?php
require_once 'example.php';
require_once 'jsonRPCServer.php';
$myExample = new example();
jsonRPCServer::handle($myExample)
    or print 'no request';
echo '<b>Attempt to perform basic operations</b><br />'."\n";
try 
    echo 'Your name is <i>'.$myExample->giveMeSomeData('name').'</i><br />'."\n";
    $myExample->changeYourState('I am using this funnction from the local environement');
    echo 'Your status request has been accepted<br />'."\n";
 catch (Exception $e) 
    echo nl2br($e->getMessage()).'<br />'."\n";

var_dump($myExample);
echo '<br /><b>Attempt to store strategic data</b><br />'."\n";
try 
    $myExample->writeSomething('bite me');
    echo 'Strategic data succefully stored';
 catch (Exception $e) 
    echo nl2br($e->getMessage());

?>

来自远程 cURL 客户端的输出:

gentoo-mini htdocs # curl -X POST -H "Content-Type: application/json" -d "foo:bar" http://nyctelecomm.com/hooker/
<b>Attempt to perform basic operations</b><br />
Your name is <i>Bubba</i><br />
Your status request has been accepted<br />
object(example)#1 (1) 
  ["someData":"example":private]=>
  array(2) 
    ["name"]=>
    string(5) "Bubba"
    ["attr"]=>
    string(17) "Some me Attribute"
  

<br /><b>Attempt to store strategic data</b><br />
Strategic data succefully stored

存储的 var_dump($_SERVER) 数据:

array(29) 
  ["HTTP_HOST"]=>
  string(15) "nyccomm.com"
  ["HTTP_USER_AGENT"]=>
  string(11) "curl/7.42.1"
  ["HTTP_ACCEPT"]=>
  string(3) "*/*"
  ["CONTENT_TYPE"]=>
  string(16) "application/json"
  ["CONTENT_LENGTH"]=>
  string(1) "9"
  ["PATH"]=>
  string(29) "/sbin:/bin:/usr/sbin:/usr/bin"
  ["LD_LIBRARY_PATH"]=>
  string(29) "/usr/local/lib:/usr/local/lib"
  ["SERVER_SIGNATURE"]=>
  string(0) ""
  ["SERVER_SOFTWARE"]=>
  string(34) "Apache/2.4.12 (FreeBSD) PHP/5.6.10"
  ["SERVER_NAME"]=>
  string(15) "nyccomm.com"
  ["SERVER_ADDR"]=>
  string(13) "108.61.175.20"
  ["SERVER_PORT"]=>
  string(2) "80"
  ["REMOTE_ADDR"]=>
  string(12) "67.82.49.236"
  ["DOCUMENT_ROOT"]=>
  string(21) "/home/www"
  ["REQUEST_SCHEME"]=>
  string(4) "http"
  ["CONTEXT_PREFIX"]=>
  string(0) ""
  ["CONTEXT_DOCUMENT_ROOT"]=>
  string(21) "/home/www"
  ["SERVER_ADMIN"]=>
  string(19) "admin@ex-mailer.com"
  ["SCRIPT_FILENAME"]=>
  string(38) "/home/www/hooker/index.php"
  ["REMOTE_PORT"]=>
  string(5) "52841"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["REQUEST_METHOD"]=>
  string(4) "POST"
  ["QUERY_STRING"]=>
  string(0) ""
  ["REQUEST_URI"]=>
  string(8) "/hooker/"
  ["SCRIPT_NAME"]=>
  string(17) "/hooker/index.php"
  ["PHP_SELF"]=>
  string(17) "/hooker/index.php"
  ["REQUEST_TIME_FLOAT"]=>
  float(1436429001.683)
  ["REQUEST_TIME"]=>
  int(1436429001)

string(4) "name"

tcpdump:

00:29:06.659025 IP 192.168.0.55.52841 > 108.61.175.20.vultr.com.http: Flags [P.], seq 1:148, ack 1, win 115, options [nop,nop,TS val 2017270703 ecr 2483478707], length 147
E....A@.@.f....7l=...i.P...g.I.]...s.......
x=......POST /hooker/ HTTP/1.1
Host: nyccomm.com
User-Agent: curl/7.42.1
Accept: */*
Content-Type: application/json
Content-Length: 9

foo:bar
00:29:06.746198 IP 108.61.175.20.vultr.com.http > 192.168.0.55.52841: Flags [P.], seq 1:561, ack 148, win 1033, options [nop,nop,TS val 2483478793 ecr 2017270703], length 560
E..dm.@.5...l=.....7.P.i.I.].......     :......
...     x=..HTTP/1.1 200 OK
Date: Thu, 09 Jul 2015 08:03:21 GMT
Server: Apache/2.4.12 (FreeBSD) PHP/5.6.10
X-Powered-By: PHP/5.6.10
Content-Length: 373
Content-Type: text/html; charset=UTF-8

<b>Attempt to perform basic operations</b><br />
Your name is <i>Bubba</i><br />
Your status request has been accepted<br />
object(example)#1 (1) 
  ["someData":"example":private]=>
  array(2) 
    ["name"]=>
    string(5) "Bubba"
    ["attr"]=>
    string(17) "Some me Attribute"
  

<br /><b>Attempt to store strategic data</b><br />
Strategic data succefully stored
00:29:06.746271 IP 192.168.0.55.52841 > 108.61.175.20.vultr.com.http: Flags [.], ack 561, win 123, options [nop,nop,TS val 2017270790 ecr 2483478793], length 0
E..4.B@.@.gP...7l=...i.P.....I......W.....

如何从入站 RPC 对象访问 JSON 数据? (特别是 'foo:bar')

【问题讨论】:

【参考方案1】:

单独使用时有效

<?php
        $request = file_get_contents('php://input');
        $args=func_get_args();
        ob_start();
        var_dump($request);
        $result2 = ob_get_clean();
        $file = 'stripedump.txt';
        $current = file_get_contents($file);
        $current .= $result2;
        file_put_contents($file, $current);
?>

【讨论】:

以上是关于从 RPC 对象 php5.5 获取 JSON 数据的主要内容,如果未能解决你的问题,请参考以下文章

在 JSON-RPC 连接上读取多个 JSON 对象

哪个是码头最好的 json rpc 库?

具有特定命名的 JSON RPC 序列化对象

错误 32600:JSON-RPC 请求必须是对象

在 JSON-RPC 方法中获取 websocket 连接信息

Firebase Documentsnapshot 映射到 Flutter 中的 Json 对象