Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

(no description)

File Size: 1351 lines (56 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: Client  - X-Ref

Used to represent a client of an XML-RPC server.

getLogger()   X-Ref
Sent to servers in http headers.


setLogger($logger)   X-Ref
No description

__construct($path, $server = '', $port = '', $method = '')   X-Ref

param: string $path either the PATH part of the xmlrpc server URL, or complete server URL (in which case you
param: string $server the server name / ip address
param: integer $port the port the server is listening on, when omitted defaults to 80 or 443 depending on
param: string $method the http protocol variant: defaults to 'http'; 'https', 'http11', 'h2' and 'h2c' can

setDebug($level)   X-Ref
No description

setCredentials($user, $password, $authType = 1)   X-Ref
Sets the username and password for authorizing the client to the server.

With the default (HTTP) transport, this information is used for HTTP Basic authorization.
Note that username and password can also be set using the class constructor.
With HTTP 1.1 and HTTPS transport, NTLM and Digest authentication protocols are also supported. To enable them use
the constants CURLAUTH_DIGEST and CURLAUTH_NTLM as values for the auth type parameter.

param: string $user username
param: string $password password
param: integer $authType auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC

setCertificate($cert, $certPass = '')   X-Ref
Set the optional certificate and passphrase used in SSL-enabled communication with a remote server.

Note: to retrieve information about the client certificate on the server side, you will need to look into the
environment variables which are set up by the webserver. Different webservers will typically set up different
variables.

param: string $cert the name of a file containing a PEM formatted certificate
param: string $certPass the password required to use it

setCaCertificate($caCert, $isDir = false)   X-Ref
Add a CA certificate to verify server with in SSL-enabled communication when SetSSLVerifypeer has been set to TRUE.

See the php manual page about CURLOPT_CAINFO for more details.

param: string $caCert certificate file name (or dir holding certificates)
param: bool $isDir set to true to indicate cacert is a dir. defaults to false

setKey($key, $keyPass)   X-Ref
Set attributes for SSL communication: private SSL key.

NB: does not work in older php/curl installs.
Thanks to Daniel Convissor.

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

setSSLVerifyPeer($i)   X-Ref
Set attributes for SSL communication: verify the remote host's SSL certificate, and cause the connection to fail
if the cert verification fails.

By default, verification is enabled.
To specify custom SSL certificates to validate the server with, use the setCaCertificate method.

param: bool $i enable/disable verification of peer certificate

setSSLVerifyHost($i)   X-Ref
Set attributes for SSL communication: verify the remote host's SSL certificate's common name (CN).

Note that support for value 1 has been removed in cURL 7.28.1

param: int $i Set to 1 to only the existence of a CN, not that it matches

setSSLVersion($i)   X-Ref
Set attributes for SSL communication: SSL version to use. Best left at 0 (default value): let cURL decide

param: int $i

setProxy($proxyHost, $proxyPort, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1)   X-Ref
Set proxy info.

NB: CURL versions before 7.11.10 cannot use a proxy to communicate with https servers.

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 defaults to CURLAUTH_BASIC (Basic authentication protocol); set to constant CURLAUTH_NTLM

setAcceptedCompression($compMethod)   X-Ref
Enables/disables reception of compressed xmlrpc responses.

This requires the "zlib" extension to be enabled in your php install. If it is, by default xmlrpc_client
instances will enable reception of compressed content.
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 ''

setRequestCompression($compMethod)   X-Ref
Enables/disables http compression of xmlrpc request.

This requires the "zlib" extension to be enabled in your php install.
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 ''

setCookie($name, $value = '', $path = '', $domain = '', $port = null)   X-Ref
Adds a cookie to list of cookies that will be sent to server with every further request (useful e.g. for keeping
session info outside of the xml-rpc payload).

NB: By default cookies are sent using the 'original/netscape' format, which is also the same as the RFC 2965;
setting any param but name and value will turn the cookie into a 'version 1' cookie (i.e. RFC 2109 cookie) that
might not be fully supported by the server. Note that RFC 2109 has currently 'historic' status...

param: string $name nb: will not be escaped in the request's http headers. Take care not to use CTL chars or
param: string $value
param: string $path leave this empty unless the xml-rpc server only accepts RFC 2109 cookies
param: string $domain leave this empty unless the xml-rpc server only accepts RFC 2109 cookies
param: int $port leave this empty unless the xml-rpc server only accepts RFC 2109 cookies

setCurlOptions($options)   X-Ref
Directly set cURL options, for extra flexibility (when in cURL mode).

It allows eg. to bind client to a specific IP interface / address.

param: array $options

setUseCurl($useCurlMode)   X-Ref

param: int $useCurlMode self::USE_CURL_ALWAYS, self::USE_CURL_AUTO or self::USE_CURL_NEVER

setUserAgent($agentString)   X-Ref
Set user-agent string that will be used by this client instance in http headers sent to the server.

The default user agent string includes the name of this library and the version number.

param: string $agentString

send($req, $timeout = 0, $method = '')   X-Ref
Send an xmlrpc request to the server.

param: Request|Request[]|string $req The Request object, or an array of requests for using multicall, or the
param: integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply.
param: string $method valid values are 'http', 'http11', 'https', 'h2' and 'h2c'. If left unspecified,
return: Response|Response[] Note that the client will always return a Response object, even if the call fails

sendPayloadHTTP10($req, $server, $port, $timeout = 0, $username = '', $password = '',$authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1,$method='http')   X-Ref

param: Request $req
param: string $server
param: int $port
param: int $timeout
param: string $username
param: string $password
param: int $authType
param: string $proxyHost
param: int $proxyPort
param: string $proxyUsername
param: string $proxyPassword
param: int $proxyAuthType
param: string $method
return: Response

sendPayloadHTTPS($req, $server, $port, $timeout = 0, $username = '', $password = '',$authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,$proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '',$sslVersion = 0)   X-Ref

param: Request $req
param: string $server
param: int $port
param: int $timeout
param: string $username
param: string $password
param: int $authType
param: string $cert
param: string $certPass
param: string $caCert
param: string $caCertDir
param: string $proxyHost
param: int $proxyPort
param: string $proxyUsername
param: string $proxyPassword
param: int $proxyAuthType
param: bool $keepAlive
param: string $key
param: string $keyPass
param: int $sslVersion
return: Response

sendPayloadSocket($req, $server, $port, $timeout = 0, $username = '', $password = '',$authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,$proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method='http', $key = '', $keyPass = '',$sslVersion = 0)   X-Ref

param: Request $req
param: string $server
param: int $port
param: int $timeout
param: string $username
param: string $password
param: int $authType only value supported is 1
param: string $cert
param: string $certPass
param: string $caCert
param: string $caCertDir
param: string $proxyHost
param: int $proxyPort
param: string $proxyUsername
param: string $proxyPassword
param: int $proxyAuthType only value supported is 1
param: string $method 'http' (synonym for 'http10'), 'http10' or 'https'
param: string $key
param: string $keyPass @todo not implemented yet.
param: int $sslVersion @todo not implemented yet. See http://php.net/manual/en/migration56.openssl.php
return: Response

sendPayloadCURL($req, $server, $port, $timeout = 0, $username = '', $password = '',$authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,$proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'https', $keepAlive = false, $key = '',$keyPass = '', $sslVersion = 0)   X-Ref
Contributed by Justin Miller <justin@voxel.net>
Requires curl to be built into PHP
NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers!

param: Request $req
param: string $server
param: int $port
param: int $timeout
param: string $username
param: string $password
param: int $authType
param: string $cert
param: string $certPass
param: string $caCert
param: string $caCertDir
param: string $proxyHost
param: int $proxyPort
param: string $proxyUsername
param: string $proxyPassword
param: int $proxyAuthType
param: string $method 'http' (let curl decide), 'http10', 'http11', 'https', 'h2c' or 'h2'
param: bool $keepAlive
param: string $key
param: string $keyPass
param: int $sslVersion
return: Response

prepareCurlHandle($req, $server, $port, $timeout = 0, $username = '', $password = '',$authType = 1, $cert = '', $certPass = '', $caCert = '', $caCertDir = '', $proxyHost = '', $proxyPort = 0,$proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method = 'https', $keepAlive = false, $key = '',$keyPass = '', $sslVersion = 0)   X-Ref
No description

multicall($reqs, $timeout = 0, $method = '', $fallback = true)   X-Ref
Send an array of requests and return an array of responses.

Unless $this->no_multicall has been set to true, it will try first to use one single xmlrpc call to server method
system.multicall, and revert to sending many successive calls in case of failure.
This failure is also stored in $this->no_multicall for subsequent calls.
Unfortunately, there is no server error code universally used to denote the fact that multicall is unsupported,
so there is no way to reliably distinguish between that and a temporary failure.
If you are sure that server supports multicall and do not want to fallback to using many single calls, set the
fourth parameter to FALSE.

NB: trying to shoehorn extra functionality into existing syntax has resulted
in pretty much convoluted code...

param: Request[] $reqs an array of Request objects
param: integer $timeout connection timeout (in seconds). See the details in the docs for the send() method
param: string $method the http protocol variant to be used. See the details in the docs for the send() method
param: boolean fallback When true, upon receiving an error during multicall, multiple single calls will be
return: Response[]

_try_multicall($reqs, $timeout, $method)   X-Ref
Attempt to boxcar $reqs via system.multicall.

Returns either an array of Response, a single error Response or false (when received response does not respect
valid multicall syntax).

param: Request[] $reqs
param: int $timeout
param: string $method
return: Response[]|false|mixed|Response