guzzle post example

Example 1: guzzlehttp post json example

use GuzzleHttp\Client;

$client = new Client();

$response = $client->post('url', [
    GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar'] // or 'json' => [...]
]);

Example 2: guzzle post request with data

$response = $client->request('POST', 'http://httpbin.org/post', [
    'form_params' => [
        'field_name' => 'abc',
        'other_field' => '123',
        'nested_field' => [
            'nested' => 'hello'
        ]
    ]
]);

Example 3: guzzle login example

$post_data = array(
  'title' => 'My work',
  'description' => 'This is a request created using curl',
  'submit' => 'submit',
);
$response = $http_client->post('/create.php', array(), $post_data)->send();

Tags:

Php Example