server 端同上篇用 perl 寫的,client 端改用 php 來 call。
參考
PHP:
-
/**
-
* Debian: apt-get install php5-xmlrpc php5-curl
-
*/
-
-
/**
-
The MIT License
-
Copyright (c) 2007
-
Permission is hereby granted, free of charge, to any person obtaining a copy
-
of this software and associated documentation files (the "Software"), to deal
-
in the Software without restriction, including without limitation the rights
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-
copies of the Software, and to permit persons to whom the Software is
-
furnished to do so, subject to the following conditions:
-
The above copyright notice and this permission notice shall be included in
-
all copies or substantial portions of the Software.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-
THE SOFTWARE.
-
*
-
* @author: Tsung
-
* @date: 2007/4/29
-
* @version: v1.00
-
*
-
* @brief xmlrpc format generator
-
* XMLRPC format:
-
*
-
*
-
* $method_name
-
* * *
-
* <$value_type>$value
-
*
-
* * *
-
*
-
* USAGE:
-
* $re = encode_xmlrpc('echo', array( array('string' => 'Linux test')) );
-
* $xml = xmlrpc_request('http://localhost/', $re);
-
* $xml = xmlrpc_decode($xml);
-
*
-
* $re = encode_xmlrpc('echo', array( 0 => array('string' => 'Linux test'), 1 => array('string' => 'test') ));
-
* $xml = xmlrpc_request('http://localhost/', $re);
-
* $xml = xmlrpc_decode($xml);
-
*
-
* @param method string:xmlrpc function name
-
* @param params array: array( 0 => array('type', $value), 1 => array('type', $value) )
-
* @retval xmlrpc_format/false
-
*/
-
function encode_xmlrpc($method, $params)
-
{
-
-
$response_pre = '';
-
$response_pre.= "$method";
-
$response_pre.= " ";
-
-
// xmlrpc spec: http://www.xmlrpc.com/spec, allow tag list
-
-
$response_mid = '';
-
foreach ($params as $param) {
-
foreach($param as $type => $val) {
-
return false;
-
}
-
-
$response_mid .= " <$type>$val";
-
}
-
}
-
}
-
-
$response_post = '';
-
-
return $response_pre . $response_mid . $response_post;
-
}
-
-
/**
-
* HTTP Post
-
* @param url
-
* @param postvar: args
-
*/
-
-
function postUrl($url, $postvar) {
-
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $url);
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
-
curl_setopt($ch, CURLOPT_POST, 1);
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar);
-
-
$res = curl_exec ($ch);
-
curl_close ($ch);
-
-
return $res;
-
}
-
-
/**
-
* @brief xmlrpc request
-
* @param url xmlrpc url
-
* @param method string:xmlrpc function name
-
* @param params array: array( 0 => array('type', $value), 1 => array('type', $value) )
-
* @retval array/false
-
*/
-
function request_xmlrpc($url, $method='', $params='') {
-
return false;
-
}
-
-
$postvar = encode_xmlrpc($method, $params);
-
$res = postUrl($url, $postvar);
-
$res = xmlrpc_decode($res);
-
-
return $res;
-
}
-
-
/* test */
-
-
$xml = postUrl('http://127.0.0.1:1234/RPC2', $re);
-
$xml = xmlrpc_decode($xml);