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-8Http::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 | Type | Value | Description |
|---|---|---|---|
METHOD_DELETE
|
string |
"DELETE"
|
string
"DELETE"
|
METHOD_GET
|
string |
"GET"
|
string
"GET"
|
METHOD_HEAD
|
string |
"HEAD"
|
string
"HEAD"
|
METHOD_OPTIONS
|
string |
"OPTIONS"
|
string
"OPTIONS"
|
METHOD_PATCH
|
string |
"PATCH"
|
string
"PATCH"
|
METHOD_POST
|
string |
"POST"
|
string
"POST"
|
METHOD_PUT
|
string |
"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
= 10
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
= null
Internal counter
Methods
public __toString () : string
Handy if this object is called directly.
The last response.
public
auth (string $user, string $pass = null)
: self
Adds authentication to the comms.
| Property | Type | Description |
|---|---|---|
| $user | string |
string
|
| $pass | string |
string
|
public
data (array | string $key, $value = null)
: self
Add a data to the request.
| Property | Type | Description |
|---|---|---|
| $key | array | string |
array | string
|
| $value | mixed |
mixed
|
public
static
delete (string $url, callable $options = null)
: self
Make a HTTP DELETE call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public
static
get (string $url, callable $options = null)
: self
Make a HTTP GET call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public getRequestData () : string
Return the request data set.
public
static
head (string $url, callable $options = null)
: self
Make a HTTP HEAD call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public
header ($key, string $value = null)
: self
Add a header to the request.
| Property | Type | Description |
|---|---|---|
| $key | mixed |
mixed
|
| $value | string |
string
|
public json ($payload) : self
Add JSON encoded payload
| Property | Type | Description |
|---|---|---|
| $payload | mixed |
mixed
|
public
static
make (string $url, string $method, callable $options = null)
Make the object with common properties
| Property | Type | Description |
|---|---|---|
| $url | string |
string
HTTP request address |
| $method | string |
string
Request method (GET, POST, PUT, DELETE, etc) |
| $options | callable |
callable
Callable helper function to modify the object |
public noRedirect ()
Disable follow location (redirects)
public
static
options (string $url, callable $options = null)
: self
Make a HTTP OPTIONS call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public
static
patch (string $url, callable $options = null)
: self
Make a HTTP PATCH call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public
static
post (string $url, callable $options = null)
: self
Make a HTTP POST call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public
proxy ($type, $host, $port, $username = null, $password = null)
Sets a proxy to use with this request
| Property | Type | Description |
|---|---|---|
| $type | mixed |
mixed
|
| $host | mixed |
mixed
|
| $port | mixed |
mixed
|
| $username | mixed |
mixed
|
| $password | mixed |
mixed
|
public
static
put (string $url, callable $options = null)
: self
Make a HTTP PUT call.
| Property | Type | Description |
|---|---|---|
| $url | string |
string
|
| $options | callable |
callable
|
public send () : self
Execute the HTTP request.
Returns the Http instance in order to be able to inspect and retrieve the response.
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.
| Property | Type | Description |
|---|---|---|
| $option | array | string | int |
array | string | int
|
| $value | mixed |
mixed
|
public timeout (string $timeout) : self
Sets the request timeout.
| Property | Type | Description |
|---|---|---|
| $timeout | string |
string
|
public
toFile (string $path, string $filter = null)
: self
Write the response to a file
| Property | Type | Description |
|---|---|---|
| $path | string |
string
Path to file |
| $filter | string |
string
Stream filter as listed in stream_get_filters() |
public verifySSL ()
Enable SSL verification
protected headerToArray (string $header) : array
Turn a header string into an array.
| Property | Type | Description |
|---|---|---|
| $header | string |
string
|