campaignmonitor / createsend-php
A php library which implements the complete functionality of the Campaign Monitor API.
Installs: 4 702 347
Dependents: 33
Suggesters: 1
Security: 0
Stars: 285
Watchers: 39
Forks: 106
Open Issues: 37
Requires
- php: >=5.3.0
Requires (Dev)
- simpletest/simpletest: 1.2.0
- dev-master
- v7.1.0
- v7.0.0
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.2
- v6.0.1
- v6.0.0
- v5.1.3
- 5.1.2
- v5.0.2
- v5.0.1
- v5.0.0
- v4.1.1
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.0
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.2
- v2.3.1
- dev-UpdateSimpleTestPhp7
- dev-UpdateSimpleTest
- dev-Journey-API
- dev-consent-to-track
- dev-custom-e2e
This package is auto-updated.
Last update: 2024-11-28 10:05:13 UTC
README
A PHP library which implements the complete functionality of the Campaign Monitor API.
Installation
Composer
If you use Composer, you can run the following command from the root of your project:
composer require campaignmonitor/createsend-php
Or add campaignmonitor/createsend-php to your composer.json
file:
{ "require": { "campaignmonitor/createsend-php": "{version}" } }
Followed by running:
composer update
Manual Installation
Otherwise you can simply download the library and include it in your project.
After you have installed the library, simply include the relevant API class, as follows:
require_once 'csrest_campaigns.php'
Authenticating
The Campaign Monitor API supports authentication using either OAuth or an API key.
Using OAuth
Depending on the environment you are developing in, you may wish to use a PHP OAuth library to get access tokens for your users. If you don't use an OAuth library, you will need to get access tokens for your users by following the instructions included in the Campaign Monitor API documentation. This package provides functionality to help you do this, as described below. You may also wish to reference this example application, which is implemented using Slim but could easily be adapted for use with any PHP framework.
The first thing your application should do is redirect your user to the Campaign Monitor authorization URL where they will have the opportunity to approve your application to access their Campaign Monitor account. You can get this authorization URL by using the CS_REST_General::authorize_url()
method, like so:
require_once 'csrest_general.php'; $authorize_url = CS_REST_General::authorize_url( 'Client ID for your application', 'Redirect URI for your application', 'The permission level your application requires', 'Optional state data to be included' ); # Redirect your users to $authorize_url.
If your user approves your application, they will then be redirected to the redirect_uri
you specified, which will include a code
parameter, and optionally a state
parameter in the query string. Your application should implement a handler which can exchange the code passed to it for an access token, using CS_REST_General::exchange_token()
like so:
require_once 'csrest_general.php'; $result = CS_REST_General::exchange_token( 'Client ID for your application', 'Client Secret for your application', 'Redirect URI for your application', 'A unique code for your user' # Get the code parameter from the query string ); if ($result->was_successful()) { $access_token = $result->response->access_token; $expires_in = $result->response->expires_in; $refresh_token = $result->response->refresh_token; # Save $access_token, $expires_in, and $refresh_token. } else { echo 'An error occurred:\n'; echo $result->response->error.': '.$result->response->error_description."\n"; # Handle error... }
At this point you have an access token and refresh token for your user which you should store somewhere convenient so that your application can look up these values when your user wants to make future Campaign Monitor API calls.
Once you have an access token and refresh token for your user, you can authenticate and make further API calls like so:
require_once 'csrest_general.php'; $auth = array( 'access_token' => 'your access token', 'refresh_token' => 'your refresh_token'); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); var_dump($result->response);
All OAuth tokens have an expiry time, and can be renewed with a corresponding refresh token. If your access token expires when attempting to make an API call, you will receive an error response, so your code should handle this. Here's an example of how you could do this:
require_once 'csrest_general.php'; $auth = array( 'access_token' => 'your access token', 'refresh_token' => 'your refresh token' ); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); if (!$result->was_successful()) { # If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { list($new_access_token, $new_expires_in, $new_refresh_token) = $wrap->refresh_token(); # Save $new_access_token, $new_expires_in, and $new_refresh_token } # Make the call again $result = $wrap->get_clients(); } var_dump($result->response);
Using an API key
require_once 'csrest_general.php'; $auth = array('api_key' => 'your API key'); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); var_dump($result->response);
API Call Timeout
You can set your local API call timeout time in createsend-php\class\transport.php line 11, in the CS_REST_CALL_TIMEOUT variable. Currently the default is 120 secs.
Examples
Samples for creating or accessing all resources can be found in the samples directory. These samples can be used as the basis for your own application and provide an outline of the expected inputs for each API call.
Further documentation of the inputs and outputs of each call can be found in the documentation in each of the csrest_*.php files or simply by examining the var_dump results in each of the provided samples.
Contributing
Please check the guidelines for contributing to this repository.
Releasing
Please check the instructions for releasing this library.