Skip to content

Commit 3408642

Browse files
author
tuck1s
committed
No longer need formfeed replacement. README work.
1 parent fe98775 commit 3408642

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

README.md

+58-37
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ curl -sS https://getcomposer.org/installer | php
2626
Sparkpost requires php-http client (see [Setting up a Request Adapter](#setting-up-a-request-adapter)). There are several [providers](https://packagist.org/providers/php-http/client-implementation) available. If you were using guzzle6 your install might look like this.
2727

2828
```
29-
composer require guzzlehttp/guzzle
30-
composer require php-http/guzzle6-adapter
29+
composer require php-http/guzzle6-adapter "^1.1"
30+
composer require guzzlehttp/guzzle "^6.0"
3131
```
3232

3333
Next, run the Composer command to install the SparkPost PHP Library:
@@ -43,7 +43,18 @@ require 'vendor/autoload.php';
4343
use SparkPost\SparkPost;
4444
```
4545

46-
**Note:** Without composer the costs outweight the benefits of using the PHP client library. A simple function like the one in [issue #164](https://github.com/SparkPost/php-sparkpost/issues/164#issuecomment-289888237) wraps the SparkPost API and makes it easy to use the API without resolving the composer dependencies.
46+
**Note:** Without composer the costs outweigh the benefits of using the PHP client library. A simple function like the one in [issue #164](https://github.com/SparkPost/php-sparkpost/issues/164#issuecomment-289888237) wraps the SparkPost API and makes it easy to use the API without resolving the composer dependencies.
47+
48+
## Running with IDEs
49+
50+
When running with `xdebug` under an IDE such as VS Code, you may see an exception is thrown in file `vendor/php-http/discovery/src/Strategy/PuliBetaStrategy.php`:
51+
52+
```
53+
Exception has occurred.
54+
Http\Discovery\Exception\PuliUnavailableException: Puli Factory is not available
55+
```
56+
57+
[This is usual](http://docs.php-http.org/en/latest/discovery.html#puli-factory-is-not-available). Puli is not required to use the library. You can resume running after the exception.
4758

4859
## Setting up a Request Adapter
4960

@@ -179,44 +190,54 @@ use GuzzleHttp\Client;
179190
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
180191

181192
$httpClient = new GuzzleAdapter(new Client());
182-
$sparky = new SparkPost($httpClient, ['key'=>'YOUR_API_KEY']);
193+
// Good practice to not have API key literals in code - set an environment variable instead
194+
$sparky = new SparkPost($httpClient, ['key' => getenv('SPARKPOST_API_KEY')]);
195+
// For simple example, use synchronous model
196+
$sparky->setOptions(['async' => false]);
183197

184-
$promise = $sparky->transmissions->post([
185-
'content' => [
186-
'from' => [
187-
'name' => 'SparkPost Team',
188-
'email' => '[email protected]',
198+
try {
199+
$response = $sparky->transmissions->post([
200+
'content' => [
201+
'from' => [
202+
'name' => 'SparkPost Team',
203+
'email' => '[email protected]',
204+
],
205+
'subject' => 'First Mailing From PHP',
206+
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
207+
'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
189208
],
190-
'subject' => 'First Mailing From PHP',
191-
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
192-
'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
193-
],
194-
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
195-
'recipients' => [
196-
[
197-
'address' => [
198-
'name' => 'YOUR_NAME',
199-
'email' => 'YOUR_EMAIL',
209+
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
210+
'recipients' => [
211+
[
212+
'address' => [
213+
'name' => 'YOUR_NAME',
214+
'email' => 'YOUR_EMAIL',
215+
],
200216
],
201217
],
202-
],
203-
'cc' => [
204-
[
205-
'address' => [
206-
'name' => 'ANOTHER_NAME',
207-
'email' => 'ANOTHER_EMAIL',
218+
'cc' => [
219+
[
220+
'address' => [
221+
'name' => 'ANOTHER_NAME',
222+
'email' => 'ANOTHER_EMAIL',
223+
],
208224
],
209225
],
210-
],
211-
'bcc' => [
212-
[
213-
'address' => [
214-
'name' => 'AND_ANOTHER_NAME',
215-
'email' => 'AND_ANOTHER_EMAIL',
226+
'bcc' => [
227+
[
228+
'address' => [
229+
'name' => 'AND_ANOTHER_NAME',
230+
'email' => 'AND_ANOTHER_EMAIL',
231+
],
216232
],
217233
],
218-
],
219-
]);
234+
]);
235+
} catch (\Exception $error) {
236+
var_dump($error);
237+
}
238+
print($response->getStatusCode());
239+
$results = $response->getBody()['results'];
240+
var_dump($results);
220241
?>
221242
```
222243

@@ -250,8 +271,8 @@ The API calls either return a `SparkPostPromise` or `SparkPostResponse` dependin
250271
```php
251272
$sparky->setOptions(['async' => false]);
252273
try {
253-
$response = $sparky->transmissions->get();
254-
274+
$response = $sparky->transmissions->get(); //TODO: Change this. Transmissions no longer supports GET call
275+
255276
echo $response->getStatusCode()."\n";
256277
print_r($response->getBody())."\n";
257278
}
@@ -265,7 +286,7 @@ catch (\Exception $e) {
265286
Asynchronous an be handled in two ways: by passing callbacks or waiting for the promise to be fulfilled. Waiting acts like synchronous request.
266287
##### Wait (Synchronous)
267288
```php
268-
$promise = $sparky->transmissions->get();
289+
$promise = $sparky->transmissions->get(); //TODO: Change this. Transmissions no longer supports GET call
269290

270291
try {
271292
$response = $promise->wait();
@@ -281,7 +302,7 @@ echo "I will print out after the promise is fulfilled";
281302

282303
##### Then (Asynchronous)
283304
```php
284-
$promise = $sparky->transmissions->get();
305+
$promise = $sparky->transmissions->get(); //TODO: Change this. Transmissions no longer supports GET call
285306

286307
$promise->then(
287308
// Success callback

lib/SparkPost/SparkPost.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,8 @@ public function buildRequestValues($method, $uri, $payload, $headers)
184184
$url = $this->getUrl($uri, $params);
185185
$headers = $this->getHttpHeaders($headers);
186186

187-
// Sparkpost API will not tolerate form feed in JSON.
188-
$jsonReplace = [
189-
'\f' => '',
190-
];
191-
$body = strtr(json_encode($body), $jsonReplace);
192-
187+
// old form-feed workaround now removed
188+
$body = json_encode($body);
193189
return [
194190
'method' => $method,
195191
'url' => $url,

0 commit comments

Comments
 (0)