Skip to content

Instantly share code, notes, and snippets.

@atomicpages
Created October 15, 2014 20:40
Show Gist options
  • Save atomicpages/9a46d6e4f74690e7e786 to your computer and use it in GitHub Desktop.
Save atomicpages/9a46d6e4f74690e7e786 to your computer and use it in GitHub Desktop.
<?php
/**
* Class Instagram
* A simple class that connects to the Instagram API without the overhead
* of other classes. This class depends on cURL being installed on your OS
* and it MUST be enabled in PHP. If you're not sure if it's enabled, you can
* run phpinfo() and search for cURL.
* @author Dennis Thompson
* @license MIT
* @version 1.0
* @copyright AtomicPages LLC 2014
*/
class Instagram {
private $displaySize, $accessToken, $count, $id, $cache;
public function __construct($id, $token, $cache = true) {
$this->id = $id;
$this->accessToken = $token;
$this->count = 10;
$this->cache = $cache;
}
public function size($size) {
$this->displaySize = $size;
}
public function cache($bool) {
$this->cache = $bool;
}
public function token($token) {
$this->accessToken = $token;
}
public function count($count) {
$this->count = $count;
}
public function getCache() {
return $this->cache;
}
public function getCount() {
return $this->count;
}
public function getSize() {
return $this->displaySize;
}
/**
* Returns the access token. We don't want this method public!
* @return string
* @access protected
*/
protected function getAccessToken() {
return $this->accessToken;
}
/**
* Returns the ID of the user's instagram feed.
* @return string
* @access protected
*/
protected function getID() {
return $this->id;
}
/**
* Pulls information from the Instagram API and returns it as a hashtable
* @return array | NULL on failure
* @throws InvalidArgumentException
* @access public
*/
public function fetch() {
if(!isset($this->id)) {
throw new InvalidArgumentException("ID cannot be empty!", 1);
}
if(!isset($this->accessToken)) {
throw new InvalidArgumentException("Access Token cannot be empty!", 1);
}
if(!function_exists("curl_init")) {
throw new BadFunctionCallException("cURL is not enabled on this host! It must be enabled for this script
to work!", 1);
}
$url = "https://api.instagram.com/v1/users/" . $this->id . "/media/recent/?access_token=" . $this->accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
if($this->cache) {
// TODO: need to handle this exception with something so we know when it happens. Maybe use try-catch?
$this->writeCache($result);
}
return $result;
}
/**
* A method that writes custom JSON data directly to a file.
* The file serves as local cache to reduce the number of calls
* to the Instagram API and takes the load off the server when high
* volumes of users are on the site. Returns false on failure.
* @param $array
* @return bool
* @see buildJsonString for JSON format
*/
// TODO: Maybe can the return value and throw exceptions instead?
private function writeCache($array) {
if(!is_object($array)) {
throw new InvalidArgumentException("Parameter must be an array!", 1);
}
$filename = $_SERVER["DOCUMENT_ROOT"] . "/insta_cache.json";
if(!file_exists($filename) || (filemtime($filename) > (time() - 60 * 60 * 2 ))) { // cache is > 2 hours old
$fp = fopen($filename, "w");
if($fp != NULL) {
$json = $this->buildJsonString($array);
if(flock($fp, LOCK_EX)) { // acquire exclusive lock
if(!ftruncate($fp, 0)) { // truncate the file
rewind($fp); // rewind the stream just in case...
}
fwrite($fp, $json);
flock($fp, LOCK_UN); // release the lock
}
fclose($fp);
}
}
}
/**
* A method that formats and converts $array into a JSON string
* that is handed off to Instagram::writeCache()
* @param $array
* @return string
* @see writeCache
*/
private function buildJsonString($array) {
if(!is_object($array)) {
throw new InvalidArgumentException("Parameter must be an array!", 1);
}
if(count($array->data) < $this->getCount()) {
$this->count(count($array->data));
}
$json = "{\n\t\"images\": [\n";
$separator = ",";
for($i = 0; $i < $this->getCount(); $i++) {
if($i == $this->getCount() - 1) {
$separator = "";
}
$json .= json_encode($array->data[$i]->images) . $separator;
}
$json .= "\n\t]\n}";
unset($separator); // deallocate memory for $separator
return $json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment