rex_socket
in package
Class for sockets.
Example:
try {
//Open socket connection. (Host, Port, SSL)
$socket = rex_socket::factory('www.example.com','443', true);
//set path to rex_socket
$socket->setPath('/url/to/my/resource?param=1');
//set PHP Context-Option
$socket->setOptions([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]);
//make request and get rex_socket_request-Objekt back
$response = $socket->doGet();
//check if status code is 200
if($response->isOk()) {
//get file body
$body = $response->getBody();
}
} catch(rex_socket_exception $e) {
//error message: $e->getMessage()
}
Tags
Table of Contents
Properties
- $acceptCompression : bool
- $followRedirects : false|int
- $headers : array<string, string>
- $host : string
- $options : array<string|int, mixed>
- $path : string
- $port : int
- $ssl : bool
- $stream : resource
- $timeout : int
Methods
- acceptCompression() : $this
- addBasicAuthorization() : $this
- Adds the basic authorization header to the current request.
- addHeader() : $this
- Adds a header to the current request.
- doDelete() : rex_socket_response
- Makes a DELETE request.
- doGet() : rex_socket_response
- Makes a GET request.
- doPost() : rex_socket_response
- Makes a POST request.
- doRequest() : rex_socket_response
- Makes a request.
- factory() : static
- Factory method.
- factoryUrl() : static
- Creates a socket by a full URL.
- followRedirects() : $this
- Sets number of redirects that should be followed automatically.
- setOptions() : $this
- Sets the socket context options.
- setPath() : $this
- Sets the path.
- setTimeout() : $this
- Sets the timeout for the connection.
- __construct() : mixed
- openConnection() : void
- Opens the socket connection.
- parseUrl() : array{host: string, port: int, ssl: bool, path: string}
- Parses a full URL and returns an array with the keys "host", "port", "ssl" and "path".
- writeRequest() : rex_socket_response
- Writes a request to the opened connection.
Properties
$acceptCompression
protected
bool
$acceptCompression
= \false
$followRedirects
protected
false|int
$followRedirects
= \false
$headers
protected
array<string, string>
$headers
= []
$host
protected
string
$host
$options
protected
array<string|int, mixed>
$options
= []
$path
protected
string
$path
= '/'
$port
protected
int
$port
$ssl
protected
bool
$ssl
$stream
protected
resource
$stream
$timeout
protected
int
$timeout
= 15
Methods
acceptCompression()
public
acceptCompression() : $this
Return values
$thisaddBasicAuthorization()
Adds the basic authorization header to the current request.
public
addBasicAuthorization(string $user, string $password) : $this
Parameters
- $user : string
- $password : string
Return values
$this —Current socket
addHeader()
Adds a header to the current request.
public
addHeader(string $key, string $value) : $this
Parameters
- $key : string
- $value : string
Return values
$this —Current socket
doDelete()
Makes a DELETE request.
public
doDelete() : rex_socket_response
Tags
Return values
rex_socket_response —Response
doGet()
Makes a GET request.
public
doGet() : rex_socket_response
Tags
Return values
rex_socket_response —Response
doPost()
Makes a POST request.
public
doPost([mixed $data = '' ][, array<string, array{path: string, type: string}> $files = [] ]) : rex_socket_response
Parameters
- $data : mixed = ''
- $files : array<string, array{path: string, type: string}> = []
-
Files array, e.g.
array('myfile' => array('path' => $path, 'type' => 'image/png'))
Tags
Return values
rex_socket_response —Response
doRequest()
Makes a request.
public
doRequest(string $method[, mixed $data = '' ]) : rex_socket_response
Parameters
- $method : string
-
HTTP method, e.g. "GET"
- $data : mixed = ''
Tags
Return values
rex_socket_response —Response
factory()
Factory method.
public
static factory(string $host[, int $port = 80 ][, bool $ssl = false ]) : static
Parameters
- $host : string
-
Host name
- $port : int = 80
-
Port number
- $ssl : bool = false
-
SSL flag
Tags
Return values
static —Socket instance
factoryUrl()
Creates a socket by a full URL.
public
static factoryUrl(string $url) : static
Parameters
- $url : string
-
URL
Tags
Return values
static —Socket instance
followRedirects()
Sets number of redirects that should be followed automatically.
public
followRedirects(false|int $redirects) : $this
The method only affects GET requests.
Parameters
- $redirects : false|int
-
Number of max redirects
Return values
$this —Current socket
setOptions()
Sets the socket context options.
public
setOptions(array<string|int, mixed> $options) : $this
Available options can be found on https://www.php.net/manual/en/context.php
Parameters
- $options : array<string|int, mixed>
Return values
$this —Current socket
setPath()
Sets the path.
public
setPath(string $path) : $this
Parameters
- $path : string
Return values
$this —Current socket
setTimeout()
Sets the timeout for the connection.
public
setTimeout(int $timeout) : $this
Parameters
- $timeout : int
-
Timeout
Return values
$this —Current socket
__construct()
protected
__construct(string $host[, int $port = 80 ][, bool $ssl = false ]) : mixed
Parameters
- $host : string
-
Host name
- $port : int = 80
-
Port number
- $ssl : bool = false
-
SSL flag
openConnection()
Opens the socket connection.
protected
openConnection() : void
Tags
parseUrl()
Parses a full URL and returns an array with the keys "host", "port", "ssl" and "path".
protected
static parseUrl(string $url) : array{host: string, port: int, ssl: bool, path: string}
Parameters
- $url : string
-
Full URL
Tags
Return values
array{host: string, port: int, ssl: bool, path: string} —URL parts
writeRequest()
Writes a request to the opened connection.
protected
writeRequest(string $method, string $path[, array<string, string> $headers = [] ][, mixed $data = '' ]) : rex_socket_response
Parameters
- $method : string
-
HTTP method, e.g. "GET"
- $path : string
-
Path
- $headers : array<string, string> = []
-
Headers
- $data : mixed = ''
Tags
Return values
rex_socket_response —Response