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

PHP中的RPC/XML-RPC/JSON-RPC,php,client-server,xml-rpc,json-rpc,simplexmlrpcserver,Php,Client Server,Xml Rpc,Json Rpc,Simplexmlrpcserver,我正在查找PHP中XML-RPC和JSON-RPC的示例或教程 XML-RPC/JSON-RPC客户端和服务器 有人能告诉我吗 谢谢你? 对不起,我的英语不好。对于JSON-RPC,您可以使用这个:jsonrpcphp 见示例: [服务器] [客户] <?php require_once 'jsonRPCClient.php'; $myExample = new jsonRPCClient('http://jsonrpcphp.org/server.php'); // perform

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

有人能告诉我吗

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

对于JSON-RPC,您可以使用这个:jsonrpcphp

见示例: [服务器]


[客户]

<?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());
}
?>


来源:

我认为实现json rpc服务的最佳方法是使用Zend组件Zend_json_服务器

因此,我建议您使用Zend_Json组件在php中实现Json rpc服务。 Zend框架允许使用其组件“开箱即用”。 因此,您可以执行如下结构:

Project
   |
   ------libs/Zend
                |
                -----Json/
                       |
                       -----Server/
                |
                -----Loader.php
并实现如下内容:

<?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();
}

在本文中,您可以看到一些通过curl或Http Zend模块进行json请求的示例。

我使用vdata作为RPC协议:

1、PHP和JavaScript都可以

2、跨源资源共享(CORS)呼叫仍然可以

PHP:在客户端调用服务

use dekuan\vdata\CConst; use dekuan\vdata\CRequest; $cRequest = CRequest::GetInstance(); $arrResp = []; $nCall = $cRequest->Post ( [ 'url' => 'http://api-account.dekuan.org/login', 'data' => [ 'u_name' => 'username', 'u_pwd' => 'password', 'u_keep' => 1 ], 'version' => '1.0', // required version of service 'timeout' => 30, // timeout in seconds 'cookie' => [], // array or string are both okay. ], $arrResp ); if ( CConst::ERROR_SUCCESS == $nCall && $cRequest->IsValidVData( $arrResp ) ) { // arrResp // 'errorid' : error id // 'errordesc' : error desc // 'vdata' : virtual data // 'version' : in-service version of service // 'json' : original json array print_r( $arrResp ); } 使用dekuan\vdata\CConst; 使用dekuan\vdata\CRequest; $cRequest=cRequest::GetInstance(); $arrrep=[]; $nCall=$cRequest->Post ( [ 'url'=>'http://api-account.dekuan.org/login', “数据”=> [ 'u_name'=>'username', “u_pwd”=>“密码”, “u_keep”=>1 ], 'version'=>'1.0',//所需的服务版本 'timeout'=>30,//以秒为单位的超时 “cookie”=>[],//数组或字符串都可以。 ], $arrrep ); 如果(CConst::ERROR_SUCCESS==$nCall&& $cRequest->isvaliddata($arresp)) { //阿瑞普 //“errorid”:错误id //“errordesc”:错误描述 //“vdata”:虚拟数据 //“版本”:服务的在用版本 //“json”:原始json数组 印刷费($arresp); } PHP:在服务器上响应客户端

use dekuan\vdata\CResponse; $cResponse = CResponse::GetInstance(); $cResponse->SetServiceName( 'vdata protocol service' ); $cResponse->SetServiceUrl( 'http://vdata.dekuan.org/vdata' ); $cResponse->Send ( 0, // error id "error desc", // error description [ "info" => "..." ], // customized info '1.0' // in-service version of service ); 使用德宽\vdata\response; $cResponse=cResponse::GetInstance(); $creponse->SetServiceName('vdata协议服务'); $response->SetServiceUrl('http://vdata.dekuan.org/vdata' ); $creponse->Send ( 0,//错误id “error desc”,//错误描述 [“信息”=>“…”],//自定义信息 “1.0”//服务的在用版本 ); use dekuan\vdata\CConst; use dekuan\vdata\CRequest; $cRequest = CRequest::GetInstance(); $arrResp = []; $nCall = $cRequest->Post ( [ 'url' => 'http://api-account.dekuan.org/login', 'data' => [ 'u_name' => 'username', 'u_pwd' => 'password', 'u_keep' => 1 ], 'version' => '1.0', // required version of service 'timeout' => 30, // timeout in seconds 'cookie' => [], // array or string are both okay. ], $arrResp ); if ( CConst::ERROR_SUCCESS == $nCall && $cRequest->IsValidVData( $arrResp ) ) { // arrResp // 'errorid' : error id // 'errordesc' : error desc // 'vdata' : virtual data // 'version' : in-service version of service // 'json' : original json array print_r( $arrResp ); } use dekuan\vdata\CResponse; $cResponse = CResponse::GetInstance(); $cResponse->SetServiceName( 'vdata protocol service' ); $cResponse->SetServiceUrl( 'http://vdata.dekuan.org/vdata' ); $cResponse->Send ( 0, // error id "error desc", // error description [ "info" => "..." ], // customized info '1.0' // in-service version of service );