Skip to content

Commit 2c7f60d

Browse files
author
SteveT
authored
Merge pull request #201 from SparkPost/update-examples
Update examples
2 parents eeb6ba9 + 1a795f0 commit 2c7f60d

22 files changed

+218
-115
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ test/output/
77
/composer.phar
88
composer.lock
99
test.php
10+
.php-version
11+
.gitignore
12+
.phpunit.result.cache
13+
.DS_Store
14+
.gitignore
15+
.vscode/launch.json

CHANGELOG.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased][unreleased]
66

7-
## [2.2.1] - 2021-03-08
7+
## [2.3.0] - 2021-03-16
8+
- [#201](https://github.com/SparkPost/php-sparkpost/pull/201) Update examples, README
9+
- [#200](https://github.com/SparkPost/php-sparkpost/pull/200) PHP 8 support
810

9-
- [#198](https://github.com/SparkPost/php-sparkpost/pull/198)
10-
- [#191](https://github.com/SparkPost/php-sparkpost/pull/191)
11+
## [2.2.1] - 2021-03-08
12+
- [#198](https://github.com/SparkPost/php-sparkpost/pull/198) Address #197: No longer need formfeed replacement. README work.
13+
- [#191](https://github.com/SparkPost/php-sparkpost/pull/191) Updating License
1114

1215
## [2.2.0] - 2019-06-04
13-
- [#187](https://github.com/SparkPost/php-sparkpost/pull/169) Updated composer.json
16+
- [#187](https://github.com/SparkPost/php-sparkpost/pull/187) Updated composer.json
1417
- [#169](https://github.com/SparkPost/php-sparkpost/pull/169) Optional automatic retry on 5xx
1518
- [#166](https://github.com/SparkPost/php-sparkpost/pull/166/files) Quick fix for using the API without composer
1619
- [#149](https://github.com/SparkPost/php-sparkpost/pull/149) Setters should return current object

CONTRIBUTING.md

+36-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,41 @@ curl -sS https://getcomposer.org/installer | php
2424

2525
Add composer install directory to $PATH `~/.composer/vendor/bin/`
2626

27-
#### Install PHPUnit for Testing
28-
```
29-
composer global require "phpunit/phpunit=4.8.*"
27+
### phpenv
28+
29+
[phpenv](https://github.com/phpenv/phpenv-installer) is useful for testing locally across different PHP versions.
30+
31+
### Developing your app against a local version of the SparkPost library
32+
33+
If you're working on the library and your app together, you can tell Composer to get `php-sparkpost` from a local path. With a directory structure such as:
34+
35+
home
36+
- php-sparkpost
37+
- my-app
38+
- composer.json
39+
- .. etc
40+
41+
Use the following for `my-app/composer.json`:
42+
```json
43+
{
44+
"name": "sparkpost/php_simple_email_send",
45+
"description": "a small test program to send an email",
46+
"repositories": [
47+
{
48+
"type": "path",
49+
"url": "../php-sparkpost"
50+
}
51+
],
52+
"require": {
53+
"php-http/guzzle6-adapter": "^1.1",
54+
"guzzlehttp/guzzle": "^6.0",
55+
"sparkpost/sparkpost": "dev-master"
56+
}
57+
}
3058
```
3159

60+
61+
### Memory
3262
We recommend increasing PHP’s memory limit, by default it uses 128MB. We ran into some issues during local development without doing so. You can do this by editing your php.ini file and modifying `memory_limit`. We set ours to `memory_limit = 1024M`.
3363

3464
#### Install XDebug for code coverage generation
@@ -68,6 +98,7 @@ Once you are setup for local development:
6898

6999
## Releasing
70100

71-
* Update version information in composer.json during development.
72-
* Once its been merged down, create a release tag in git.
101+
* Update version in the [library](https://github.com/SparkPost/php-sparkpost/blob/eeb6ba971584fcc4c12fd69247c6b24df7827af5/lib/SparkPost/SparkPost.php#L16) during development. This is used in the `user_agent` of your requests.
102+
103+
* Once it's been merged down, create a release tag in git.
73104
* Composer will automatically pickup the new tag and present it as a release.

README.md

+38-21
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ $sparky = new SparkPost($httpClient, ['key'=>'YOUR_API_KEY']);
143143
* `uri`
144144
* Required: Yes
145145
* Type: `String`
146-
* The URI to recieve the request
146+
* The URI to receive the request
147147
* `payload`
148148
* Required: No
149149
* Type: `Array`
@@ -170,24 +170,18 @@ Sends an asynchronous request to the SparkPost API and returns a `SparkPostPromi
170170
* Type: `Array`
171171
* See constructor
172172

173-
174173
## Endpoints
175174
### transmissions
176-
* **get([transmissionID] [, payload])**
177-
* `transmissionID` - see `uri` request options
178-
* `payload` - see request options
179175
* **post(payload)**
180176
* `payload` - see request options
181177
* `payload.cc`
182178
* Required: No
183179
* Type: `Array`
184-
* Recipients to recieve a carbon copy of the transmission
180+
* Recipients to receive a carbon copy of the transmission
185181
* `payload.bcc`
186182
* Required: No
187183
* Type: `Array`
188-
* Recipients to descreetly recieve a carbon copy of the transmission
189-
* **delete(transmissionID)**
190-
* `transmissionID` - see `uri` request options
184+
* Recipients to discreetly receive a carbon copy of the transmission
191185

192186
## Examples
193187

@@ -202,9 +196,8 @@ use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
202196

203197
$httpClient = new GuzzleAdapter(new Client());
204198
// Good practice to not have API key literals in code - set an environment variable instead
205-
$sparky = new SparkPost($httpClient, ['key' => getenv('SPARKPOST_API_KEY')]);
206199
// For simple example, use synchronous model
207-
$sparky->setOptions(['async' => false]);
200+
$sparky = new SparkPost($httpClient, ['key' => getenv('SPARKPOST_API_KEY'), 'async' => false]);
208201

209202
try {
210203
$response = $sparky->transmissions->post([
@@ -252,7 +245,28 @@ var_dump($results);
252245
?>
253246
```
254247

248+
More examples [here](./examples/):
249+
### [Transmissions](./examples/transmissions/)
250+
- Create with attachment
251+
- Create with recipient list
252+
- Create with cc and bcc
253+
- Create with template
254+
- Create
255+
- Delete (scheduled transmission by campaign_id *only*)
256+
257+
### [Templates](./examples/templates/)
258+
- Create
259+
- Get
260+
- Get (list) all
261+
- Update
262+
- Delete
263+
264+
### [Message Events](./examples/message-events/)
265+
- get
266+
- get (with retry logic)
267+
255268
### Send An API Call Using The Base Request Function
269+
256270
We provide a base request function to access any of our API resources.
257271
```php
258272
<?php
@@ -263,17 +277,19 @@ use GuzzleHttp\Client;
263277
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
264278

265279
$httpClient = new GuzzleAdapter(new Client());
266-
$sparky = new SparkPost($httpClient, ['key'=>'YOUR_API_KEY']);
280+
$sparky = new SparkPost($httpClient, [
281+
'key' => getenv('SPARKPOST_API_KEY'),
282+
'async' => false]);
267283

268-
$promise = $sparky->request('GET', 'metrics/ip-pools', [
269-
'from' => '2014-12-01T09:00',
270-
'to' => '2015-12-01T08:00',
271-
'timezone' => 'America/New_York',
272-
'limit' => '5',
273-
]);
284+
$webhookId = 'afd20f50-865a-11eb-ac38-6d7965d56459';
285+
$response = $sparky->request('DELETE', 'webhooks/' . $webhookId);
286+
print($response->getStatusCode());
274287
?>
275288
```
276289

290+
> Be sure to not have a leading `/` in your resource URI.
291+
292+
For complete list of resources, refer to [API documentation](https://developers.sparkpost.com/api/).
277293

278294
## Handling Responses
279295
The API calls either return a `SparkPostPromise` or `SparkPostResponse` depending on if `async` is `true` or `false`
@@ -282,7 +298,7 @@ The API calls either return a `SparkPostPromise` or `SparkPostResponse` dependin
282298
```php
283299
$sparky->setOptions(['async' => false]);
284300
try {
285-
$response = $sparky->transmissions->get(); //TODO: Change this. Transmissions no longer supports GET call
301+
$response = ... // YOUR API CALL GOES HERE
286302

287303
echo $response->getStatusCode()."\n";
288304
print_r($response->getBody())."\n";
@@ -297,7 +313,8 @@ catch (\Exception $e) {
297313
Asynchronous an be handled in two ways: by passing callbacks or waiting for the promise to be fulfilled. Waiting acts like synchronous request.
298314
##### Wait (Synchronous)
299315
```php
300-
$promise = $sparky->transmissions->get(); //TODO: Change this. Transmissions no longer supports GET call
316+
317+
$promise = ... // YOUR API CALL GOES HERE
301318

302319
try {
303320
$response = $promise->wait();
@@ -313,7 +330,7 @@ echo "I will print out after the promise is fulfilled";
313330

314331
##### Then (Asynchronous)
315332
```php
316-
$promise = $sparky->transmissions->get(); //TODO: Change this. Transmissions no longer supports GET call
333+
$promise = ... // YOUR API CALL GOES HERE
317334

318335
$promise->then(
319336
// Success callback

examples/debug/index.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* configure options in example-options.json
1515
*/
1616
$sparky = new SparkPost($httpClient, [
17-
"key" => "YOUR_API_KEY",
18-
// This will expose your API KEY - do not use this in production.
17+
"key" => getenv('SPARKPOST_API_KEY'),
18+
// fetch API KEY from environment variable
1919
"debug" => true
2020
]);
2121

examples/message-events/get_message_events.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY",]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
1415

15-
$promise = $sparky->request('GET', 'message-events', [
16+
// New endpoint - https://developers.sparkpost.com/api/events/
17+
$promise = $sparky->request('GET', 'events/message', [
1618
'campaign_ids' => 'CAMPAIGN_ID',
1719
]);
1820

examples/message-events/get_message_events_with_retry_logic.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY", "retries" => 3]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY'), "retries" => 3]);
1415

15-
$promise = $sparky->request('GET', 'message-events', [
16+
// New endpoint - https://developers.sparkpost.com/api/events/
17+
$promise = $sparky->request('GET', 'events/message', [
1618
'campaign_ids' => 'CAMPAIGN_ID',
1719
]);
1820

examples/templates/create_template.php

+40-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,50 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
15+
16+
$template_name = "PHP example template";
17+
$template_id = "PHP-example-template";
18+
19+
// put your own sending domain here
20+
$sending_domain = "steve2-test.trymsys.net";
21+
22+
// Valid short template content examples
23+
$plain_text = 'Write your text message part here.';
24+
25+
$html = <<<HTML
26+
<!DOCTYPE html>
27+
<html lang="en">
28+
<body>
29+
<p><strong>Write your HTML message part here</strong></p>
30+
</body>
31+
</html>
32+
HTML;
33+
34+
$amp_html = <<<HTML
35+
<!doctype html>
36+
<html ⚡4email>
37+
<head>
38+
<meta charset="utf-8">
39+
<style amp4email-boilerplate>body{visibility:hidden}</style>
40+
<script async src="https://cdn.ampproject.org/v0.js"></script>
41+
</head>
42+
<body>
43+
Hello World! Let's get started using AMP HTML together!
44+
</body>
45+
</html>
46+
HTML;
1447

1548
$promise = $sparky->request('POST', 'templates', [
16-
'name' => 'PHP example template',
49+
'name' => $template_name,
50+
'id' => $template_id,
1751
'content' => [
18-
'from' => 'from@YOUR_DOMAIN',
52+
'from' => "from@$sending_domain",
1953
'subject' => 'Your Subject',
20-
'html' => '<b>Write your message here.</b>',
54+
'text' => $plain_text,
55+
'html' => $html,
56+
'amp_html' => $amp_html,
2157
],
2258
]);
2359

examples/templates/delete_template.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
1415

15-
$promise = $sparky->request('DELETE', 'templates/TEMPLATE_ID');
16+
$template_id = "PHP-example-template";
17+
18+
$promise = $sparky->request('DELETE', "templates/$template_id");
1619

1720
try {
1821
$response = $promise->wait();

examples/templates/get_all_templates.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
1415

1516
$promise = $sparky->request('GET', 'templates');
1617

examples/templates/get_template.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
1415

15-
$promise = $sparky->request('GET', 'templates/TEMPLATE_ID?draft=true');
16+
$template_id = "PHP-example-template";
17+
18+
$promise = $sparky->request('GET', "templates/$template_id?draft=true");
1619

1720
try {
1821
$response = $promise->wait();

examples/templates/preview_template.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
1415

15-
$promise = $sparky->request('POST', 'templates/TEMPLATE_ID/preview?draft=true', [
16+
$template_id = "PHP-example-template";
17+
18+
$promise = $sparky->request('POST', "templates/$template_id/preview?draft=true", [
1619
'substitution_data' => [
1720
'some_key' => 'some_value',
1821
],

examples/templates/update_template.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
$httpClient = new GuzzleAdapter(new Client());
1212

13-
$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
13+
// In these examples, fetch API key from environment variable
14+
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);
1415

15-
$promise = $sparky->request('PUT', 'templates/TEMPLATE_ID', [
16+
$template_id = "PHP-example-template";
17+
18+
$promise = $sparky->request('PUT', "templates/$template_id", [
1619
'options' => [
1720
'open_tracking' => true,
1821
],

0 commit comments

Comments
 (0)