DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> php5中XML-RPC函數的使用
php5中XML-RPC函數的使用
編輯:XML詳解     
於web架構方面的獲獎作品,感受頗深,xml和php結合應用越來多了,裡面幾乎所有的作品在設計上都用到了XML這個東西.....-_-! 又落伍了不少. 建議朋友們都去看看. 所以自己這幾天在瘋狂的學習PHP和XML結合的相關知識.
這其中就遇到了XML-RPC服務, 在網上可以找到的資料不多, 而且大多數都是用了其他第三方用php開發的XML-RPC class, 而用PHP5自帶的XMLrpc函數(目前是試驗模塊)的好像就不多了, 但是個人還是比較喜歡原生的東西,就自己研究了一下.

在手冊中有人提供了一個例子,但是不支持中文, 並且沒有告訴在客戶端返回結果後怎麼處理這個結果. 自己苦搞半天終於搞明白了,其實可以很輕松讓他支持中文, 並且幾乎不需要做任何額外的工作.

下面是他原來的例子(可以在手冊中找到):
/* clIEnttest.PHP */
<?PHP
function do_call($host, $port, $request) {
  
   $fp = fsockopen($host, $port, $errno, $errstr);
   $query = "POST /servertest.PHP HTTP/1.0\nUser_Agent: My Egg ClIEnt\nHost: ".$host."\nContent-Type: text/XML\nContent-Length: ".strlen($request)."\n\n".$request."\n";

   if (!fputs($fp, $query, strlen($query))) {
       $errstr = "Write error";
       return 0;
   }

   $contents = @#@#;
   while (!feof($fp)) {
       $contents .= fgets($fp);
   }

   fclose($fp);
   return $contents;
}

$host = @#localhost@#;
$port = 80;
$request = XMLrpc_encode_request(@#cycle@#, @#egg@#);
$response = do_call($host, $port, $request);
/* do something with $response, e.g. print it */
?>

/* servertest.PHP */
<?PHP
function lifecycle($method, $params) {
/* $method = @#cycle@#, $params = (array of) request parameter(s); $data is also passed from XMLrpc_server_call_method, if we had any data to pass */
   switch($params[0]) {
       case @#egg@#:
           $reply = @#All eggs will be birds one day.@#;
       break;
       default:
           $reply = @#That must have been an otheregg@#;
   }
   return $reply;
}

$server = XMLrpc_server_create();

/* register the @#external@# name and then the @#internal@# name */
XMLrpc_server_register_method($server, "cycle", "lifecycle");

$request = $HTTP_RAW_POST_DATA; // no you don@#t need @#always on@#, and no $_POST doesn@#t work.

/* the parameters here are @#server, XML-string and user data@#.  There@#s supposed to be an optional @#output options@# array too, but I can@#t get it working :( hence header() call */
$response = XMLrpc_server_call_method($server, $request, null);
header(@#Content-Type: text/XML@#);
print $response;

XMLrpc_server_destroy($server);
?>


這個例子中客戶端得到$response後沒有作任何的處理,這個返回值是這個樣子的字符串:
HTTP/1.1 200 OK
Date: Thu, 04 Nov 2004 08:21:43 GMT
Server: apache/2.0.47 (Win32) PHP/5.0.1
X-Powered-By: PHP/5.0.1
Connection: close
Content-Type: text/XML;charset=GB2312

<?XML version="1.0" encoding="iso-8859-1"?>
<methodResponse>
<params>
 <param>
  <value>
   <string>All eggs will be birds one day.</string>
  </value>
 </param>
</params>
</methodResponse>

這個字符串包含了兩個部分,一個是Html的頭信息,一個是XML-rpc包文件,對他稍微進行一下處理就可以滿足我們的要求了.

 

下面看看我修改過的例子,主要用了一個XMLrpc_decode函數,網上沒有找到說明,自己試了半天,才試出來他的用法:
/* clIEnttest.PHP */
<?PHP
function do_call($host, $port, $request) {

   $fp = fsockopen($host, $port, $errno, $errstr);
   $query = "POST /servertest.PHP HTTP/1.0\nUser_Agent: My Egg ClIEnt\nHost: ".$host."\nContent-Type: text/XML\nContent-Length: ".strlen($request)."\n\n".$request."\n";

   if (!fputs($fp, $query, strlen($query))) {
       $errstr = "Write error";
       return 0;
   }

   $contents = @#@#;
   while (!feof($fp)) {
       $contents .= fgets($fp);
   }

   fclose($fp);
   return $contents;
}

$host = @#localhost@#;
$port = 80;
$request = XMLrpc_encode_request(@#cycle@#, @#egg@#);
$response = do_call($host, $port, $request);
/* do something with $response, e.g. print it */

$pos = strpos($response,  @#<?XML@#);
$str = substr($response, $pos);
$tmp = XMLrpc_decode($str);
print_r($tmp);

/* do something with $tmp e.g. print it */
?>
 

/* servertest.PHP */
<?PHP
function lifecycle($method, $params) {
/* $method = @#cycle@#, $params = (array of) request parameter(s); $data is also passed from XMLrpc_server_call_method, if we had any data to pass */
   switch($params[0]) {
       case @#egg@#:
         $reply[] = @#All eggs will be birds one day.我市好人@#;
           $reply[] = @#All eggs will be birds one day.我市好人aaaaa@#;
       break;
       default:
           $reply[] = @#That must have been an otheregg我市好人@#;
   }
   return $reply;
}

$server = XMLrpc_server_create();

/* register the @#external@# name and then the @#internal@# name */
XMLrpc_server_register_method($server, "cycle", "lifecycle");

$request = $HTTP_RAW_POST_DATA; // no you don@#t need @#always on@#, and no $_POST doesn@#t work.

/* the parameters here are @#server, XML-string and user data@#.  There@#s supposed to be an optional @#output options@# array too, but I can@#t get it working :( hence header() call */
$response = XMLrpc_server_call_method($server, $request, null);
header(@#Content-Type: text/XML@#);
print ($response);

XMLrpc_server_destroy($server);
?>

運行一下,看看顯示的數組的樣式,這樣處理起來就舒服多了.

明白了這個,別閒著,看看PHP5自帶的"SOAP Functions",使用方法和工作原理,大同小異啊.

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved