Skip to content

Instantly share code, notes, and snippets.

@vindia
Created November 14, 2011 14:42
Show Gist options
  • Save vindia/1364079 to your computer and use it in GitHub Desktop.
Save vindia/1364079 to your computer and use it in GitHub Desktop.
Simple Launchrock API class
<?php
/**
* Simple class for the Launchrock API, see http://support.launchrock.com/customer/portal/articles/74480-using-the-api for more information
*/
class LaunchRockApi {
var $apiKey = ''; # Your API key, get it from Launchrock.com
var $urlPrefix = ''; # Your referral url, minus the actual referral id, e.g. 'example.com/?refid='
var $lrDomain = ''; # The domain of your site as you registered it at Launchrock, e.g. 'example.com'
public function submit($email, $referralid = '') {
$values = array(
'email' => $email,
'referralid' => $referralid,
'lrDomain' => $this->lrDomain,
'urlPrefix' => $this->urlPrefix,
'apiKey' => $this->apiKey,
);
$postString = $this->buildPostString($values);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://208.72.154.212/widgetapi.0.2.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, count($values));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$data = curl_exec($ch);
# Launchrock returns json surrounded by parentheses and some whitespace,
# so we have to trim those out to keep valid json
$data = trim(trim($data), '()');
return json_decode($data);
}
private function buildPostString($values) {
$postString = '';
foreach($values as $key => $value) {
$postString .= $key . '=' . urlencode($value) . '&';
}
return trim($postString, '&');
}
}
# Example usage
$launchRockApi = new LaunchRockApi();
$response = $launchRockApi->submit('[email protected]', 'abc1234');
var_dump($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment