我在PHP XML-RPC / JSON-RPC客户机和服务器中查找XML-RPC和JSON-RPC的示例或教程
有人能告诉我吗?
谢谢?对不起,我的英语不太好。
发布于 2013-09-19 19:57:45
对于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
发布于 2014-04-16 00:24:29
我认为实现json-rpc服务的最好方法是使用Zend组件Zend_Json_Server。
因此,我建议您在php中使用Zend_Json组件来实现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();
}关于客户端,您可以在post请求中发送如下json消息:
{
"jsonrpc": "2.0",
"method": "helloworld",
"params": {},
"id": 1
}在这篇文章Send json post using php中,你可以看到一些通过curl或Http Zend module的json请求的例子。
https://stackoverflow.com/questions/18687161
复制相似问题