• PHP中使用XMLRPC


     

      PHP中简单使用XMLRPC,服务器端和客户端都为PHP代码实现。

     

      这里使用的XML-RPC完整包括client和server的XML-RPC实现。
      客户端和服务器端分别由 xmlrpc_client 类和 xmlrpc_server 类实现,主要用于接收和发送XML-RPC报文。

    客户端:

      xmlrpcval 类用于将PHP变量编码为XML-RPC等价数据类型和向远程方法传递参数。相反的过程使用 xmlrpc_decode() 函数。

      xmlrpcmsg 类通过传递给它一个参数表来创建XML-RPC报文。
      xmlrpc_client 类发送使用 xmlrpcmsg 类创建的XML-RPC报文。

    服务器端:

      xmlrpc_server 类解析这些收到的报文(xmlrpcmsg 对象)为PHP变量。

      然后被作为一个单独参数传递给用户函数,该函数必须返回一个 xmlrpcresp 对象,。

      xmlrpc_server 类将其用于序列化并返回给客户端。

    服务器端:xmlrpc_s.php

    <?php
    /*XMLRPC服务器(PHP版)*/
    
    require "libphpxmlrpc/xmlrpc.inc";
    require "libphpxmlrpc/xmlrpcs.inc";
    
    function saySomething($msg) 
    {
    //从msg对象中解码出参数放入变量中
     $words = php_xmlrpc_decode($msg->getParam(0));
    
     //返回结果
     if(strlen($words) > 0) 
     {   
      return new xmlrpcresp( new xmlrpcval('Server say: '.$words, 'string') );//返回给客户的
     }
     else
     {    
       return new xmlrpcresp(0,$xmlrpcerruser + 100, "paras '".$words."' understand.");
     //出现错误返回给客户的,当然也可以不返回
     //如果有什么参数不对啊 系统会返回错误的
     //$xmlrpcerruser + 100 这个是返回客户自定义错误时候的错误代码
     //开发RPC程序的时候最好自己定义一个错误表客户端显示的错误编号会是100
     }
    }
    
    //建立服务器
    $s = new xmlrpc_server(array('say' => array('function' => 'saySomething', //命令对应要调用的函数     
     'signature' => array(array('string','string')), //返回输入的数据类型,一个函数可以有几种输入和输出类型     
     'docstring' => 'This service echoes Hello+input stirng.')), //对该调用的说明    
     0 //此参数决定此时不立即开启服务   
     );
    
    //设置参数
    $s->response_charset_encoding = "UTF-8"; 
    
    //开启服务
     $s->service();
    ?>

    客户端:xmlrpc_c.php

    <?php
    //引入xmlrpc开发库
    require('libphpxmlrpc/xmlrpc.inc');
    
    //建立连接器
    $client = new xmlrpc_client('/PHPWorkspace/xmlrpc_s.php', 'localhost', 80);
    
    $client->request_charset_encoding = 'UTF-8';
    
    //可以设置调试来查看详细HTTP请求信息 
    if($_GET['debug']) $client->setDebug(true); 
    
    //创建xmlrpcval对象,将PHP变量编码为XML-RPC需要的XML形式
    $input = new xmlrpcval('Hello!', 'string');
    
    //参数都要放入到数组中
    $paras = array($input);
    
    //创建XML-RPC报文
    $msg = new xmlrpcmsg('say', $paras);
    
    
    //用刚才建立的连接器发送请求
    $rsp = $client->send($msg);
    
    //处理服务器的返回值
    if(0 == $rsp->faultcode()) 
    {
        //解码
        $response = php_xmlrpc_decode($rsp->value());
    
        //显示结果
        #var_dump($response);
        echo $response;
    } 
    else
    {
        //发生错误
        print 'Error: '.$rsp->faultcode().', '.$rsp->faultstring().'';
    }
    ?>
    其中需要引入xmlrpc开发库,不清楚的话可以参考本人前一篇博客,当中有提到。

    在浏览器中打开客户端文件,效果如下图:



  • 相关阅读:
    Natas29 Writeup(Perl命令注入、00截断、绕过过滤)
    Natas27 Writeup(mysql溢出截断漏洞)
    Natas26 Writeup(PHP反序列化漏洞)
    Natas25 Writeup(目录遍历、头部注入)
    Natas24 Writeup(strcmp绕过漏洞)
    yum提示Another app is currently holding the yum lock; waiting for it to exit
    linux网站
    fastdfs_5.05下载
    sqlog连接虚拟机mysql服务
    java知识博客网站(一些配置和学习的记录)
  • 原文地址:https://www.cnblogs.com/lxt287994374/p/3907834.html
Copyright © 2020-2023  润新知