Last active
August 29, 2015 14:01
-
-
Save salsalabs/e24c2466496860975e8a to your computer and use it in GitHub Desktop.
Sample of using the Salsa API in PHP. Note that cookies must be passed to every call after authentication!
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 | |
// See https://salsasupport.zendesk.com/entries/23514381-Definitions-for-common-terms | |
// to find out to retrieve the API URL in $url. | |
$url = "https://API_URL/api"; | |
$username = ""; # Campaign Manager username goes here | |
$password = ""; # Campaign Manager password goes here | |
// Method #1 for building post objects to send to Salsa | |
$authfields["email"] = $username; | |
$authfields["password"] = $password; | |
// Method #2 for building post objects to send to Salsa | |
// Note: unless you have a small number of supporters, this will fail. | |
// try removing the 'groupBy' => 'Date_Created' statement. | |
$fields = array('object' => 'supporter', 'groupBy' => 'Date_Created'); | |
// Initialize cURL connection | |
// * See http://us3.php.net/manual/en/book.curl.php for more information | |
// on the cURL capability in PHP. | |
$ch = curl_init(); | |
// Set basic connection parameters | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 100); | |
// Set parameters to maintain cookies across sessions | |
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies_file'); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies_file'); | |
//Execute connection to authenticate to Salsa | |
//* Example: | |
// https://sandbox.salsalabs.com/api/authenticate.sjs?email=whatever&password=whatever | |
curl_setopt($ch, CURLOPT_URL, "$url/authenticate.sjs"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($authfields)); | |
$auth = curl_exec($ch); | |
// Execute query to return data back to the User/Application | |
// * Example: | |
// https://sandbox.salsalabs.com/api/getCounts.sjs?object=supporter&groupBy=Date_Created | |
// | |
curl_setopt($ch, CURLOPT_URL, "$url/getCounts.sjs"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
$count = curl_exec($ch); | |
//Close the connection | |
curl_close($ch); | |
// Populate results from salsa into SimpleXML object | |
// * See http://php.net/manual/en/book.simplexml.php for more | |
// information on SimpleXML objects in PHP | |
$response = simplexml_load_string($count); | |
// Uncomment the next statement to see the contents of $response. | |
// * See http://us3.php.net/manual/en/function.print-r.php for more info. | |
// print_r($response); | |
// Parse SimpleXML object and display Date_Created for each entry to the screen. | |
foreach($response->supporter->count AS $data){ | |
echo "VALUE -> DATA: "; | |
echo $data->Date_Created . " | |
<br/> | |
"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment