php xml_encode,PHP: xmlrpc_encode_request - Manual

这个博客介绍了如何使用PHP创建一个简单的XML-RPC客户端,该客户端实现了方法重载功能。通过类`RpcClient`,文章展示了如何构造HTTP POST请求,进行身份验证,并将PHP方法映射到XML-RPC方法。当调用未注册的方法时,它会回退到对象的内置方法。这为开发者提供了一种与远程服务交互的方式。
摘要由CSDN通过智能技术生成

Simple OO client with function Overload :

the php metho test_helloworld is translated to xmlrpc method test.helloworld.

class RpcClient {

private $_methods;

private $_context;

private $_url;

function __construct ($url, $user, $passwd) {

$auth = base64_encode(sprintf('%s:%s', $user,$passwd));

$this->_context = stream_context_create(array(

'http' => array(

'method' => 'POST',

'header' => "Content-Type: text/xml\r\n".

"Authorization: Basic $auth" ,

)

));

$this->_url = $url;

$this->registerMethod ("Test_HelloWorld");

}

function __call($methodName, $params) {

if (array_key_exists($methodName,$this->_methods)) {

// on appelle la fonction RPC

$m = str_replace('_', '.', $methodName);

$r = xmlrpc_encode_request($m, $params,array('verbosity'=>'newlines_only'));

$c = $this->_context;

stream_context_set_option($c,'http','content',$r);

$f = file_get_contents($this->_url,false,$c);

$resp = xmlrpc_decode($f);

return $resp;

} else {

// on appelle la fonction de l'objet

call_user_method_array($methodName, $this,$params);

}

}

private function registerMethod ($method) {

$this->_methods[$method] = true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值