From 6171690e4d54d5c872a4442226d166e837afb550 Mon Sep 17 00:00:00 2001 From: tuck1s Date: Tue, 16 Mar 2021 12:04:22 +0000 Subject: [PATCH] Updated and tested all examples. README updated. Update transmissions GET & DELETE deprecations --- .gitignore | 6 +++ CHANGELOG.md | 9 ++-- README.md | 30 ++++++++++--- examples/debug/index.php | 4 +- .../message-events/get_message_events.php | 6 ++- .../get_message_events_with_retry_logic.php | 6 ++- examples/templates/create_template.php | 44 +++++++++++++++++-- examples/templates/delete_template.php | 7 ++- examples/templates/get_all_templates.php | 3 +- examples/templates/get_template.php | 7 ++- examples/templates/preview_template.php | 7 ++- examples/templates/update_template.php | 7 ++- .../transmissions/create_transmission.php | 11 +++-- .../create_transmission_with_attachment.php | 11 +++-- .../create_transmission_with_cc_and_bcc.php | 17 ++++--- ...reate_transmission_with_recipient_list.php | 13 ++++-- .../create_transmission_with_template.php | 13 ++++-- .../transmissions/delete_transmission.php | 8 +++- .../transmissions/get_all_transmissions.php | 25 +---------- examples/transmissions/get_transmission.php | 25 +---------- 20 files changed, 164 insertions(+), 95 deletions(-) diff --git a/.gitignore b/.gitignore index a2e132e..a27257c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,9 @@ test/output/ /composer.phar composer.lock test.php +.php-version +.gitignore +.phpunit.result.cache +.DS_Store +.gitignore +.vscode/launch.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b10d2..a5489fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,14 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased][unreleased] -## [2.2.1] - 2021-03-08 +- [#200](https://github.com/SparkPost/php-sparkpost/pull/200) PHP 8 support -- [#198](https://github.com/SparkPost/php-sparkpost/pull/198) -- [#191](https://github.com/SparkPost/php-sparkpost/pull/191) +## [2.2.1] - 2021-03-08 +- [#198](https://github.com/SparkPost/php-sparkpost/pull/198) Address #197: No longer need formfeed replacement. README work. +- [#191](https://github.com/SparkPost/php-sparkpost/pull/191) Updating License ## [2.2.0] - 2019-06-04 -- [#187](https://github.com/SparkPost/php-sparkpost/pull/169) Updated composer.json +- [#187](https://github.com/SparkPost/php-sparkpost/pull/187) Updated composer.json - [#169](https://github.com/SparkPost/php-sparkpost/pull/169) Optional automatic retry on 5xx - [#166](https://github.com/SparkPost/php-sparkpost/pull/166/files) Quick fix for using the API without composer - [#149](https://github.com/SparkPost/php-sparkpost/pull/149) Setters should return current object diff --git a/README.md b/README.md index 8150250..38e0c2b 100644 --- a/README.md +++ b/README.md @@ -170,12 +170,9 @@ Sends an asynchronous request to the SparkPost API and returns a `SparkPostPromi * Type: `Array` * See constructor - +--- ## Endpoints ### transmissions -* **get([transmissionID] [, payload])** - * `transmissionID` - see `uri` request options - * `payload` - see request options * **post(payload)** * `payload` - see request options * `payload.cc` @@ -186,8 +183,8 @@ Sends an asynchronous request to the SparkPost API and returns a `SparkPostPromi * Required: No * Type: `Array` * Recipients to descreetly recieve a carbon copy of the transmission -* **delete(transmissionID)** - * `transmissionID` - see `uri` request options + +For complete list of endpoints, refer to [API documentation](https://developers.sparkpost.com/api/). ## Examples @@ -252,6 +249,26 @@ var_dump($results); ?> ``` +More examples [here](./examples/): +### Transmission +- Create with attachment +- Create with recipient list +- Create with cc and bcc +- Create with template +- Create +- Delete (scheduled transmission by campaign_id *only*) + +### Template +- Create +- Get +- Get (list) all +- Update +- Delete + +### Message Events +- get +- get (with retry logic) + ### Send An API Call Using The Base Request Function We provide a base request function to access any of our API resources. ```php @@ -274,7 +291,6 @@ $promise = $sparky->request('GET', 'metrics/ip-pools', [ ?> ``` - ## Handling Responses The API calls either return a `SparkPostPromise` or `SparkPostResponse` depending on if `async` is `true` or `false` diff --git a/examples/debug/index.php b/examples/debug/index.php index 84120f0..15e3b8f 100644 --- a/examples/debug/index.php +++ b/examples/debug/index.php @@ -14,8 +14,8 @@ * configure options in example-options.json */ $sparky = new SparkPost($httpClient, [ - "key" => "YOUR_API_KEY", - // This will expose your API KEY - do not use this in production. + "key" => getenv('SPARKPOST_API_KEY'), + // fetch API KEY from environment variable "debug" => true ]); diff --git a/examples/message-events/get_message_events.php b/examples/message-events/get_message_events.php index 0a3c4ac..92bc70b 100644 --- a/examples/message-events/get_message_events.php +++ b/examples/message-events/get_message_events.php @@ -10,9 +10,11 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY",]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); -$promise = $sparky->request('GET', 'message-events', [ +// New endpoint - https://developers.sparkpost.com/api/events/ +$promise = $sparky->request('GET', 'events/message', [ 'campaign_ids' => 'CAMPAIGN_ID', ]); diff --git a/examples/message-events/get_message_events_with_retry_logic.php b/examples/message-events/get_message_events_with_retry_logic.php index 68225cb..82efd2a 100644 --- a/examples/message-events/get_message_events_with_retry_logic.php +++ b/examples/message-events/get_message_events_with_retry_logic.php @@ -10,9 +10,11 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY", "retries" => 3]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY'), "retries" => 3]); -$promise = $sparky->request('GET', 'message-events', [ +// New endpoint - https://developers.sparkpost.com/api/events/ +$promise = $sparky->request('GET', 'events/message', [ 'campaign_ids' => 'CAMPAIGN_ID', ]); diff --git a/examples/templates/create_template.php b/examples/templates/create_template.php index ada8ff1..1d697d5 100644 --- a/examples/templates/create_template.php +++ b/examples/templates/create_template.php @@ -10,14 +10,50 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); + +$template_name = "PHP example template"; +$template_id = "PHP-example-template"; + +// put your own sending domain here +$sending_domain = "steve2-test.trymsys.net"; + +// Valid short template content examples +$plain_text = 'Write your text message part here.'; + +$html = << + + +

Write your HTML message part here

+ + +HTML; + +$amp_html = << + + + + + + + +Hello World! Let's get started using AMP HTML together! + + +HTML; $promise = $sparky->request('POST', 'templates', [ - 'name' => 'PHP example template', + 'name' => $template_name, + 'id' => $template_id, 'content' => [ - 'from' => 'from@YOUR_DOMAIN', + 'from' => "from@$sending_domain", 'subject' => 'Your Subject', - 'html' => 'Write your message here.', + 'text' => $plain_text, + 'html' => $html, + 'amp_html' => $amp_html, ], ]); diff --git a/examples/templates/delete_template.php b/examples/templates/delete_template.php index 906a308..609e67e 100644 --- a/examples/templates/delete_template.php +++ b/examples/templates/delete_template.php @@ -10,9 +10,12 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); -$promise = $sparky->request('DELETE', 'templates/TEMPLATE_ID'); +$template_id = "PHP-example-template"; + +$promise = $sparky->request('DELETE', "templates/$template_id"); try { $response = $promise->wait(); diff --git a/examples/templates/get_all_templates.php b/examples/templates/get_all_templates.php index 289ccb8..87a5b4d 100644 --- a/examples/templates/get_all_templates.php +++ b/examples/templates/get_all_templates.php @@ -10,7 +10,8 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); $promise = $sparky->request('GET', 'templates'); diff --git a/examples/templates/get_template.php b/examples/templates/get_template.php index 1cd4d42..3dd8849 100644 --- a/examples/templates/get_template.php +++ b/examples/templates/get_template.php @@ -10,9 +10,12 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); -$promise = $sparky->request('GET', 'templates/TEMPLATE_ID?draft=true'); +$template_id = "PHP-example-template"; + +$promise = $sparky->request('GET', "templates/$template_id?draft=true"); try { $response = $promise->wait(); diff --git a/examples/templates/preview_template.php b/examples/templates/preview_template.php index 60f38c1..51675a2 100644 --- a/examples/templates/preview_template.php +++ b/examples/templates/preview_template.php @@ -10,9 +10,12 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); -$promise = $sparky->request('POST', 'templates/TEMPLATE_ID/preview?draft=true', [ +$template_id = "PHP-example-template"; + +$promise = $sparky->request('POST', "templates/$template_id/preview?draft=true", [ 'substitution_data' => [ 'some_key' => 'some_value', ], diff --git a/examples/templates/update_template.php b/examples/templates/update_template.php index bc6ed53..2f79cfd 100644 --- a/examples/templates/update_template.php +++ b/examples/templates/update_template.php @@ -10,9 +10,12 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); -$promise = $sparky->request('PUT', 'templates/TEMPLATE_ID', [ +$template_id = "PHP-example-template"; + +$promise = $sparky->request('PUT', "templates/$template_id", [ 'options' => [ 'open_tracking' => true, ], diff --git a/examples/transmissions/create_transmission.php b/examples/transmissions/create_transmission.php index a46edd2..6204ff8 100644 --- a/examples/transmissions/create_transmission.php +++ b/examples/transmissions/create_transmission.php @@ -10,13 +10,18 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); + +// put your own sending domain and test recipient address here +$sending_domain = "steve2-test.trymsys.net"; +$your_email = "bob@sink.sparkpostmail.com"; $promise = $sparky->transmissions->post([ 'content' => [ 'from' => [ 'name' => 'SparkPost Team', - 'email' => 'from@sparkpostbox.com', + 'email' => "from@$sending_domain", ], 'subject' => 'First Mailing From PHP', 'html' => '

