Download the PHP package spontaneit/sparkpost without Composer
On this page you can find all versions of the php package spontaneit/sparkpost. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spontaneit/sparkpost
More information about spontaneit/sparkpost
Files in spontaneit/sparkpost
Package sparkpost
Short Description Client library for interfacing with the SparkPost API.
License MIT
Informations about the package sparkpost
It's a fork of sparkpost/sparkpost (https://github.com/SparkPost/php-sparkpost)
Sign up for a SparkPost account and visit our Developer Hub for even more content.
SparkPost PHP Library
The official PHP library for using the SparkPost REST API.
Before using this library, you must have a valid API Key. To get an API Key, please log in to your SparkPost account and generate one in the Settings page.
Installation
Please note: The composer package sparkpost/php-sparkpost
has been changed to sparkpost/sparkpost
starting with version 2.0.
The recommended way to install the SparkPost PHP Library is through composer.
Sparkpost requires php-http client (see providers available. If you were using guzzle6 your install might look like this.
Next, run the Composer command to install the SparkPost PHP Library:
After installing, you need to require Composer's autoloader:
Note: Without composer the costs outweigh the benefits of using the PHP client library. A simple function like the one in issue #164 wraps the SparkPost API and makes it easy to use the API without resolving the composer dependencies.
Running with IDEs
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
:
This is usual. Puli is not required to use the library. You can resume running after the exception.
You can prevent the exception, by setting the discovery strategies, prior to creating the adapter object:
Setting up a Request Adapter
Because of dependency collision, we have opted to use a request adapter rather than requiring a request library. This means that your application will need to pass in a request adapter to the constructor of the SparkPost Library. We use the HTTPlug in SparkPost. Please visit their repo for a list of supported clients and adapters. If you don't currently use a request library, you will need to require one and create a client from it and pass it along. The example below uses the GuzzleHttp Client Library.
A Client can be setup like so:
Initialization
new Sparkpost(httpClient, options)
httpClient
- Required: Yes
- HTTP client or adapter supported by HTTPlug
options
- Required: Yes
- Type:
String
orArray
- A valid Sparkpost API key or an array of options
options.key
- Required: Yes
- Type:
String
- A valid Sparkpost API key
options.host
- Required: No
- Type:
String
- Default:
api.sparkpost.com
options.protocol
- Required: No
- Type:
String
- Default:
https
options.port
- Required: No
- Type:
Number
- Default: 443
options.version
- Required: No
- Type:
String
- Default:
v1
options.async
- Required: No
- Type:
Boolean
- Default:
true
async
defines if therequest
function sends an asynchronous or synchronous request. If your client does not support async requests set this tofalse
options.retries
- Required: No
- Type:
Number
- Default:
0
retries
controls how many API call attempts the client makes after receiving a 5xx response
options.debug
- Required: No
- Type:
Boolean
- Default:
false
- If
debug
is true, then allSparkPostResponse
andSparkPostException
instances will return any array of the request values through the functiongetRequest
Methods
request(method, uri [, payload [, headers]])
method
- Required: Yes
- Type:
String
- HTTP method for request
uri
- Required: Yes
- Type:
String
- The URI to receive the request
payload
- Required: No
- Type:
Array
- If the method is
GET
the values are encoded into the URL. Otherwise, if the method isPOST
,PUT
, orDELETE
the payload is used for the request body.
headers
- Required: No
- Type:
Array
- Custom headers to be sent with the request.
syncRequest(method, uri [, payload [, headers]])
Sends a synchronous request to the SparkPost API and returns a SparkPostResponse
asyncRequest(method, uri [, payload [, headers]])
Sends an asynchronous request to the SparkPost API and returns a SparkPostPromise
setHttpClient(httpClient)
httpClient
- Required: Yes
- HTTP client or adapter supported by HTTPlug
setOptions(options)
options
- Required: Yes
- Type:
Array
- See constructor
Endpoints
transmissions
- post(payload)
payload
- see request optionspayload.cc
- Required: No
- Type:
Array
- Recipients to receive a carbon copy of the transmission
payload.bcc
- Required: No
- Type:
Array
- Recipients to discreetly receive a carbon copy of the transmission
Examples
Send An Email Using The Transmissions Endpoint
More examples here:
Transmissions
- Create with attachment
- Create with recipient list
- Create with cc and bcc
- Create with template
- Create
- Delete (scheduled transmission by campaign_id only)
Templates
- 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.
Be sure to not have a leading
/
in your resource URI.
For complete list of resources, refer to API documentation.
Handling Responses
The API calls either return a SparkPostPromise
or SparkPostResponse
depending on if async
is true
or false
Synchronous
Asynchronous
Asynchronous an be handled in two ways: by passing callbacks or waiting for the promise to be fulfilled. Waiting acts like synchronous request.
Wait (Synchronous)
Then (Asynchronous)
Handling Exceptions
An exception will be thrown in two cases: there is a problem with the request or the server returns a status code of 400
or higher.
SparkPostException
- getCode()
- Returns the response status code of
400
or higher
- Returns the response status code of
- getMessage()
- Returns the exception message
- getBody()
- If there is a response body it returns it as an
Array
. Otherwise it returnsnull
.
- If there is a response body it returns it as an
- getRequest()
- Returns an array with the request values
method
,url
,headers
,body
whendebug
istrue
- Returns an array with the request values
Contributing
See contributing.
All versions of sparkpost with dependencies
php-http/httplug Version ^1.0 || ^2.0
php-http/message Version ^1.0
php-http/client-implementation Version ^1.0
php-http/discovery Version ^1.0
php-http/message-factory Version ^1.1