Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 401 and 402]

(no description)

File Size: 2057 lines (85 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.

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

param: string $path either the PATH part of the xml-rpc 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

setOption($name, $value)   X-Ref
No description

getOption($name)   X-Ref

param: string $name see all the OPT_ constants
return: mixed

getOptions()   X-Ref
Returns the complete list of Client options, with their value.

return: array

setOptions($options)   X-Ref

param: array $options key: any valid option (see all the OPT_ constants)
return: $this

setDebug($level)   X-Ref
Enable/disable the echoing to screen of the xml-rpc responses received. The default is not to output anything.

The debugging information at level 1 includes the raw data returned from the XML-RPC server it was querying
(including bot HTTP headers and the full XML payload), and the PHP value the client attempts to create to
represent the value returned by the server.
At level 2, the complete payload of the xml-rpc request is also printed, before being sent to the server.
At level -1, the Response objects returned by send() calls will not carry information about the http response's
cookies, headers and body, which might save some memory

This option can be very useful when debugging servers as it allows you to see exactly what the client sends and
the server returns. Never leave it enabled for production!

param: integer $level values -1, 0, 1 and 2 are supported
return: $this

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
return: $this

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
return: $this

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
return: $this

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
return: $this

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
return: $this

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
return: $this

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 see  CURL_SSLVERSION_ constants
return: $this

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
return: $this

setAcceptedCompression($compMethod)   X-Ref
Enables/disables reception of compressed xml-rpc 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 xml-rpc requests.
It is up to the xml-rpc server to return compressed responses when receiving such requests.

param: string $compMethod either 'gzip', 'deflate', 'any' or ''
return: $this

setRequestCompression($compMethod)   X-Ref
Enables/disables http compression of xml-rpc 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 ''
return: $this

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 the xml-rpc payload).

NB: by default all cookies set via this method are sent to the server, regardless of path/domain/port. Taking
advantage of those values is left to the single developer.

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
param: string $domain
param: int $port do not use! Cookies are not separated by port
return: $this

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

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

param: array $options
return: $this

setUseCurl($useCurlMode)   X-Ref

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

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
return: $this

getUrl($component = null)   X-Ref

param: null|int $component allowed values: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_PATH
return: string|int Notes: the path component will include query string and fragment; NULL is a valid value for port

send($req, $timeout = 0, $method = '')   X-Ref
Send an xml-rpc 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 deprecated. Connection timeout, in seconds, If unspecified, the timeout set with setOption
param: string $method deprecated. Use the same value in the constructor instead.
return: Response|Response[] Note that the client will always return a Response object, even if the call fails

sendViaSocket($req, $method, $server, $port, $path, $opts)   X-Ref

param: Request $req
param: string $method
param: string $server
param: int $port
param: string $path
param: array $opts
return: Response

sendViaCURL($req, $method, $server, $port, $path, $opts)   X-Ref
Contributed by Justin Miller
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 $method
param: string $server
param: int $port
param: string $path
param: array $opts the keys/values match self::getOptions
return: Response

createCURLHandle($req, $method, $server, $port, $path, $opts)   X-Ref

param: Request $req
param: string $method
param: string $server
param: int $port
param: string $path
param: array $opts the keys/values match self::getOptions
return: \CurlHandle|resource|false

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 xml-rpc 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
2np 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: bool $noFallback When true, upon receiving an error during multicall, multiple single calls will not be
param: string $method deprecated. Was: "the http protocol variant to be used. See the details in the docs for the send() method."
param: boolean $fallback deprecated. Was: "w"hen true, upon receiving an error during multicall, multiple single
return: Response[]

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

param: Request[] $reqs
param: int $timeout
param: string $method
return: Response[]|Response a single Response when the call returned a fault / does not conform to what we expect

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

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

param: $req
param: $server
param: $port
param: $timeout
param: $username
param: $password
param: $authType
param: $cert
param: $certPass
param: $caCert
param: $caCertDir
param: $proxyHost
param: $proxyPort
param: $proxyUsername
param: $proxyPassword
param: $proxyAuthType
param: $method
param: $keepAlive
param: $key
param: $keyPass
param: $sslVersion
return: false|\CurlHandle|resource

__get($name)   X-Ref
No description

__set($name, $value)   X-Ref
No description

__isset($name)   X-Ref
No description

__unset($name)   X-Ref
No description