Congratulations, {{name}}!

You just sent your very first mailing!

', @@ -27,7 +32,7 @@ [ 'address' => [ 'name' => 'YOUR_NAME', - 'email' => 'YOUR_EMAIL', + 'email' => $your_email, ], ], ], diff --git a/examples/transmissions/create_transmission_with_attachment.php b/examples/transmissions/create_transmission_with_attachment.php index 74c1822..b97dd1d 100644 --- a/examples/transmissions/create_transmission_with_attachment.php +++ b/examples/transmissions/create_transmission_with_attachment.php @@ -10,18 +10,23 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); $filePath = dirname(__FILE__).'/'; $fileName = 'sparkpost.png'; $fileType = mime_content_type($filePath.$fileName); $fileData = base64_encode(file_get_contents($filePath.$fileName)); +// put your own sending domain and test recipient address here +$sending_domain = "steve2-test.trymsys.net"; +$your_email = "bob@sink.sparkpostmail.com"; + $promise = $sparky->transmissions->post([ 'content' => [ 'from' => [ 'name' => 'SparkPost Team', - 'email' => 'from@sparkpostbox.com', + 'email' => "from@$sending_domain", ], 'subject' => 'Mailing With Attachment From PHP', 'html' => '

Congratulations, {{name}}!

You just sent an email with an attachment!

', @@ -39,7 +44,7 @@ [ 'address' => [ 'name' => 'YOUR_NAME', - 'email' => 'YOUR_EMAIL', + 'email' => $your_email, ], ], ], diff --git a/examples/transmissions/create_transmission_with_cc_and_bcc.php b/examples/transmissions/create_transmission_with_cc_and_bcc.php index de65b29..c49ab0c 100644 --- a/examples/transmissions/create_transmission_with_cc_and_bcc.php +++ b/examples/transmissions/create_transmission_with_cc_and_bcc.php @@ -10,13 +10,20 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); + +// put your own sending domain and test recipient address here +$sending_domain = "steve2-test.trymsys.net"; +$your_email = "bob@sink.sparkpostmail.com"; +$your_cc = "alice@sink.sparkpostmail.com"; +$your_bcc = "charles@sink.sparkpostmail.com"; $promise = $sparky->transmissions->post([ 'content' => [ 'from' => [ 'name' => 'SparkPost Team', - 'email' => 'from@sparkpostbox.com', + 'email' => "from@$sending_domain", ], 'subject' => 'Mailing With CC and BCC From PHP', 'html' => '

Congratulations, {{name}}!

You just sent your very first mailing with CC and BCC recipients!

