3

我正在使用 xmlrpc-epi 为基于 PHP/MySQL 的网站使用 web 服务,我得到 xmlrpc 客户端响应,如下所示:

opening socket to host: comingsoon.com, port: 80, uri: /CM_server.php
sending http request:
POST /CM_server.php HTTP/1.0
User-Agent: xmlrpc-epi-php/0.2 (PHP) 
Host: fn24dev.com:80 Content-Type: text/xml 
Content-Length: 117 
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall> 
<methodName>listVideos</methodName> 
<params/> 
</methodCall>
receiving response...
got response:
<?xml version="1.0" encoding="iso-8859-1"?> 
<methodResponse> 
<params>
 <param>
  <value>
   <array>
    <data>
     <value>
      <struct>
       <member>
        <name>0</name>
         <value>
          <string>63</string>
         </value>
       </member>
       <member>
        <name>indexer</name>
         <value>
          <string>63</string>
         </value>
       </member>
       <member>
        <name>1</name>
        <value>
         <string>02_CHIMPPANTS_24-03-2012_04-43-06</string>
        </value>
       </member>
       <member>
        <name>video_id</name>
        <value>
         <string>02_CHIMPPANTS_24-03-2012_04-43-06</string>
        </value>
       </member>
       <member>
        <name>2</name>
        <value>
        <string>mp4</string>
        </value> </member>
      <member>
        <name>type</name>
        <value>
         <string>mp4</string>
        </value>
      </member>
     </struct>
   </value> 
  </data> 
  </array>
</value>
</param>
</params>
</methodResponse>

在此,我将 mysql 表字段名和字段值都作为 xml 文本内容。但我想得到像 mysql 字段名作为元素和字段值作为文本内容的响应。如下所示,

<member>
       <indexer>63</indexer>
</member>

<member>
      <video_id>02_CHIMPPANTS_24-03-2012_04-43-06</video_id>
</member>
<member>
  <type>mp4</type>
</member>

我添加了 PHP 代码和 mysql 表和数据以进行说明。

Table_name: videos
    indexer     video_id                           type
    --------       ----------------------------------  -----
    63          02_CHIMPPANTS_24-03-2012_04-43-06      mp4
    54          02_Friday_23-03-2012_05-11-18          mp4

CM_server.php

<?php
function Connect(){
$connect = mysql_connect("localhost","root","");
$db = mysql_select_db("database") or die("Database is not valid");
}

//Get the Videos
function getVideos(){
//connect database
Connect();
$videoquery = mysql_query("select * from videos");
    if(mysql_num_rows($videoquery) == "0"){
        $returnquery = "0";
    } else {
        while($getquery = mysql_fetch_array($videoquery)){
            $returnquery[] = $getquery;
        }
    }
    return $returnquery;
}
//server creating and check a list a method
$request = $HTTP_RAW_POST_DATA;
if (!$request) $request_xml = $HTTP_POST_VARS['xml'];
$server = xmlrpc_server_create( );
if (!$server) die("Couldn't create server");
xmlrpc_server_register_method($server, 'listVideos', 'getVideos');
$options = array('output_type' => 'xml', 'version' => 'auto');
echo xmlrpc_server_call_method($server, $request, null, $options);
xmlrpc_server_destroy($server);
?>

客户端.php

<?php
include("utils.php");
$options = array('output_type' => 'xml', 'version' => 'xmlrpc');
$result1 = xu_rpc_http_concise(array(method => 'listVideos',host  => 'comingsoon.com',uri=> '/CM_server.php',options => $options,debug => 1));
?>

实用程序.php

<?php
xu_load_extension();
// a function to ensure the xmlrpc extension is loaded.
// xmlrpc_epi_dir = directory where libxmlrpc.so.0 is located
// xmlrpc_php_dir = directory where xmlrpc-epi-php.so is located
function xu_load_extension($xmlrpc_php_dir="") {
   if(!extension_loaded('xmlrpc')) {
      $bSuccess = true;
      putenv("LD_LIBRARY_PATH=/usr/lib/php4/apache/xmlrpc/");
      if ($xmlrpc_php_dir) {
         $xmlrpc_php_dir .= '/';
      }
      if (!extension_loaded("xmlrpc")) {
         $bSuccess = dl($xmlrpc_php_dir . "xmlrpc-epi-php.so");
      }
   }
   return $bSuccess;
}

