2012-04-28 56 views
3

我使用XMLRPC-EPI爲PHP/MySQL爲基礎的網站,web服務的工作,我得到XML-RPC客戶端respaonse象下面這樣:如何從一個MySQL數據庫獲得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); 
?> 

client.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)); 
?> 

utils.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來獲得這個解決方案,請指導我。

+0

您的代碼可能會有所幫助。 – eggyal 2012-04-28 08:32:55

+0

我已經添加了更多說明的源代碼。 – 2012-04-28 08:51:17

回答

1

您所要求的結構不會是XML-RPC,因此您將無法使用PHP的XML-RPC擴展來實現它。 XML-RPC Specification定義了響應的精確結構,包括可用的類型。您想要的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; 
相關問題