xmlrpc.inc
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· INC 代码 · 共 2,042 行 · 第 1/5 页
INC
2,042 行
/**
* List of http compression methods accepted by the client for responses.
* NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib
*
* NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since
* in those cases it will be up to CURL to decide the compression methods
* it supports. You might check for the presence of 'zlib' in the output of
* curl_version() to determine wheter compression is supported or not
*/
var $accepted_compression = array();
/**
* Name of compression scheme to be used for sending requests.
* Either null, gzip or deflate
*/
var $request_compression = '';
/**
* CURL handle: used for keep-alive connections (PHP 4.3.8 up, see:
* http://curl.haxx.se/docs/faq.html#7.3)
*/
var $xmlrpc_curl_handle = null;
/// Wheter to use persistent connections for http 1.1 and https
var $keepalive = false;
/// Charset encodings that can be decoded without problems by the client
var $accepted_charset_encodings = array();
/// Charset encoding to be used in serializing request. NULL = use ASCII
var $request_charset_encoding = '';
/**
* Decides the content of xmlrpcresp objects returned by calls to send()
* valid strings are 'xmlrpcvals', 'phpvals' or 'xml'
*/
var $return_type = 'xmlrpcvals';
/**
* @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php
* @param string $server the server name / ip address
* @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used
* @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed
*/
function xmlrpc_client($path, $server='', $port='', $method='')
{
// allow user to specify all params in $path
if($server == '' and $port == '' and $method == '')
{
$parts = parse_url($path);
$server = $parts['host'];
$path = $parts['path'];
if(isset($parts['query']))
{
$path .= '?'.$parts['query'];
}
if(isset($parts['fragment']))
{
$path .= '#'.$parts['fragment'];
}
if(isset($parts['port']))
{
$port = $parts['port'];
}
if(isset($parts['scheme']))
{
$method = $parts['scheme'];
}
if(isset($parts['user']))
{
$this->username = $parts['user'];
}
if(isset($parts['pass']))
{
$this->password = $parts['pass'];
}
}
if($path == '' || $path[0] != '/')
{
$this->path='/'.$path;
}
else
{
$this->path=$path;
}
$this->server=$server;
if($port != '')
{
$this->port=$port;
}
if($method != '')
{
$this->method=$method;
}
// if ZLIB is enabled, let the client by default accept compressed responses
if(function_exists('gzinflate') || (
function_exists('curl_init') && (($info = curl_version()) &&
((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version'])))
))
{
$this->accepted_compression = array('gzip', 'deflate');
}
// keepalives: enabled by default ONLY for PHP >= 4.3.8
// (see http://curl.haxx.se/docs/faq.html#7.3)
if(version_compare(phpversion(), '4.3.8') >= 0)
{
$this->keepalive = true;
}
// by default the xml parser can support these 3 charset encodings
$this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');
}
/**
* Enables/disables the echoing to screen of the xmlrpc responses received
* @param integer $debug values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
* @access public
*/
function setDebug($in)
{
$this->debug=$in;
}
/**
* Add some http BASIC AUTH credentials, used by the client to authenticate
* @param string $u username
* @param string $p password
* @param integer $t auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC (basic auth)
* @access public
*/
function setCredentials($u, $p, $t=1)
{
$this->username=$u;
$this->password=$p;
$this->authtype=$t;
}
/**
* Add a client-side https certificate
* @param string $cert
* @param string $certpass
* @access public
*/
function setCertificate($cert, $certpass)
{
$this->cert = $cert;
$this->certpass = $certpass;
}
/**
* Add a CA certificate to verify server with (see man page about
* CURLOPT_CAINFO for more details
* @param string $cacert certificate file name (or dir holding certificates)
* @param bool $is_dir set to true to indicate cacert is a dir. defaults to false
* @access public
*/
function setCaCertificate($cacert, $is_dir=false)
{
if ($is_dir)
{
$this->cacert = $cacert;
}
else
{
$this->cacertdir = $cacert;
}
}
/**
* Set attributes for SSL communication: private SSL key
* @param string $key The name of a file containing a private SSL key
* @param string $keypass The secret password needed to use the private SSL key
* @access public
* NB: does not work in older php/curl installs
* Thanks to Daniel Convissor
*/
function setKey($key, $keypass)
{
$this->key = $key;
$this->keypass = $keypass;
}
/**
* Set attributes for SSL communication: verify server certificate
* @param bool $i enable/disable verification of peer certificate
* @access public
*/
function setSSLVerifyPeer($i)
{
$this->verifypeer = $i;
}
/**
* Set attributes for SSL communication: verify match of server cert w. hostname
* @param int $i
* @access public
*/
function setSSLVerifyHost($i)
{
$this->verifyhost = $i;
}
/**
* Set proxy info
* @param string $proxyhost
* @param string $proxyport Defaults to 8080 for HTTP and 443 for HTTPS
* @param string $proxyusername Leave blank if proxy has public access
* @param string $proxypassword Leave blank if proxy has public access
* @param int $proxyauthtype set to constant CURLAUTH_NTLM to use NTLM auth with proxy
* @access public
*/
function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1)
{
$this->proxy = $proxyhost;
$this->proxyport = $proxyport;
$this->proxy_user = $proxyusername;
$this->proxy_pass = $proxypassword;
$this->proxy_authtype = $proxyauthtype;
}
/**
* Enables/disables reception of compressed xmlrpc responses.
* Note that enabling reception of compressed responses merely adds some standard
* http headers to xmlrpc requests. It is up to the xmlrpc server to return
* compressed responses when receiving such requests.
* @param string $compmethod either 'gzip', 'deflate', 'any' or ''
* @access public
*/
function setAcceptedCompression($compmethod)
{
if ($compmethod == 'any')
$this->accepted_compression = array('gzip', 'deflate');
else
$this->accepted_compression = array($compmethod);
}
/**
* Enables/disables http compression of xmlrpc request.
* Take care when sending compressed requests: servers might not support them
* (and automatic fallback to uncompressed requests is not yet implemented)
* @param string $compmethod either 'gzip', 'deflate' or ''
* @access public
*/
function setRequestCompression($compmethod)
{
$this->request_compression = $compmethod;
}
/**
* Adds a cookie to list of cookies that will be sent to server.
* NB: setting any param but name and value will turn the cookie into a 'version 1' cookie:
* do not do it unless you know what you are doing
* @param string $name
* @param string $value
* @param string $path
* @param string $domain
* @param int $port
* @access public
*
* @todo check correctness of urlencoding cookie value (copied from php way of doing it...)
*/
function setCookie($name, $value='', $path='', $domain='', $port=null)
{
$this->cookies[$name]['value'] = urlencode($value);
if ($path || $domain || $port)
{
$this->cookies[$name]['path'] = $path;
$this->cookies[$name]['domain'] = $domain;
$this->cookies[$name]['port'] = $port;
$this->cookies[$name]['version'] = 1;
}
else
{
$this->cookies[$name]['version'] = 0;
}
}
/**
* Send an xmlrpc request
* @param mixed $msg The message object, or an array of messages for using multicall, or the complete xml representation of a request
* @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply
* @param string $method if left unspecified, the http protocol chosen during creation of the object will be used
* @return xmlrpcresp
* @access public
*/
function& send($msg, $timeout=0, $method='')
{
// if user deos not specify http protocol, use native method of this client
// (i.e. method set during call to constructor)
if($method == '')
{
$method = $this->method;
}
if(is_array($msg))
{
// $msg is an array of xmlrpcmsg's
$r = $this->multicall($msg, $timeout, $method);
return $r;
}
elseif(is_string($msg))
{
$n =& new xmlrpcmsg('');
$n->payload = $msg;
$msg = $n;
}
// where msg is an xmlrpcmsg
$msg->debug=$this->debug;
if($method == 'https')
{
$r =& $this->sendPayloadHTTPS(
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password,
$this->authtype,
$this->cert,
$this->certpass,
$this->cacert,
$this->cacertdir,
$this->proxy,
$this->proxyport,
$this->proxy_user,
$this->proxy_pass,
$this->proxy_authtype,
$this->keepalive,
$this->key,
$this->keypass
);
}
elseif($method == 'http11')
{
$r =& $this->sendPayloadCURL(
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password,
$this->authtype,
null,
null,
null,
null,
$this->proxy,
$this->proxyport,
$this->proxy_user,
$this->proxy_pass,
$this->proxy_authtype,
'http',
$this->keepalive
);
}
else
{
$r =& $this->sendPayloadHTTP10(
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password,
$this->authtype,
$this->proxy,
$this->proxyport,
$this->proxy_user,
$this->proxy_pass,
$this->proxy_authtype
);
}
return $r;
}
/**
* @access private
*/
function &sendPayloadHTTP10($msg, $server, $port, $timeout=0,
$username='', $password='', $authtype=1, $proxyhost='',
$proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1)
{
if($port==0)
{
$port=80;
}
// Only create the payload if it was not created previously
if(empty($msg->payload))
{
$msg->createPayload($this->request_charset_encoding);
}
$payload = $msg->payload;
// Deflate request body and set appropriate request headers
if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate'))
{
if($this->request_compression == 'gzip')
{
$a = @gzencode($payload);
if($a)
{
$payload = $a;
$encoding_hdr = "Content-Encoding: gzip\r\n";
}
}
else
{
$a = @gzcompress($payload);
if($a)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?