2013-09-08 6 views
0

Я отрываясь exmples или учебник по XML-RPC и JSON-RPC в PHP XML-RPC/клиент JSON-RPC и серверRPC/XML-RPC/JSON-RPC в PHP

Может кто-нибудь сказать мне, что?

Спасибо? Извините, мой английский не очень хорош.

ответ

0

Для JSON-RPC вы можете использовать этот: jsonrpcphp

Смотрите пример: [Сервер]

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

[клиент]

<?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

Я думаю, что лучший способ реализации службы json-rpc - usin g Компонент Zend Zend_Json_Server.

Поэтому я предлагаю вам использовать компонент Zend_Json для реализации службы json-rpc в php. Zend framework позволяет использовать его компонент «из коробки». Таким образом, вы могли бы сделать структуру как следующее:

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

О клиенте вы можете отправить сообщение JSon, как это в запросе сообщение:

{ 
    "jsonrpc": "2.0", 
    "method": "helloworld", 
    "params": {}, 
    "id": 1 
} 

В этом сообщении Send json post using php вы можете увидеть несколько примеров запроса json через curl или через модуль Http Zend.

0

Я использую VDATA для протокола RPC: http://vdata.dekuan.org/

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

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 
    );