WpOrgRequestsTransportFsockopen
Socket-based HTTP transport using stream_socket_client(). Serves as the fallback when cURL is unavailable.
Source: wp-includes/Requests/src/Transport/Fsockopen.php
Namespace: WpOrgRequestsTransport
Implements: WpOrgRequestsTransport
Declared: final class Fsockopen
Constants
| Constant | Value | Description |
|---|---|---|
SECOND_IN_MICROSECONDS | 1000000 | Conversion factor for stream_set_timeout() |
Properties
Public
| Property | Type | Description |
|---|---|---|
$headers | string | Raw HTTP data (headers + body after processing) |
$info | array | Stream metadata from stream_get_meta_data() |
Private
| Property | Type | Description |
|---|---|---|
$max_bytes | int|bool | Response byte limit, or false |
$connect_error | string | Cached connection error messages |
Methods
request()
public function request(
string|Stringable $url,
array $headers = [],
string|array $data = [],
array $options = []
): stringPerform an HTTP request via stream_socket_client().
Flow:
- Validates arguments
- Dispatches
fsockopen.before_request - Parses URL, creates stream context
- HTTPS: Configures SSL context options:
verify_peer→true(default)capture_peer_cert→true- SNI enabled if
OPENSSL_TLSEXT_SERVER_NAMEdefined - Custom CA file from
$options['verify'] - Peer name verification from
$options['verifyname']
- Dispatches
fsockopen.remote_sockethook with&$remote_socket - Connects via
stream_socket_client()with connection timeout - For HTTPS, verifies certificate against hostname via
verify_certificate_from_context() - Formats request line and data based on
data_format - Dispatches
fsockopen.remote_host_pathhook - Builds raw HTTP request: method line, Host, User-Agent, Accept-Encoding, custom headers
- Dispatches
fsockopen.after_headers,fsockopen.before_send - Writes to socket via
fwrite() - Dispatches
fsockopen.after_send - For non-blocking: closes socket, returns empty string
- Sets stream timeout (supports fractional seconds via microseconds)
- Reads response in
BUFFER_SIZEchunks, splitting headers from body - Enforces
max_byteslimit on body - Supports streaming to file via
$options['filename'] - Dispatches
fsockopen.after_requestwith&$headers, &$info
Throws:
verify_peer→true(default)capture_peer_cert→true- SNI enabled if
OPENSSL_TLSEXT_SERVER_NAMEdefined - Custom CA file from
$options['verify'] - Peer name verification from
$options['verifyname']
Auto-added headers (if not already set):
verify_peer→true(default)capture_peer_cert→true- SNI enabled if
OPENSSL_TLSEXT_SERVER_NAMEdefined - Custom CA file from
$options['verify'] - Peer name verification from
$options['verifyname']
request_multiple()
public function request_multiple( array $requests, array $options ): arraySend multiple requests sequentially (no parallel support). Creates a new Fsockopen instance per request, dispatches transport.internal.parse_response and multiple.request.complete hooks.
On exception, stores the Exception object as the response for that request ID.
Throws: InvalidArgument for invalid parameter types.
test() (static)
public static function test( array $capabilities = [] ): boolChecks:
fsockopen()function exists- If
Capability::SSLrequested, checksopensslextension andopenssl_x509_parse()function
verify_certificate_from_context()
public function verify_certificate_from_context( string $host, resource $context ): boolVerify the SSL certificate against hostname using the stream context’s peer certificate. PHP doesn’t check Subject Alternative Names by default; this method uses Ssl::verify_certificate() to handle that.
Throws:
InvalidArgumentfor invalid parameter typesException— Invalid URL (invalidurl)Exception— Connection failure (fsockopen.connect_errororfsockopenerror)Exception— Socket timeout (timeout)Exception— SSL mismatch (ssl.no_match)Exception— File open failure (fopen)
connect_error_handler()
public function connect_error_handler( int $errno, string $errstr ): boolCustom error handler for stream_socket_client(). Captures E_WARNING and E_NOTICE messages into $this->connect_error. Returns false for other error types.
Private Methods
| Method | Description |
|---|---|
accept_encoding() | Returns Accept-Encoding header value: deflate;q=1.0, compress;q=0.5, gzip;q=0.5 based on available PHP functions |
format_get($url_parts, $data) | Builds request path with query string from parsed URL parts and data |
Differences from cURL Transport
| Feature | Curl | Fsockopen |
|---|---|---|
| Parallel requests | True multi-handle parallelism | Sequential (one at a time) |
| SSL verification | Via cURL options (CURLOPT_SSL_*) | Via stream context + manual cert check |
| Compression | cURL handles automatically (CURLOPT_ENCODING) | Accept-Encoding header + Requests decompression |
| Timeout precision | Millisecond (cURL ≥ 7.16.2) | Microsecond via stream_set_timeout() |
| HTTP construction | cURL builds request internally | Manually constructs raw HTTP request text |