-
-
Save azcoov/4068353 to your computer and use it in GitHub Desktop.
A PHP library for the lessneglect.com API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]' => '[email protected]', | |
* '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_key . ":" . self::$api_secret ); | |
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' => '[email protected]', | |
* '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_key . ":" . self::$api_secret ); | |
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_key . ":" . self::$api_secret ); | |
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>'; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment