Last active
May 23, 2016 17:06
-
-
Save williameliel/7c98c6aa3339ecc5a1d388deec175b31 to your computer and use it in GitHub Desktop.
PHP Instagram funcions
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 | |
/* Get instagram User ID */ | |
function get_instagram_id($username) { | |
$user_id = false; | |
if (!isset($username)) return false; | |
$username = str_replace('@', '', $username); | |
$access_token = '14319814.b082a92.c96b36393e574726ad08491ea64731ed'; | |
$url = "https://api.instagram.com/v1/users/search?q=" . $username . "&access_token=" . $access_token; | |
$get = wp_remote_request($url); | |
if (is_array($get) && isset($get['body'])) { | |
$json = json_decode($get['body']); | |
if (isset($json) && isset($json->data)) { | |
foreach ($json->data as $user) { | |
if ($user->username == $username) { | |
$user_id = $user->id; | |
} | |
} | |
} | |
} | |
return $user_id; | |
} | |
function get_instagram($username = 'jdvhotels') { | |
if (!isset($username)) return false; | |
if ($username == '') $username = 'jdvhotels'; | |
$trans_name = TRANSIENT_PREFIX . 'instagram_feed_' . $username; | |
if (false === ($instagram = unserialize(base64_decode(get_transient($trans_name))))) { | |
// Uses BBC News API to fetch new data | |
$instagram = refresh_instagram($username); | |
} elseif (isset($instagram['expiry']) && $instagram['expiry'] < time()) { | |
$instagram['expiry'] = (time() + (2 * HOUR_IN_SECONDS)); | |
set_transient($trans_name, base64_encode(serialize($instagram)), 24 * HOUR_IN_SECONDS); | |
//add_action( 'shutdown', 'refresh_instagram' ,10, 1 ); | |
register_shutdown_function('refresh_instagram', $username, 'SHUT'); | |
} | |
return $instagram; | |
} | |
/* | |
* Refreshes the Transient data. | |
*/ | |
function refresh_instagram($username = 'jdvhotels', $s = '') { | |
//echo $s; | |
if (!isset($username)) return false; | |
$instagram = array(); | |
$content = array(); | |
$trans_name = TRANSIENT_PREFIX . 'instagram_feed_' . $username; | |
$access_token = '14319814.b082a92.c96b36393e574726ad08491ea64731ed'; | |
$username = strtolower($username); | |
$user_id = get_instagram_id($username); | |
$count = 12; | |
//now get feed | |
if ($user_id) { | |
$url = 'https://api.instagram.com/v1/users/' . $user_id . '/media/recent/?count=' . $count . '&access_token=' . $access_token; | |
$obj = wp_remote_request($url); | |
if (isset($obj['body'])) { | |
$obj = json_decode($obj['body']); | |
if (isset($obj)) { | |
foreach ($obj->data as $key => $value) { | |
$content[$key]['username'] = $value->user->username; | |
if (isset($value->caption) && $value->caption->text != '') $content[$key]['text'] = remove_emoji($value->caption->text); | |
$content[$key]['image'] = $value->images; | |
$content[$key]['link'] = $value->link; | |
} | |
} | |
} | |
$instagram['expiry'] = (time() + (2 * HOUR_IN_SECONDS)); | |
$instagram['body'] = $content; | |
set_transient($trans_name, base64_encode(serialize($instagram)), 24 * HOUR_IN_SECONDS); | |
// This is the soft expire | |
} | |
return $instagram; | |
} | |
function remove_emoji($text) { | |
$clean_text = ""; | |
// Match Emoticons | |
$regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u'; | |
$clean_text = preg_replace($regexEmoticons, '', $text); | |
// Match Miscellaneous Symbols and Pictographs | |
$regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u'; | |
$clean_text = preg_replace($regexSymbols, '', $clean_text); | |
// Match Transport And Map Symbols | |
$regexTransport = '/[\x{1F680}-\x{1F6FF}]/u'; | |
$clean_text = preg_replace($regexTransport, '', $clean_text); | |
return $clean_text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment