static.md 1.57 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# HTTP Client - Static Usage

zend-http provides another client implementation, `Zend\Http\ClientStatic`, a
static HTTP client which exposes a simplified API for quickly performing one-off `GET`
and `POST` operations.

## Quick Start

```php
use Zend\Http\ClientStatic;

// Simple GET request
$response = ClientStatic::get('http://example.org');

// More complex GET request, specifying query string 'foo=bar' and adding a
// custom header to request JSON data be returned (Accept: application/json):
$response = ClientStatic::get(
    'http://example.org',
    ['foo' => 'bar'],
    ['Accept' => 'application/json']
);

// We can also do a POST request using the same format. Here we POST
// login credentials (username/password) to a login page:
$response = ClientStatic::post(
    'https://example.org/login.php',
    [
        'username' => 'foo',
        'password' => 'bar',
    ]
);
```

## Available Methods

### get()

```php
get(
    string $url,
    array $query = [],
    array $headers = [],
    mixed $body = null,
    $clientOptions = null
) : Response
```

Perform an HTTP `GET` request using the provided URL, query string variables,
headers, and request body. The fifth parameter can be used to pass configuration
options to the HTTP client instance.

### post()

```php
post(
    string $url,
    array $params,
    array $headers = [],
    mixed $body = null,
    $clientOptions = null
) : Response
```

Perform an HTTP `POST` request using the provided URL, parameters, headers, and
request body. The fifth parameter can be used to pass configuration options to
the HTTP client instance.