Skip to content

Instantly share code, notes, and snippets.

@DeviaVir
Created September 11, 2012 22:30
Show Gist options
  • Save DeviaVir/3702633 to your computer and use it in GitHub Desktop.
Save DeviaVir/3702633 to your computer and use it in GitHub Desktop.
A PHP library for the lessneglect.com API
<?PHP
Class LN {
// API credentials
private static $api_key = '';
private static $api_secret = '';
// Production (no sandboxed version available yet)
private static $api_url = 'https://lessneglect.com/api/v2/';
// Show debug messages?
private static $debug = 0;
public static function event( $data ) {
/*
* This is an example for a simple event, for other events check out http://lessneglect.com/api#events
* $data should be:
* Array(
* 'person[name]' => 'Christopher Gooley',
* 'person[email]' => 'gooley@lessneglect.com',
* 'event[name]' => 'tested_api',
* 'event[klass]' => 'actionevent'
* )
*/
$url = self::$api_url . 'events';
// Url-ify the data for the POST
foreach( $data as $key => $value ) {
$data_string .= $key.'='.HTML_Entity_Decode( $value, ENT_QUOTES, 'UTF-8' ).'&';
}
rtrim($data_string, '&');
if( self::$debug ) {
print '<pre>';
print_r( $data );
print '</pre>';
print '<pre>';
print_r( $data_string );
print '</pre>';
}
// Open connection
$ch = curl_init();
// Set the url, don't echo the response, set user login, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_USERPWD, self::$api_secret . ":" . self::$api_key );
curl_setopt( $ch, CURLOPT_POST, count( $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
// Execute post
$result = curl_exec( $ch );
if( self::$debug ) {
print '<pre>';
print_r( $result );
print '</pre>';
}
}
public static function person( $data ) {
/*
* see http://lessneglect.com/api#people for more info
* $data should be:
* Array(
* 'name' => 'Christopher Gooley',
* 'email' => 'gooley@lessneglect.com',
* 'external_identifier' => '12231',
* 'properties[is_paying]' => '1',
* 'properties[created_at]' => '1347060566'
* )
*/
$url = self::$api_url . 'people';
// Url-ify the data for the POST
foreach( $data as $key => $value ) {
$data_string .= $key.'='.$value.'&';
}
rtrim($data_string, '&');
if( self::$debug ) {
print '<pre>';
print_r( $data );
print '</pre>';
print '<pre>';
print_r( $data_string );
print '</pre>';
}
// Open connection
$ch = curl_init();
// Set the url, don't echo the response, set user login, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_USERPWD, self::$api_secret . ":" . self::$api_key );
curl_setopt( $ch, CURLOPT_POST, count( $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
// Execute post
$result = curl_exec( $ch );
if( self::$debug ) {
print '<pre>';
print_r( $result );
print '</pre>';
}
}
public static function message( $email = '', $subject = '', $body = '' ) {
$data = Array(
"person[email]" => ( $email ? $email : User::$data[ 'email' ] ),
"event[subject]" => HTML_Entity_Decode( $subject, ENT_QUOTES, 'UTF-8' ),
"event[body]" => HTML_Entity_Decode( $body, ENT_QUOTES, 'UTF-8' ),
"event[klass]" => 'message'
);
$url = self::$api_url . 'events';
// Url-ify the data for the POST
foreach( $data as $key => $value ) {
$data_string .= $key.'='.$value.'&';
}
rtrim($data_string, '&');
if( self::$debug ) {
print '<pre>';
print_r( $data );
print '</pre>';
print '<pre>';
print_r( $data_string );
print '</pre>';
}
// Open connection
$ch = curl_init();
// Set the url, don't echo the response, set user login, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_USERPWD, self::$api_secret . ":" . self::$api_key );
curl_setopt( $ch, CURLOPT_POST, count( $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
// Execute post
$result = curl_exec( $ch );
if( self::$debug ) {
print '<pre>';
print_r( $result );
print '</pre>';
}
}
}
?>
@azcoov
Copy link

azcoov commented Oct 31, 2012

The curl_setopt( $ch, CURLOPT_USERPWD, self::$api_secret . ":" . self::$api_key ); lines need to be changed to curl_setopt( $ch, CURLOPT_USERPWD, self::$api_key . ":" . self::$api_secret );

aka, key first, then secret.

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