首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP中的RPC / XML-RPC / JSON-RPC

PHP中的RPC / XML-RPC / JSON-RPC
EN

Stack Overflow用户
提问于 2013-09-09 02:26:37
回答 2查看 2.1K关注 0票数 0

我在PHP XML-RPC / JSON-RPC客户机和服务器中查找XML-RPC和JSON-RPC的示例或教程

有人能告诉我吗?

谢谢?对不起,我的英语不太好。

EN

回答 2

Stack Overflow用户

发布于 2013-09-19 19:57:45

对于JSON-RPC,您可以使用以下命令: jsonrpcphp

请参见示例:服务器

代码语言:javascript
复制
<?php
require_once 'example.php';
$myExample = new example();

// performs some basic operation
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 function from the local environement');
    echo 'Your status request has been accepted<br />'."\n";
} catch (Exception $e) {
    echo nl2br($e->getMessage()).'<br />'."\n";
}

// performs some strategic operation, locally allowed
echo '<br /><b>Attempt to store strategic data</b><br />'."\n";
try {
    $myExample->writeSomething('Strategic string!');
    echo 'Strategic data succefully stored';
} catch (Exception $e) {
    echo nl2br($e->getMessage());
}
?>

客户端

代码语言:javascript
复制
<?php
require_once 'jsonRPCClient.php';
$myExample = new jsonRPCClient('http://jsonrpcphp.org/server.php');

// performs some basic operation
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 function from the network');
    echo 'Your status request has been accepted<br />'."\n";
} catch (Exception $e) {
    echo nl2br($e->getMessage()).'<br />'."\n";
}

// performs some strategic operation, locally allowed
echo '<br /><b>Attempt to store strategic data</b><br />'."\n";
try {
    $myExample->writeSomething('Strategic string!');
    echo 'Strategic data succefully stored';
} catch (Exception $e) {
    echo nl2br($e->getMessage());
}
?>

来源:http://jsonrpcphp.org/?page=example&lang=en

票数 0
EN

Stack Overflow用户

发布于 2014-04-16 00:24:29

我认为实现json-rpc服务的最好方法是使用Zend组件Zend_Json_Server。

因此,我建议您在php中使用Zend_Json组件来实现json-rpc服务。Zend框架允许“开箱即用”其组件。所以你可以像下面这样做一个结构:

代码语言:javascript
复制
Project
   |
   ------libs/Zend
                |
                -----Json/
                       |
                       -----Server/
                |
                -----Loader.php

并实现类似这样的东西:

代码语言:javascript
复制
<?php

  // path to dir with Zend root
  set_include_path(__DIR__ . "/libs");
  // path to Zend loader
  require_once __DIR__ . "/libs/Zend/Loader.php";

  Zend_Loader::loadClass('Zend_Json_Server');

  $server = new Zend_Json_Server();
  $server->setClass('Service');

  /**
   * Service Implementation
   */
  class Service
  {
      public function __construct()
      {
        // init some service attributes ...
      }

      /**
       * example of api method exposed by service
       * return "hello world" message
       * @param $domain
       * @return object (json)
       */
       public function helloworld()
       {
            $aOut = array('msg' => 'hello world');
            return json_encode($aOut);
       }

       // ... other methods of the service

 }

try {
    $output = $server->handle();
    echo $output;
} catch (Exception $e) {
    echo  ($e->getMessage());
    //header('HTTP/1.1 400 BAD REQUEST');
    exit();
}

关于客户端,您可以在post请求中发送如下json消息:

代码语言:javascript
复制
{ 
    "jsonrpc": "2.0", 
    "method": "helloworld", 
    "params": {}, 
    "id": 1 
}

在这篇文章Send json post using php中,你可以看到一些通过curl或Http Zend module的json请求的例子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18687161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档