Http

HTTP Network Access

 class Winter\Storm\Network\Http

Used as a cURL wrapper for the HTTP protocol.

Usage:

Http::get('https://wintercms.com'); Http::post('...'); Http::delete('...'); Http::patch('...'); Http::put('...'); Http::options('...'); Http::head('...');

$result = Http::post('https://wintercms.com'); echo $result; // Outputs:

<title>... echo $result->code; // Outputs: 200 echo $result->headers['Content-Type']; // Outputs: text/html; charset=UTF-8

Http::post('https://wintercms.com', function($http){

  // Sets a HTTP header
  $http->header('Rest-Key', '...');

  // Set a proxy of type (http, socks4, socks5)
  $http->proxy('type', 'host', 'port', 'username', 'password');

  // Use basic authentication
  $http->auth('user', 'pass');

  // Sends data with the request
  $http->data('foo', 'bar');
  $http->data(['key' => 'value', ...]);

  // Disable redirects
  $http->noRedirect();

  // Check host SSL certificate
  $http->verifySSL();

  // Sets the timeout duration
  $http->timeout(3600);

  // Write response to a file
  $http->toFile('some/path/to/a/file.txt');

  // Sets a cURL option manually
  $http->setOption(CURLOPT_SSL_VERIFYHOST, false);

});

Constants

Constant Description
METHOD_DELETE
string "DELETE"
METHOD_GET
string "GET"
METHOD_HEAD
string "HEAD"
METHOD_OPTIONS
string "OPTIONS"
METHOD_PATCH
string "PATCH"
METHOD_POST
string "POST"
METHOD_PUT
string "PUT"

Properties

public $argumentSeparator : string

Argument separator.

public $body : string

The last response body.

public $code : int

The last returned HTTP code.

public $headers : array

The headers to be sent with the request.

public $info : array

The cURL response information.

public $maxRedirects : int

The maximum redirects allowed.

public $method : string

The method the request should use.

public $ok : bool

The last response was successful (ie. the HTTP code was 2xx)

public $rawBody : string

The last response body (without headers extracted).

public $requestData : array | string

Request data.

public $requestHeaders : array

Request headers.

public $requestOptions : array

cURL Options.

public $streamFile : string

If writing response to a file, which file to use.

public $streamFilter : string

If writing response to a file, which write filter to apply.

public $url : string

The HTTP address to use.

protected $redirectCount : int

Internal counter

Methods

public __toString () : string

Handy if this object is called directly.

Returns
string

The last response.

public auth (string $user, string $pass = null) : self

Adds authentication to the comms.

Parameters
Property Description
$user
string
$pass
string
Returns
self

public data (array | string $key, $value = null) : self

Add a data to the request.

Parameters
Property Description
$key
array | string
$value
mixed
Returns
self

public static delete (string $url, callable $options = null) : self

Make a HTTP DELETE call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public static get (string $url, callable $options = null) : self

Make a HTTP GET call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public getRequestData () : string

Return the request data set.

Returns
string

public static head (string $url, callable $options = null) : self

Make a HTTP HEAD call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public header ($key, string $value = null) : self

Add a header to the request.

Parameters
Property Description
$key
mixed
$value
string
Returns
self

public json ($payload) : self

Add JSON encoded payload

Parameters
Property Description
$payload
mixed
Returns
self

public static make (string $url, string $method, callable $options = null)

Make the object with common properties

Parameters
Property Description
$url
string

HTTP request address

$method
string

Request method (GET, POST, PUT, DELETE, etc)

$options
callable

Callable helper function to modify the object

Returns
mixed

public noRedirect ()

Disable follow location (redirects)

Returns
mixed

public static options (string $url, callable $options = null) : self

Make a HTTP OPTIONS call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public static patch (string $url, callable $options = null) : self

Make a HTTP PATCH call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public static post (string $url, callable $options = null) : self

Make a HTTP POST call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public proxy ($type, $host, $port, $username = null, $password = null)

Sets a proxy to use with this request

Parameters
Property Description
$type
mixed
$host
mixed
$port
mixed
$username
mixed
$password
mixed
Returns
mixed

public static put (string $url, callable $options = null) : self

Make a HTTP PUT call.

Parameters
Property Description
$url
string
$options
callable
Returns
self

public send () : self

Execute the HTTP request.

Returns the Http instance in order to be able to inspect and retrieve the response.

Returns
self

public setOption (array | string | int $option, mixed $value = null) : self

Add single or multiple CURL options to this request.

You must either provide a constant or string that represents a CURL_* constant as the $option, and a $value to set a single option, or you may provide an array of CURL_* constants and values instead.

Parameters
Property Description
$option
array | string | int
$value
mixed
Returns
self

public timeout (string $timeout) : self

Sets the request timeout.

Parameters
Property Description
$timeout
string
Returns
self

public toFile (string $path, string $filter = null) : self

Write the response to a file

Parameters
Property Description
$path
string

Path to file

$filter
string

Stream filter as listed in stream_get_filters()

Returns
self

public verifySSL ()

Enable SSL verification

Returns
mixed

protected headerToArray (string $header) : array

Turn a header string into an array.

Parameters
Property Description
$header
string
Returns
array
Copyright © 2024 Winter CMS