Skip to content

Instantly share code, notes, and snippets.

@fiftyandfifty
Created December 10, 2014 23:52
Show Gist options
  • Save fiftyandfifty/88d415b0392a1820613b to your computer and use it in GitHub Desktop.
Save fiftyandfifty/88d415b0392a1820613b to your computer and use it in GitHub Desktop.
This is a simple example script for testing valid calls to the Donately API in PHP
<?php
/*
* This is a simple example script for testing valid calls to the Donately API in PHP
* Edit this script and test locally
*
* Note: if you are testing on a remote server don't put your password in this file,
* but use the auth token instead - then renew your token when you're done testing
*
* Bryan Shanaver CTO - Donately / Fiftyandfifty
* bryan@fiftyandfifty.org
*/
$email = 'email@example.com';
$password = 'xxx-xxx';
$url = 'https://www.dntly.com/api/v1/sessions.json';
$fields = array(
'email' => urlencode($email),
'password' => urlencode($password)
);
$fields_string = '';
foreach($fields as $key => $value){ $fields_string .= $key . '=' . $value . '&'; }
rtrim($fields_string, '&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, count($fields)); //number of fields we are posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string); //the actual posted fields
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_POST, TRUE);
$response1 = curl_exec($curl);
if ($response1 === false){ json_encode('Curl error: ' . curl_error($curl)); die(); }
curl_close($curl);
print "<p>Authentication Call Result:</p>";
print_r($response1);
$subdomain = 'demo';
//$token = 'XXXXXXXXXXXXXX';
$response1_object = json_decode( $response1 );
$token = $response1_object->token;
$url2 = "https://{$subdomain}.dntly.com/api/v1/admin/donations.json";
$fields = array(
'count' => urlencode('10'),
'order_by' => urlencode('amount_in_cents')
);
$fields_string = '';
foreach($fields as $key => $value){ $fields_string .= $key . '=' . $value . '&'; }
rtrim($fields_string, '&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url2 . '?' . $fields_string);
// try these if you get an SSL error
// curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Basic ' . base64_encode($token);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response2 = curl_exec($curl); if ($response2 === false){print_r('<pre>Curl error: ' . curl_error($curl) . "</pre>");}
curl_close($curl);
print "<p>Donations Call result:</p>";
print_r($response2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment