-
-
Save joshuapekera/d5383042f0295de31f08 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Example_API { | |
private $api_url; | |
private $api_key; | |
private $password; | |
/** | |
* Class Constructor | |
* | |
* @access public | |
* @return null | |
*/ | |
public function __construct() | |
{ | |
$this->api_url = 'https://accountname.gathercontent.com/api/0.3/'; | |
$this->api_key = '64-characters-long-api-key'; | |
$this->password = 'x'; // leave it as 'x' | |
} | |
/** | |
* Function test_api | |
* | |
* Few test calls to GatherContent API | |
* | |
* @access public | |
* @return null | |
*/ | |
public function test_api() | |
{ | |
$query = $this->_curl('get_me'); | |
echo 'Me:<br>'; | |
echo 'Response code: '.$query['code'].'<br>'; | |
echo '<pre>'; | |
print_r(json_decode($query['response'])); | |
echo '</pre>'; | |
$query = $this->_curl('get_my_group'); | |
echo 'My group:<br>'; | |
echo 'Response code: '.$query['code'].'<br>'; | |
echo '<pre>'; | |
print_r(json_decode($query['response'])); | |
echo '</pre>'; | |
//non-existing group, will return an error 404 | |
$query = $this->_curl('get_group', array('id' => 123456789)); | |
echo 'Group 123456789:<br>'; | |
echo 'Response code: '.$query['code'].'<br>'; | |
echo '<pre>'; | |
print_r(json_decode($query['response'])); | |
echo '</pre>'; | |
exit(); | |
} | |
/** | |
* Function _curl | |
* | |
* Using cURL to access GatherContent API | |
* | |
* @access private | |
* @param string | |
* @param array | |
* @return array | |
*/ | |
private function _curl($command = '', $postfields = array()) | |
{ | |
$postfields = http_build_query($postfields); | |
$session = curl_init(); | |
curl_setopt($session, CURLOPT_URL, $this->api_url.$command); | |
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); | |
curl_setopt($session, CURLOPT_HEADER, false); | |
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/x-www-form-urlencoded')); | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($session, CURLOPT_USERPWD, $this->api_key . ":" . $this->password); | |
curl_setopt($session, CURLOPT_POST, true); | |
curl_setopt($session, CURLOPT_POSTFIELDS, $postfields); | |
if (substr($this->api_url, 0, 8) == 'https://') { | |
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, true); | |
} | |
$response = curl_exec($session); | |
$httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE); | |
curl_close($session); | |
return array( 'code' => $httpcode, 'response' => $response ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment