Hooking system
Requests has a hook system that you can use to manipulate parts of the request process along with internal transport hooks.
Check out the API documentation for WpOrg\Requests\Hooks for more
information on how to use the hook system.
Available Hooks
-
requests.before_requestAlter the request before it is sent to the transport.
Parameters:
string &$url,array &$headers,array|null &$data,string &$type,array &$options -
request.progressRun events based on how much body data has been received up to this point for the current request.
Parameters:
string $data,int $response_bytes,int|bool $response_byte_limit -
requests.before_parseAlter the raw HTTP response before parsing.
Parameters:
string &$response,string $url,array $headers,array|null $data,string $type,array $options -
requests.before_redirect_checkAlter the response object before a potential redirect is detected and acted on.
Parameters:
WpOrg\Requests\Response &$return,array $req_headers,array $req_data,array $options -
requests.before_redirectAlter the request arguments before a redirect is acted upon.
Parameters:
string &$location,array &$req_headers,array &$req_data,array &$options,WpOrg\Requests\Response $return -
requests.after_requestAlter the response object before it is returned to the user.
Parameters:
WpOrg\Requests\Response &$return,array &$req_headers,array &$req_data,array &$options -
multiple.request.completeAlter the response for an individual request in a multi-request.
Parameters:
WpOrg\Requests\Response &$response,string|int $idDo not register this hook directly. To use this hook, pass a callback via
$options['complete']when callingWpOrg\Requests\Requests\request_multiple(). -
curl.before_requestSet cURL options before the transport sets any (note that Requests may override these).
Parameters:
cURL resource|CurlHandle &$handle -
curl.before_sendSet cURL options just before the request is actually sent via
curl_exec().Parameters:
cURL resource|CurlHandle &$handle -
curl.after_sendRun events after sending the request, but before interpreting the response.
Parameters: -
-
curl.after_requestAlter the raw HTTP response before returning for parsing.
Parameters:
string &$headers,[array &$info]The optional
$infoparameter contains the associated array as defined in the return value for curl_getinfo().
This optional parameter will be present when a blocking request was made ($options['blocking' = true) and will not be present when a non-blocking request was made ($options['blocking' = false). The callback signature needs to be adapted accordingly.
-
curl.before_multi_addSet cURL options before adding the request to a cURL multi handle.
Parameters:
cURL resource|CurlHandle &$subhandle -
curl.before_multi_execSet cURL options before executing a cURL multi handle request.
Parameters:
cURL resource|CurlMultiHandle &$multihandle -
curl.after_multi_execRun events before a cURL multi handle is closed.
Parameters:
cURL resource|CurlMultiHandle &$multihandle -
fsockopen.before_requestRun events before the transport does anything.
-
fsockopen.remote_socketAlter the remote socket.
Parameters:
string &$remote_socket -
fsockopen.remote_host_pathAlter the remote host path.
Parameters:
string &$path,string $url -
fsockopen.after_headersAdd extra headers before the body begins (i.e. before
\r\n\r\n).Parameters:
string &$out -
fsockopen.before_sendAdd body data before sending the request.
Parameters:
string &$out -
fsockopen.after_sendRun events after writing the data to the socket.
Parameters:
string $out -
fsockopen.after_requestAlter the raw HTTP response before returning for parsing.
Parameters:
string &$headers,[array &$info]The optional
$infoparameter contains the associated array as defined in the return value for stream_get_meta_data().
This optional parameter will be present when a blocking request was made ($options['blocking' = true) and will not be present when a non-blocking request was made ($options['blocking' = false). The callback signature needs to be adapted accordingly.
Registering Hooks
Note: if you’re doing this in an authentication handler, see the Custom Authentication guide instead.
In order to register your own hooks, you need to instantiate WpOrg\Requests\Hooks
and pass the object in via the 'hooks' option.
$hooks = new \WpOrg\Requests\Hooks();
$hooks->register('requests.after_request', 'mycallback');
$request = \WpOrg\Requests\Requests::get('https://httpbin.org/get', array(), array('hooks' => $hooks));
Previous: Requests through proxy
Next: Upgrading to Requests 2.0
Is something missing in this documentation or did you find something to be wrong?
Please create an issue to help us improve the documentation.