WireHttp enables you to send HTTP requests to URLs, download files, and more.
// Get an instance of WireHttp
$http = new WireHttp();
// Get the contents of a URL
$response = $http->get("http://domain.com/path/");
if($response !== false) {
echo "Successful response: " . $sanitizer->entities($response);
} else {
echo "HTTP request failed: " . $http->getError();
}
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Wire
class also inherits all the methods and properties of: Wire.
Common
Name | Return | Summary | |
---|---|---|---|
$http->getError() $http->getError() $http->getError(bool $getArray = false) | string array | Get a string of the last error message | |
$http->sendStatusHeader() $http->sendStatusHeader($status) $http->sendStatusHeader($status) | (nothing) | Send an HTTP status header |
Advanced
Name | Return | Summary | |
---|---|---|---|
$http->setData() $http->setData($data) $http->setData($data) | $this | Set an array of data, or string of raw data to send with next GET/POST/etc. request (overwriting the existing data or rawData) | |
$http->setValidateURLOptions() $http->setValidateURLOptions() $http->setValidateURLOptions(array $options = []) | array | Set options array given to $sanitizer->url() |
HTTP requests
Files
Name | Return | Summary | |
---|---|---|---|
$http->download() $http->download(string $fromURL, string $toFile) $http->download(string $fromURL, string $toFile, array $options = []) | string | Download a file from a URL and save it locally | |
$http->sendFile() $http->sendFile($filename) $http->sendFile($filename, array $options = [], array $headers = []) | int | Send the contents of the given filename to the current http connection. |
Request headers
Name | Return | Summary | |
---|---|---|---|
$http->getHeaders() $http->getHeaders() $http->getHeaders() | array | Get all currently set request headers in an associative array | |
$http->getUserAgent() $http->getUserAgent() $http->getUserAgent() | string | Get the current user-agent header | |
$http->setCookie() $http->setCookie(string $name, $value) $http->setCookie(string $name, $value) | self | Set cookie(s) for http GET/POST/etc. request (currently used by curl option only) | |
$http->setHeader() $http->setHeader(string $key, string $value) $http->setHeader(string $key, string $value) | $this | Send an individual request header to send with GET/POST/etc. request | |
$http->setHeaders() $http->setHeaders(array $headers) $http->setHeaders(array $headers, array $options = []) | $this | Set an array of request headers to send with GET/POST/etc. request | |
$http->setUserAgent() $http->setUserAgent(string $userAgent) $http->setUserAgent(string $userAgent) | (nothing) | Set the current user-agent header |
Response headers
Name | Return | Summary | |
---|---|---|---|
$http->getResponseHeader() $http->getResponseHeader() $http->getResponseHeader(string $key = '') | array string null | Get the last HTTP response headers (normal array). | |
$http->getResponseHeaderValues() $http->getResponseHeaderValues() $http->getResponseHeaderValues(string $key = '', bool $forceArrays = false) | array string null | Get last HTTP response headers with multi-value headers as arrays | |
$http->getResponseHeaders() $http->getResponseHeaders() $http->getResponseHeaders(string $key = '') | array string null | Get the last HTTP response headers (associative array) |
HTTP codes
Name | Return | Summary | |
---|---|---|---|
$http->getErrorCodes() $http->getErrorCodes() $http->getErrorCodes() | array | Return array of all possible HTTP error codes as (code => description) | |
$http->getHttpCode() $http->getHttpCode() $http->getHttpCode(bool $withText = false) | int string | Get last HTTP code | |
$http->getHttpCodes() $http->getHttpCodes() $http->getHttpCodes() | array | Return array of all possible HTTP codes as (code => description) | |
$http->getSuccessCodes() $http->getSuccessCodes() $http->getSuccessCodes() | array | Return array of all possible HTTP success codes as (code => description) |
Settings
Name | Return | Summary | |
---|---|---|---|
$http->getAllowSchemes() $http->getAllowSchemes() $http->getAllowSchemes() | array | Return array of allowed schemes | |
$http->getTimeout() $http->getTimeout() $http->getTimeout() | float | Get the number of seconds till connection times out | |
$http->setAllowSchemes() $http->setAllowSchemes($schemes) $http->setAllowSchemes($schemes, bool $replace = false) | $this | Set schemes Wire | |
$http->setTimeout() $http->setTimeout($seconds) $http->setTimeout($seconds) | $this | Set the number of seconds till connection times out |
Additional methods and properties
In addition to the methods and properties above, Wire
API reference based on ProcessWire core version 3.0.236