Skip to content

Instantly share code, notes, and snippets.

@jdennes
Last active December 13, 2015 20:58
Show Gist options
  • Save jdennes/4973318 to your computer and use it in GitHub Desktop.
Save jdennes/4973318 to your computer and use it in GitHub Desktop.
A PHP application to demonstrate authenticating with the Campaign Monitor API using OAuth. Uses the createsend-php library as well as the Slim PHP micro framework (but could easily be adapted for use with any PHP framework).
<?php
# 1. Ensure you have Slim installed and in your include path.
# 2. Ensure you have createsend-php installed and in your include path.
# 3. Set CREATESEND_CLIENT_ID, CREATESEND_CLIENT_SECRET,
# and CREATESEND_REDIRECT_URI environment variables for
# your registered OAuth application.
require 'createsend/csrest_general.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/', function () use ($app) {
$authorize_url = CS_REST_General::authorize_url(
getenv('CREATESEND_CLIENT_ID'),
getenv('CREATESEND_REDIRECT_URI'),
'ViewReports,CreateCampaigns,SendCampaigns');
$app->redirect($authorize_url);
});
$app->get('/exchange_token', function () use ($app) {
$code = $app->request()->params('code');
$result = CS_REST_General::exchange_token(
getenv('CREATESEND_CLIENT_ID'),
getenv('CREATESEND_CLIENT_SECRET'),
getenv('CREATESEND_REDIRECT_URI'),
$code
);
if ($result->was_successful()) {
$access_token = $result->response->access_token;
$expires_in = $result->response->expires_in;
$refresh_token = $result->response->refresh_token;
$response = "<pre>";
$response .= "Your user is successfully authenticated. Here are the details you need:<br/><br/>";
$response .= "access token: ".$access_token."<br/>";
$response .= "refresh token: ".$refresh_token."<br/>";
$response .= "expires in: ".$expires_in."<br/>";
$response .= "<br/><br/>";
$auth = array(
'access_token' => $access_token,
'refresh_token' => $refresh_token
);
$cs = new CS_REST_General($auth);
$clients = $cs->get_clients()->response;
$response .= "We've made an API call too. Here are your clients:<br/><br/>";
$response .= var_export($clients, true);
$response .= "</pre>";
} else {
$response = 'An error occurred:<br/>';
$response .= $result->response->error.': '.$result->response->error_description."<br/>";
}
echo $response;
});
$app->run();
@lovekairon
Copy link

where include that file in the wordpress project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment