📄 xmlrpc.php
字号:
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) { $payload = $a; $encoding_hdr = "Content-Encoding: deflate\r\n"; } } } else { $encoding_hdr = ''; } // thanks to Grant Rauscher <grant7@firstworld.net> for this $credentials=''; if($username!='') { $credentials='Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n"; if ($authtype != 1) { error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth is supported with HTTP 1.0'); } } $accepted_encoding = ''; if(is_array($this->accepted_compression) && count($this->accepted_compression)) { $accepted_encoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n"; } $proxy_credentials = ''; if($proxyhost) { if($proxyport == 0) { $proxyport = 8080; } $connectserver = $proxyhost; $connectport = $proxyport; $uri = 'http://'.$server.':'.$port.$this->path; if($proxyusername != '') { if ($proxyauthtype != 1) { error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth to proxy is supported with HTTP 1.0'); } $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername.':'.$proxypassword) . "\r\n"; } } else { $connectserver = $server; $connectport = $port; $uri = $this->path; } // Cookie generation, as per rfc2965 (version 1 cookies) or // netscape's rules (version 0 cookies) $cookieheader='';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -