【问题标题】:Need sample XML-RPC client code for PHP5 [closed]需要 PHP5 的示例 XML-RPC 客户端代码 [关闭]
【发布时间】:2010-10-17 15:08:51
【问题描述】:

需要有关如何将 PHP 内置的 XML-RPC 库(版本 PHP 版本 5.2.6)用于 XML-RPC 客户端的教程或说明。服务器在 Python 中并且可以工作。

Google 和 php.net 让我失望了。

更新:

根据 phpinfo,我安装了 xmlrpc-epi v. 0.51。我访问了http://xmlrpc-epi.sourceforge.net/,但左侧的 xmlrpc-epi-php 示例部分显示了 sf.net 的 404 版本。

更新2:

我将使用http://phpxmlrpc.sourceforge.net/,希望这对我有用。

更新3:

http://phpxmlrpc.sourceforge.net/ 的代码很简单,我开始工作了。

没有结束问题。如果有人想加入超简单的解决方案,那就太好了!

【问题讨论】:

    标签: php xml-rpc


    【解决方案1】:

    用php编写的客户端和服务器:

    https://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-php.html

    【讨论】:

      【解决方案2】:

      一个很简单的xmlrpc客户端,我用的是cURL类,你可以从:https://github.com/dcai/curl/blob/master/src/dcai/curl.php得到它

      class xmlrpc_client {
          private $url;
          function __construct($url, $autoload=true) {
              $this->url = $url;
              $this->connection = new curl;
              $this->methods = array();
              if ($autoload) {
                  $resp = $this->call('system.listMethods', null);
                  $this->methods = $resp;
              }
          }
          public function call($method, $params = null) {
              $post = xmlrpc_encode_request($method, $params);
              return xmlrpc_decode($this->connection->post($this->url, $post));
          }
      }
      header('Content-Type: text/plain');
      $rpc = "http://10.0.0.10/api.php";
      $client = new xmlrpc_client($rpc, true);
      $resp = $client->call('methodname', array());
      print_r($resp);
      

      【讨论】:

      • 谢谢。我已经使用问题中 update2 中的那个实现了,但我认为您的解决方案会很好。
      • 该代码无效(语法错误)。是否缺少构造函数声明?
      • @rewbs 是的,代码中缺少__construct函数,刚刚添加,谢谢指出。
      【解决方案3】:

      我在http://code.runnable.com/UnEjkT04_CBwAAB4/how-to-create-a-xmlrpc-server-and-a-xmlrpc-client-for-php找到了这个解决方案

      webfaction api登录示例

      // login is the method in the xml-rpc server and username and password
      // are the params
      $request = xmlrpc_encode_request("login", array('username', 'password'));
      
      $context = stream_context_create(array('http' => array(
      'method' => "POST",
      'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
      'content' => $request
      )));
      
      $server = 'https://api.webfaction.com/'; // api url
      $file = file_get_contents($server, false, $context);
      
      $response = xmlrpc_decode($file);
      
      print_r($response);
      

      你会看到类似的东西:

      Array ( [0] => 5d354f42dcc5651fxe6d1a21b74cd [1] => Array ( [username] => yourusername [home] => /home [mail_server] => Mailbox14 [web_server] => Webxxx [id] => 123456 ) )
      

      【讨论】:

        【解决方案4】:

        从 php 官方链接http://www.php.net/manual/en/ref.xmlrpc.php 使用 steph 的示例(在底部)作为起点。他使用的是同一台服务器并且易于设置。也就是说,如果您不想使用外部库或框架。但如果你这样做了,那么看看http://framework.zend.com/manual/1.12/en/zend.xmlrpc.server.html

        【讨论】:

          【解决方案5】:

          此外,fxmlrpc(与NativeSerializerNativeParser 一起使用时)是ext/xmlrpc 周围的薄包装。

          【讨论】:

            【解决方案6】:

            寻找相同的解决方案。这是一个超级简单的类,理论上可以与任何 XMLRPC 服务器一起工作。我在 20 分钟内完成了它,所以还有很多不足之处,比如自省、更好的错误处理等。

            file: xmlrpcclient.class.php
            
            <?php
            
            /**
             * XMLRPC Client
             *
             * Provides flexible API to interactive with XMLRPC service. This does _not_
             * restrict the developer in which calls it can send to the server. It also
             * provides no introspection (as of yet).
             *
             * Example Usage:
             *
             * include("xmlrpcclient.class.php");
             * $client = new XMLRPCClient("http://my.server.com/XMLRPC");
             * print var_export($client->myRpcMethod(0));
             * $client->close();
             *
             * Prints:
             * >>> array (
             * >>>   'message' => 'RPC method myRpcMethod invoked.',
             * >>>   'success' => true,
             * >>> )
             */
            
            class XMLRPCClient
            {
                public function __construct($uri)
                {
                    $this->uri = $uri;
                    $this->curl_hdl = null;
                }
            
                public function __destruct()
                {
                    $this->close();
                }
            
                public function close()
                {
                    if ($this->curl_hdl !== null)
                    {
                        curl_close($this->curl_hdl);
                    }
                    $this->curl_hdl = null;
                }
            
                public function setUri($uri)
                {
                    $this->uri = $uri;
                    $this->close();
                }
            
                public function __call($method, $params)
                {
                    $xml = xmlrpc_encode_request($method, $params);
            
                    if ($this->curl_hdl === null)
                    {
                        // Create cURL resource
                        $this->curl_hdl = curl_init();
            
                        // Configure options
                        curl_setopt($this->curl_hdl, CURLOPT_URL, $this->uri);
                        curl_setopt($this->curl_hdl, CURLOPT_HEADER, 0); 
                        curl_setopt($this->curl_hdl, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($this->curl_hdl, CURLOPT_POST, true);
                    }
            
                    curl_setopt($this->curl_hdl, CURLOPT_POSTFIELDS, $xml);
            
                    // Invoke RPC command
                    $response = curl_exec($this->curl_hdl);
            
                    $result = xmlrpc_decode_request($response, $method);
            
                    return $result;
                }
            }
            
            ?>
            

            【讨论】:

              【解决方案7】:

              我已经编写了一个简单的面向对象的包装器,它使它变得如此简单:

              require_once('ripcord.php'); $client = ripcord::xmlrpcClient($url); $score = $client->method($argument, $argument2, ..);

              有关详细信息和下载链接,请参阅http://code.google.com/p/ripcord/wiki/RipcordClientManual

              【讨论】:

              • 如何使用包装器运行 RPC 服务器?请完整示例
              【解决方案8】:

              Wordpress 有 XML-RPC.php 文件,看看那个.. 它可能会有所帮助

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2020-06-09
                • 1970-01-01
                • 1970-01-01
                • 2012-10-24
                • 2015-09-10
                • 2016-09-15
                • 2011-11-07
                • 1970-01-01
                相关资源
                最近更新 更多