Requests Library Overview

The Requests library is an HTTP client for PHP, bundled with WordPress. It provides a clean API for making HTTP requests with support for multiple transports, cookies, authentication, proxies, and parallel requests.

Source: wp-includes/Requests/
Namespace: WpOrgRequests
Version: 2.0.11


Architecture

┌─────────────────────────────────────────────────────┐
│                   Requests (static)                  │
│  Main entry point — get(), post(), request(), etc.  │
├──────────────┬──────────────────────────────────────┤
│              │                                       │
│   Session    │   Wraps Requests with shared          │
│              │   defaults, base URL, cookie jar      │
├──────────────┴──────────────────────────────────────┤
│                Transport Layer                        │
│  ┌──────────────────┐  ┌─────────────────────────┐  │
│  │  TransportCurl  │  │  TransportFsockopen    │  │
│  │  (preferred)     │  │  (fallback)             │  │
│  └──────────────────┘  └─────────────────────────┘  │
├─────────────────────────────────────────────────────┤
│                Support Classes                        │
│  Response / ResponseHeaders / Cookie / CookieJar   │
│  Hooks / HookManager / Capability                    │
└─────────────────────────────────────────────────────┘

Request Flow

  1. Requests::request() receives URL, headers, data, type, options
  2. Options merged with defaults (OPTION_DEFAULTS)
  3. set_defaults() normalizes auth, proxy, cookies, IDN encoding
  4. Hook: requests.before_request
  5. Transport selected (cURL preferred, fsockopen fallback) based on capabilities
  6. Transport executes request, returns raw HTTP response
  7. Hook: requests.before_parse
  8. parse_response() splits headers/body, handles chunked encoding, decompression, redirects
  9. Hook: requests.after_request
  10. Returns Response object

Component Reference

ComponentDocumentationSourceDescription
Requestsclass-requests.mdsrc/Requests.phpStatic entry point — HTTP methods, transport selection, response parsing
Responseclass-requests-response.mdsrc/Response.phpResponse data object with body, headers, status, cookies
ResponseHeadersclass-requests-response-headers.mdsrc/Response/Headers.phpCase-insensitive header dictionary with multi-value support
Sessionclass-requests-session.mdsrc/Session.phpPersistent session with shared defaults and cookie jar
HookManager / Hooksclass-requests-hooks.mdsrc/HookManager.php, src/Hooks.phpEvent dispatcher interface and implementation
Cookieclass-requests-cookie.mdsrc/Cookie.phpCookie parsing, validation, and formatting (RFC 6265)
CookieJarclass-requests-cookie-jar.mdsrc/Cookie/Jar.phpCookie collection with automatic request/response handling
TransportCurlclass-requests-transport-curl.mdsrc/Transport/Curl.phpcURL-based HTTP transport with multi-request support
TransportFsockopenclass-requests-transport-fsockopen.mdsrc/Transport/Fsockopen.phpSocket-based HTTP transport (fallback)
Transportclass-requests-transport-curl.mdsrc/Transport.phpTransport interface
Capabilitysrc/Capability.phpInterface defining capability constants (SSL)

Legacy Compatibility

The file library/Requests.php is a legacy/compat wrapper. It simply includes WordPress’s class-requests.php (wp-includes/class-requests.php), which existed before the library was namespaced. This bridges the old PSR-0 Requests class name to the new WpOrgRequestsRequests namespace.

php
// library/Requests.php — deprecated since WordPress 6.2.0
include_once ABSPATH . WPINC . '/class-requests.php';

Hook Points

The library dispatches hooks at key points during request processing:

HookParametersWhen
requests.before_request&$url, &$headers, &$data, &$type, &$optionsBefore transport executes
requests.before_parse&$response, $url, $headers, $data, $type, $optionsAfter transport, before parsing
requests.before_redirect_check&$return, $req_headers, $req_data, $optionsBefore redirect evaluation
requests.before_redirect&$location, &$headers, &$data, &$options, $returnBefore following a redirect
requests.after_request&$return, $req_headers, $req_data, $optionsAfter parsing complete
request.progress$data, $size, $max_bytesDuring body streaming
transport.internal.parse_response&$response, $requestInternal: multi-request parsing
transport.internal.parse_error&$response, $requestInternal: multi-request error
multiple.request.complete&$response, $idEach request in a batch completes
curl.before_request&$handlecURL: before handle setup
curl.before_send&$handlecURL: before curl_exec
curl.after_send(none)cURL: after curl_exec
curl.after_request&$headers, &$infocURL: after processing
curl.before_multi_add&$subhandlecURL: before adding to multi handle
curl.before_multi_exec&$multihandlecURL: before multi exec loop
curl.after_multi_exec&$multihandlecURL: after multi exec loop
fsockopen.before_request(none)fsockopen: before request
fsockopen.remote_socket&$remote_socketfsockopen: socket address
fsockopen.remote_host_path&$path, $urlfsockopen: request path
fsockopen.after_headers&$outfsockopen: after header construction
fsockopen.before_send&$outfsockopen: before socket write
fsockopen.after_send$outfsockopen: after socket write
fsockopen.after_request&$headers, &$infofsockopen: after request complete

Default Options

OptionTypeDefaultDescription
timeoutfloat10Response timeout (seconds)
connect_timeoutfloat10Connection timeout (seconds)
useragentstringphp-requests/2.0.11User-Agent header
protocol_versionfloat1.1HTTP protocol version
redirectedint0Current redirect count (internal)
redirectsint10Maximum redirects to follow
follow_redirectsbooltrueFollow 3xx redirects
blockingbooltrueBlock for response
typestringGETHTTP method
filenamestring|falsefalseFile to stream body to
authAuth|array|falsefalseAuthentication handler
proxyProxy|array|string|falsefalseProxy configuration
cookiesJar|array|falsefalseCookie jar
max_bytesint|falsefalseResponse body size limit
idnbooltrueEnable IDN parsing
hooksHookManager|nullnullHook dispatcher (auto-created)
transportTransport|string|nullnullCustom transport
verifystring|bool|nullcertificates/cacert.pemSSL certificate verification
verifynamebooltrueVerify SSL common name