Sends either sync or async request based on async option.
public request ( string $method = 'GET', string $uri = '', array $payload = [], array $headers = [] ) : |
||
$method | string | |
$uri | string | |
$payload | array | - either used as the request body or url query params |
$headers | array | |
return | Promise or Response depending on sync or async request |
public function testRequest() { $responseMock = Mockery::mock('Psr\\Http\\Message\\ResponseInterface'); $this->resource->setOptions(['async' => false]); $this->clientMock->shouldReceive('sendRequest')->andReturn($responseMock); $this->assertInstanceOf('SparkPost\\SparkPostResponse', $this->resource->request('POST', 'transmissions', $this->postTransmissionPayload)); $promiseMock = Mockery::mock('Http\\Promise\\Promise'); $this->resource->setOptions(['async' => true]); $this->clientMock->shouldReceive('sendAsyncRequest')->andReturn($promiseMock); $this->assertInstanceOf('SparkPost\\SparkPostPromise', $this->resource->request('GET', 'transmissions', $this->getTransmissionPayload)); }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('POST', 'templates', ["name" => "PHP example template", "content" => ["from" => "from@YOUR_DOMAIN", "subject" => "Your Subject", "html" => "<b>Write your message here.</b>"]]); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('PUT', 'templates/TEMPLATE_ID', ['options' => ['open_tracking' => true]]); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('GET', 'message-events', ['campaign_ids' => 'CAMPAIGN_ID']); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('GET', 'templates'); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('DELETE', 'templates/TEMPLATE_ID'); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('GET', 'templates/TEMPLATE_ID?draft=true'); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }
<?php namespace Examples\Templates; require dirname(__FILE__) . '/../bootstrap.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Http\Adapter\Guzzle6\Client as GuzzleAdapter; $httpClient = new GuzzleAdapter(new Client()); /* * configure options in example-options.json */ $sparky = new SparkPost($httpClient, $options); $promise = $sparky->request('POST', 'templates/TEMPLATE_ID/preview?draft=true', ['substitution_data' => ['some_key' => 'some_value']]); try { $response = $promise->wait(); echo $response->getStatusCode() . "\n"; print_r($response->getBody()) . "\n"; } catch (\Exception $e) { echo $e->getCode() . "\n"; echo $e->getMessage() . "\n"; }