PHP code example of cmdrsharp / guzzle-api

1. Go to this page and download the library: Download cmdrsharp/guzzle-api 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/ */

    

cmdrsharp / guzzle-api example snippets


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

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

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

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

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

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

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

// GET
$response = $this->client->to('brotli')->get();

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

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

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

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


// CUSTOM HEADER
$response = $this->client->to('get')->withHeaders([
	'Authorization' => 'Bearer fooBar'
])->asJson()->get();


// CUSTOM OPTIONS
$response = $this->client->to('redirect/5')->withOptions([
	'allow_redirects' => [
		'max' => 5,
		'protocols' => [
			'http',
			'https'
		]
	]
])->get();

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

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

fclose($logFileResource);