', @@ -27,7 +34,7 @@ [ 'address' => [ 'name' => 'YOUR_NAME', - 'email' => 'YOUR_EMAIL', + 'email' => $your_email, ], ], ], @@ -35,7 +42,7 @@ [ 'address' => [ 'name' => 'ANOTHER_NAME', - 'email' => 'ANOTHER_EMAIL', + 'email' => $your_cc, ], ], ], @@ -43,7 +50,7 @@ [ 'address' => [ 'name' => 'AND_ANOTHER_NAME', - 'email' => 'AND_ANOTHER_EMAIL', + 'email' => $your_bcc, ], ], ], diff --git a/examples/transmissions/create_transmission_with_recipient_list.php b/examples/transmissions/create_transmission_with_recipient_list.php index b8fa75f..b95a994 100644 --- a/examples/transmissions/create_transmission_with_recipient_list.php +++ b/examples/transmissions/create_transmission_with_recipient_list.php @@ -10,20 +10,27 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); + +// put your own sending domain and test recipient address here +$sending_domain = "steve2-test.trymsys.net"; + +// The ID of a list in your SparkPost account +$my_list = "mylist1"; $promise = $sparky->transmissions->post([ 'content' => [ 'from' => [ 'name' => 'SparkPost Team', - 'email' => 'from@sparkpostbox.com', + 'email' => "from@$sending_domain", ], 'subject' => 'Mailing With Recipient List From PHP', 'html' => '

Congratulations, {{name}}!

You just sent an email to everyone on your recipient list!

', 'text' => 'Congratulations, {{name}}! You just sent an email to everyone on your recipient list!', ], 'substitution_data' => ['name' => 'YOUR_FIRST_NAME'], - 'recipients' => ['list_id' => 'RECIPIENT_LIST_ID'], + 'recipients' => ['list_id' => $my_list], ]); try { diff --git a/examples/transmissions/create_transmission_with_template.php b/examples/transmissions/create_transmission_with_template.php index 7bb9020..66de0b9 100644 --- a/examples/transmissions/create_transmission_with_template.php +++ b/examples/transmissions/create_transmission_with_template.php @@ -10,16 +10,23 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); + +// put your own sending domain and test recipient address here +$sending_domain = "steve2-test.trymsys.net"; +$your_email = "bob@sink.sparkpostmail.com"; + +$template_id = "PHP-example-template"; $promise = $sparky->transmissions->post([ - 'content' => ['template_id' => 'TEMPLATE_ID'], + 'content' => ['template_id' => $template_id], 'substitution_data' => ['name' => 'YOUR_FIRST_NAME'], 'recipients' => [ [ 'address' => [ 'name' => 'YOUR_NAME', - 'email' => 'YOUR_EMAIL', + 'email' => $your_email, ], ], ], diff --git a/examples/transmissions/delete_transmission.php b/examples/transmissions/delete_transmission.php index f62766c..e7fb65a 100644 --- a/examples/transmissions/delete_transmission.php +++ b/examples/transmissions/delete_transmission.php @@ -10,9 +10,13 @@ $httpClient = new GuzzleAdapter(new Client()); -$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]); +// In these examples, fetch API key from environment variable +$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]); -$promise = $sparky->transmissions->delete('TRANSMISSION_ID'); +// Delete *scheduled* transmissions (only) by *campaign ID* (only) +// See https://developers.sparkpost.com/api/transmissions/#transmissions-delete-delete-a-scheduled-transmission + +$promise = $sparky->transmissions->delete('?campaign_id=white_christmas'); try { $response = $promise->wait(); diff --git a/examples/transmissions/get_all_transmissions.php b/examples/transmissions/get_all_transmissions.php index 37646ba..c199188 100644 --- a/examples/transmissions/get_all_transmissions.php +++ b/examples/transmissions/get_all_transmissions.php @@ -1,24 +1,3 @@ "YOUR_API_KEY"]); - -$promise = $sparky->transmissions->get(); - -try { - $response = $promise->wait(); - echo $response->getStatusCode()."\n"; - print_r($response->getBody())."\n"; -} catch (\Exception $e) { - echo $e->getCode()."\n"; - echo $e->getMessage()."\n"; -} +// Feature has been deprecated +// See https://developers.sparkpost.com/api/transmissions/#transmissions-get-retrieve-a-scheduled-transmission \ No newline at end of file diff --git a/examples/transmissions/get_transmission.php b/examples/transmissions/get_transmission.php index f62078f..c199188 100644 --- a/examples/transmissions/get_transmission.php +++ b/examples/transmissions/get_transmission.php @@ -1,24 +1,3 @@ "YOUR_API_KEY"]); - -$promise = $sparky->transmissions->get('TRANSMISSION_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"; -} +// Feature has been deprecated +// See https://developers.sparkpost.com/api/transmissions/#transmissions-get-retrieve-a-scheduled-transmission \ No newline at end of file