Skip to content

Instantly share code, notes, and snippets.

@Tusko
Last active October 28, 2016 12:23
Show Gist options
  • Save Tusko/5de2e7abeea4c5b31d3ebde8362eb1d3 to your computer and use it in GitHub Desktop.
Save Tusko/5de2e7abeea4c5b31d3ebde8362eb1d3 to your computer and use it in GitHub Desktop.
instagram + FB timeline
<?php
/**
Instagram API
*/
function instagram_user_ID($username, $client_id, $token){
$ins_id = get_transient( 'instagram_userid' );
if ( false === $ins_id || empty($ins_id) ) {
$url = 'https://api.instagram.com/v1/users/search?q=' . $username . '&client_id=' . $client_id .'&access_token=' . $token;
$api = wp_remote_get( $url );
$data = json_decode($api['body']);
$ins_id = $data->data[0]->id;
set_transient( 'instagram_userid', $ins_id, 12 * '3600' );
}
return $ins_id;
}
function instagram_feed( $username = '', $client_id = '', $access_token = '', $count = 10 ) {
$instagram_id = instagram_user_ID($username, $client_id, $access_token);
$insta_feed = get_transient( 'instagram_feed' . $count );
if ( false === $insta_feed || empty($insta_feed) ) {
$request = 'https://api.instagram.com/v1/users/' . $instagram_id . '/media/recent/?count=' . $count . '&access_token=' . $access_token;
$response = wp_remote_get( $request );
$data = json_decode( $response['body'] );
$insta_feed = $data->data;
set_transient( 'instagram_feed_' . $count, $insta_feed, 12 * '3600' );
}
return $insta_feed;
}
function clear_instagram_cache(){
delete_transient( 'instagram_userid');
}
/**
Facebook Posts no-SDK
*/
function curl_helper($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data);
}
function get_fb_posts($page_ID, $access_token, $limit = 10){
$fb__posts = wp_remote_get('https://graph.facebook.com/'.$page_ID.'/posts?limit='.$limit.'&access_token='.$access_token);
$fb__feed = get_transient( 'fb_feed' );
if ( false === $fb__feed || empty($fb__feed) ) {
$data = json_decode( $fb__posts['body'] );
$fb__feed = $data->data;
set_transient( 'fb_feed', $fb__feed, 12 * '3600' );
}
return $fb__feed;
}
function social_feed(){
$insta_feed = instagram_feed('USERNAME', 'CLIEND_ID', 'ACCESS_TOKEN');
$fb_feed = get_fb_posts('PAGE_ID', 'ACCESS_TOKEN');
$feed = [];
foreach($fb_feed as $fb){
$feed[] = array(
'network' => 'facebook',
'message' => !empty($fb->message) ? wp_trim_words($fb->message, 20) : '',
'date' => date('d.m.Y', strtotime($fb->created_time)),
'source' => 'https://www.facebook.com/' . $fb->id,
'image' => 'https://graph.facebook.com/'.$fb->object_id.'/picture'
);
}
foreach($insta_feed as $ins){
$feed[] = array(
'network' => 'instagram',
'message' => !empty($ins->caption->text) ? wp_trim_words($ins->caption->text, 20) : '',
'date' => date('d.m.Y', $ins->created_time),
'source' => $ins->link,
'image' => $ins->images->standard_resolution->url
);
}
usort($feed, function($a, $b) {
return ($a['date'] < $b['date']) ? -1 : 1;
});
return $feed;
}
function wpa_dump($variable){
$pretty = function($v='',$c="&nbsp;&nbsp;&nbsp;&nbsp;",$in=-1,$k=null)use(&$pretty){$r='';if(in_array(gettype($v),array('object','array'))){$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").'<br>';foreach($v as $sk=>$vl){$r.=$pretty($vl,$c,$in+1,$sk).'<br>';}}else{$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").(is_null($v)?'&lt;NULL&gt;':"<strong>$v</strong>");}return$r;};
echo '<pre style="padding-left: 150px; font-family: Courier New"><code class="json">' . $pretty($variable) . '</code></pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment