HTTP Client - Static Usage

Overview

The Zend\Http component also provides Zend\Http\ClientStatic, a static HTTP client which exposes a simplified API for quickly performing GET and POST operations:

Quick Start

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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',
    array( 'foo' => 'bar' ),
    array( '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', array(
    'username' => 'foo',
    'password' => 'bar',
));

Configuration Options

It is not possible to set configuration options on the Zend\Http\Client instance encapsulated by Zend\Http\ClientStatic. To perform a HTTP request which requires non-default configurations, please use Zend\Http\Client directly.

Available Methods

get

get(string $url, array $query = array(), array $headers = array(), mixed $body = null)

Perform an HTTP GET request using the provided URL, query string variables, headers and request body.

Returns Zend\Http\Response

post

post(string $url, array $params, array $headers = array(), mixed $body = null)

Perform an HTTP POST request using the provided URL, parameters, headers and request body.

Returns Zend\Http\Response

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to HTTP Client - Static Usage on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.