PHP code example of dpc / guzzle-client

1. Go to this page and download the library: Download dpc/guzzle-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dpc / guzzle-client example snippets


/**
 * @var RequestClientContract
 */
protected $client;

/**
 * @param RequestClientContract $client
 */
public function __construct(RequestClientContract $client)
{
    $this->client = $client;
}

$client = $this->client->make('https://httpbin.org/');

$client->to('get')->withBody([
	'foo' => 'bar'
])->withHeaders([
	'baz' => 'qux'
])->withOptions([
	'allow_redirects' => false
])->asJson()->get();

echo $response->getBody();
echo $response->getStatusCode();

$client = $this->client->make('https://httpbin.org/');

$response = $client->to('get')->with([
    'foo' => 'bar'
], [
    'baz' => 'qux'
], [
    'allow_redirects' => false
])->asFormParams()->get();

echo $response->getBody();
echo $response->getStatusCode();

$client = $this->client->make('https://httpbin.org/');

// Get request
$response = $client->to('brotli')->get();

// Post request
$response = $client->to('post')->withBody([
	'foo' => 'bar'
])->asJson()->post();

// Put request
$response = $client->to('put')->withBody([
	'foo' => 'bar'
])->asJson()->put();

// Patch request
$response = $client->to('patch')->withBody([
	'foo' => 'bar'
])->asJson()->patch();

// Delete request
$response = $client->to('delete?id=1')->delete();


// Headers are easily added using the withHeaders method
$response = $client->to('get')->withHeaders([
	'Authorization' => 'Bearer fooBar'
])->asJson()->get();


// Custom options can be specified for the Guzzle instance
$response = $client->to('redirect/5')->withOptions([
	'allow_redirects' => [
		'max' => 5,
		'protocols' => [
			'http',
			'https'
		]
	]
])->get();

// You can also specify the request method as a string
$response = $client->to('post')->withBody([
	'foo' => 'bar'
])->asJson()->request('post');

$logFile = './guzzle_client_debug_test.log';
$logFileResource = fopen($logFile, 'w+');

$client->debug($logFileResource)->to('post')->withBody([
	'foo' => 'random data'
])->asJson()->post();

fclose($logFileResource);