WireHttp class

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 WireHttp class also inherits all the methods and properties of: Wire.

Show $var?             Show args?        

Common

NameReturnSummary 
$http->download(string $fromURL, string $toFile)
stringDownload a file from a URL and save it locally 
$http->get(string $url)
bool stringSend a GET request to URL 
$http->getAllowSchemes()
arrayReturn array of allowed schemes 
$http->getError()
string arrayGet a string of the last error message 
$http->getErrorCodes()
arrayReturn array of all possible HTTP error codes as (code => description) 
$http->getHttpCode()
int stringGet last HTTP code 
$http->getHttpCodes()
arrayReturn array of all possible HTTP codes as (code => description) 
$http->getJSON(string $url)
bool array objectSend to a URL that responds with JSON (using GET request) and return the resulting array or object. 
$http->getResponseHeaderValues()
array string nullGet last HTTP response headers with multi-value headers as arrays 
$http->getResponseHeaders()
array string nullGet the last HTTP response headers (associative array) 
$http->getSuccessCodes()
arrayReturn array of all possible HTTP success codes as (code => description) 
$http->getTimeout()
floatGet the number of seconds till connection times out 
$http->getUserAgent()
stringGet the current user-agent header 
$http->head(string $url)
bool arraySend to a URL using a HEAD request 
$http->post(string $url)
bool stringSend a POST request to a URL 
$http->send(string $url)
bool stringSend the given $data array to a URL using given method (i.e. POST, GET, PUT, DELETE, etc.)
$http->sendFile(string $filename)
(nothing)Send the contents of the given filename to the current http connection. 
$http->set(string $key, $value)
$thisSet a variable to be included in the POST/GET request 
$http->setAllowSchemes($schemes)
$thisSet schemes WireHttp is allowed to access (default=[http, https]) 
$http->setData(array $data)
$thisSet an array of data, removes any existing data 
$http->setHeader(string $key, string $value)
$thisSend an individual header to send 
$http->setHeaders(array $headers)
$thisSet an array of headers, removes any existing headers 
$http->setTimeout($seconds)
$thisSet the number of seconds till connection times out 
$http->setValidateURLOptions()
arraySet options array given to $sanitizer->url() 
$http->status(string $url)
bool integer stringSend to a URL using a HEAD request and return the status code 
$http->statusText(string $url)
bool stringSend to a URL using HEAD and return the status code and text like "200 OK" 
$http->validateURL(string $url)
stringValidate a URL for WireHttp use 

Additional methods and properties

In addition to the methods and properties above, WireHttp also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.123

Twitter updates

  • New post: In this week’s post, we’ll take a look a look at the new website and focus on some parts of it and how they were built. Then we’ll dive into the latest version of ProcessWire on the dev branch, version 3.0.124— More
    11 January 2019
  • New ProcessWire main site rebuild ready for preview— More
    4 January 2019
  • Happy Holidays! This latest master version of ProcessWire contains hundreds of new additions, fixes, optimizations and more. This post covers all the highlights— More
    21 December 2018

Latest news

  • ProcessWire Weekly #244
    In the 244th issue of ProcessWire Weekly we'll cover the latest core updates, introduce the new processwire.com site, ask a PHP version related question in the weekly poll, and showcase the site of an open source room bookings app. Read on!
    Weekly.pw / 12 January 2019
  • ProcessWire 3.0.124 and new website launched
    In this week’s post, we’ll take a look a look at the new website and focus on some parts of it and how they were built. Then we’ll dive into the latest version of ProcessWire on the dev branch, version 3.0.124.
    Blog / 11 January 2019
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits