Documentation

SocketFetcher extends WebFetchApi
in package

Fetches data from HTTP URLs via socket connections.

Table of Contents

Properties

$current_redirect  : int
$error_code  : int
$error_message  : string
$host  : string
$max_redirect  : int
$port  : int
$response  : array<string|int, mixed>
$scheme_handlers  : array<string|int, mixed>
$timeout  : int
$backcompat  : array<string|int, mixed>
$buffer_size  : int
$fp  : resource
$is_chunked  : bool
$keep_alive  : bool
$line_break  : string

Methods

__construct()  : mixed
Constructor.
__destruct()  : mixed
Destructor.
closeConnection()  : void
Closes the socket connection.
exportStatic()  : void
Provides a way to export a class's public static properties and methods to global namespace.
fetch()  : string|false
Get the contents of a URL, irrespective of allow_url_fopen.
request()  : object
Main calling function.
result()  : mixed
Used to return the results to the caller.
resultRaw()  : array<string|int, mixed>
Will return all results from all loops (redirects).
buildPostData()  : string
Takes supplied POST data and urlencodes it.

Properties

$current_redirect

public int $current_redirect = 0

How many redirects have been followed.

$error_code

public int $error_code = 0

Error number that occurred in the system-level connect() call.

$error_message

public string $error_message = ''

The error message as a string.

$max_redirect

public int $max_redirect = 3

Maximum number of redirects.

$response

public array<string|int, mixed> $response = []

Stores responses (url, code, error, headers, body, size).

$scheme_handlers

public static array<string|int, mixed> $scheme_handlers = ['ftp' => ['FtpFetcher'], 'ftps' => ['FtpFetcher'], 'http' => ['SocketFetcher', 'CurlFetcher'], 'https' => ['SocketFetcher', 'CurlFetcher']]

Specifies the fetcher class or classes to try for any given URL scheme.

Keys are URL schemes. Values are the names of one or more classes to try.

Class names will be prepended with NAMESPACE . '\APIs'.

$timeout

public int $timeout = 5

The connection timeout, in seconds.

$backcompat

private static array<string|int, mixed> $backcompat = ['func_names' => ['fetch' => 'fetch_web_data']]

BackwardCompatibility settings for this class.

$buffer_size

private int $buffer_size = 4096

Whether to keep the socket connection open after the initial request.

$fp

private resource $fp

File pointer for the open socket connection.

$is_chunked

private bool $is_chunked = false

Whether the response is transfered as chunked data.

$keep_alive

private bool $keep_alive

Whether to keep the socket connection open after the initial request.

$line_break

private string $line_break = "\r\n"

The line break

Methods

__construct()

Constructor.

public __construct([bool $keep_alive = false ][, int $max_redirect = 3 ]) : mixed
Parameters
$keep_alive : bool = false

Whether to keep the socket connection open after the initial request.

$max_redirect : int = 3

Maximum number of redirects.

__destruct()

Destructor.

public __destruct() : mixed

Ensures that the socket connection has been closed before the object is destroyed.

closeConnection()

Closes the socket connection.

public closeConnection() : void

exportStatic()

Provides a way to export a class's public static properties and methods to global namespace.

public static exportStatic() : void

To do so:

  1. Use this trait in the class.
  2. At the END of the class's file, call its exportStatic() method.

Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.

Regarding the $backcompat items:

A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names']. Likewise, a class's static methods are not exported as global functions unless explicitly included in $backcompat['func_names'].

$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.

$backcompat['func_names'] is a simple array where the keys are the names of one or more of a class's static methods, and the values are the names that should be used for global functions that will encapsulate those methods. Methods that are not named in this array will not be exported.

Adding non-static properties or methods to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.

fetch()

Get the contents of a URL, irrespective of allow_url_fopen.

public static fetch(string $url[, string|array<string|int, mixed> $post_data = [] ][, bool $keep_alive = false ]) : string|false
  • Reads the contents of an HTTP or FTP address and returns those contents in a string.
  • If $post_data is supplied, the value and length is posted to the given URL as form data.
Parameters
$url : string

An HTTP or FTP URL.

$post_data : string|array<string|int, mixed> = []

The data to post to the given URL. Not applicable to FTP requests.

$keep_alive : bool = false

Whether to keep the connection alive for further requests. Not applicable to FTP requests.

Return values
string|false

The fetched data or false on failure.

request()

Main calling function.

public request(string|Url $url[, array<string|int, mixed>|string $post_data = [] ]) : object
  • Will request the page data from a given $url.
  • Optionally will post data to the page form if $post_data is supplied. Passed arrays will be converted to a POST string joined with &'s.
Parameters
$url : string|Url

the site we are going to fetch

$post_data : array<string|int, mixed>|string = []

any post data as form name => value

Return values
object

A reference to the object for method chaining.

result()

Used to return the results to the caller.

public result([string $area = null ]) : mixed
  • Called as ->result() will return the full final array.
  • Called as ->result('body') to return the page source of the result.
Parameters
$area : string = null

Used to return an area such as body, header, error.

Return values
mixed

The response.

resultRaw()

Will return all results from all loops (redirects).

public resultRaw([int $response_number = null ]) : array<string|int, mixed>
  • Can be called as ->result_raw(x) where x is a specific loop's result.
  • Call as ->result_raw() for everything.
Parameters
$response_number : int = null

Which response to get, or null for all.

Return values
array<string|int, mixed>

The specified response or all the responses.

buildPostData()

Takes supplied POST data and urlencodes it.

protected buildPostData(array<string|int, mixed>|string $post_data) : string
  • Forms the data (for POST) into a string like var=xyz&var2=abc&var3=123
  • Drops vars with @ since we don't support sending files (uploading)
Parameters
$post_data : array<string|int, mixed>|string

The raw POST data.

Return values
string

A string of POST data.


        
On this page

Search results