/* generic function to call an http server with post method */
function xu_query_http_post($request, $host, $uri, $port, $debug, 
                            $timeout, $user, $pass, $secure=false) {
   $response_buf = "";
   if ($host && $uri && $port) {
      $content_len = strlen($request);

      $fsockopen = $secure ? "fsockopen_ssl" : "fsockopen";

      dbg1("opening socket to host: $host, port: $port, uri: $uri", $debug);
      $query_fd = $fsockopen($host, $port, $errno, $errstr, 10);

      if ($query_fd) {

         $auth = "";
         if ($user) {
            $auth = "Authorization: Basic " .
                    base64_encode($user . ":" . $pass) . "\r\n";
         }

         $http_request = 
         "POST $uri HTTP/1.0\r\n" .
         "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
         "Host: $host:$port\r\n" .
         $auth .
         "Content-Type: text/xml\r\n" .
         "Content-Length: $content_len\r\n" . 
         "\r\n" .
         $request;

         dbg1("sending http request:</h3> <xmp>\n$http_request\n</xmp>", $debug);

         fputs($query_fd, $http_request, strlen($http_request));

         dbg1("receiving response...", $debug);

         $header_parsed = false;

         $line = fgets($query_fd, 4096);
         while ($line) {
            if (!$header_parsed) {
               if ($line === "\r\n" || $line === "\n") {
                  $header_parsed = 1;
               }
               dbg2("got header - $line", $debug);
            }
            else {
               $response_buf .= $line;
            }
            $line = fgets($query_fd, 4096);
         }

         fclose($query_fd);
      }
      else {
         dbg1("socket open failed", $debug);
      }
   }
   else {
      dbg1("missing param(s)", $debug);
   }

   dbg1("got response:</h3>. <xmp>\n$response_buf\n</xmp>\n", $debug);

   return $response_buf;
}

function xu_fault_code($code, $string) {
   return array('faultCode' => $code,
                'faultString' => $string);
}


function find_and_decode_xml($buf, $debug) {
   if (strlen($buf)) {
      $xml_begin = substr($buf, strpos($buf, "<?xml"));
      if (strlen($xml_begin)) {

         $retval = xmlrpc_decode($xml_begin);
      }
      else {
         dbg1("xml start token not found", $debug);
      }
   }
   else {
      dbg1("no data", $debug);
   }
   return $retval;
}

function xu_rpc_http_concise($params) {
   $host = $uri = $port = $method = $args = $debug = null;
   $timeout = $user = $pass = $secure = $debug = null;

    extract($params);

    // default values
    if(!$port) {
        $port = 80;
    }
    if(!$uri) {
        $uri = '/';
    }
    if(!$output) {
        $output = array(version => 'xmlrpc');
    }

   $response_buf = "";
   if ($host && $uri && $port) {
      $request_xml = xmlrpc_encode_request($method, $args, $output);
      $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug,
                                         $timeout, $user, $pass, $secure);

      $retval = find_and_decode_xml($response_buf, $debug);
   }
   return $retval;
}

/* call an xmlrpc method on a remote http server. legacy support. */
function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false, 
                     $timeout=0, $user=false, $pass=false, $secure=false) {
    return xu_rpc_http_concise(
        array(
            method  => $method,
            args    => $args,
            host    => $host,
            uri     => $uri,
            port    => $port,
            debug   => $debug,
            timeout => $timeout,
            user    => $user,
            pass    => $pass,
            secure  => $secure
        ));
}



function xu_is_fault($arg) {
   // xmlrpc extension finally supports this.
   return is_array($arg) ? xmlrpc_is_fault($arg) : false;
}

/* sets some http headers and prints xml */
function xu_server_send_http_response($xml) {
    header("Content-type: text/xml");
    header("Content-length: " . strlen($xml) );
    echo $xml;
}


function dbg($msg) {
   echo "<h3>$msg</h3>"; flush();
}
function dbg1($msg, $debug_level) {
   if ($debug_level >= 1) {
      dbg($msg);
   }
}
function dbg2($msg, $debug_level) {
   if ($debug_level >= 2) {
      dbg($msg);
   }
}


?>

如果有人知道 xmlrpc 来获得这个解决方案,请指导我。

4

1 回答 1

1

您要求的结构不是 XML-RPC,因此您将无法使用 PHP 的 XML-RPC 扩展来实现它。XML-RPC 规范定义了一个精确的响应结构,包括可用的类型。

您心目中的那种 XML 服务只需要一个 XML 解析库(例如 SimpleXML)、一些 XML 创建函数(可能是将 PHP 数组转换为 XML 格式字符串的递归函数)和少量“调度代码” " 在一个 URL 上支持不同的方法:

$request_xml = file_get_contents('php://input');
$request_sx = simplexml_load_string($request_xml);
switch ( (string)$request_sx->Method )
{
    case 'listVideos':
        $data = getVideos( (string)$request_sx->Params->SearchTerm );
        $response_xml = generate_xml($data);
    break;
}

header('Content-Type: text/xml');
echo $response_xml;
exit;
于 2012-09-23T15:46:23.450 回答