Last active
August 29, 2015 14:02
-
-
Save zourite/2499604f77c14d2d036b to your computer and use it in GitHub Desktop.
Get Instagram and Twitter Post via API
This file contains 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 | |
function buildBaseString( $baseURI, $method, $params ) { | |
$r = array(); | |
ksort( $params ); | |
foreach ( $params as $key=>$value ) { | |
$r[] = "$key=" . rawurlencode( $value ); | |
} | |
return $method."&" . rawurlencode( $baseURI ) . '&' . rawurlencode( implode( '&', $r ) ); | |
} | |
function buildAuthorizationHeader( $oauth ) { | |
$r = 'Authorization: OAuth '; | |
$values = array(); | |
foreach ( $oauth as $key=>$value ) | |
$values[] = "$key=\"" . rawurlencode( $value ) . "\""; | |
$r .= implode( ', ', $values ); | |
return $r; | |
} | |
function save_stream() { | |
$url = "https://api.twitter.com/1.1/search/tweets.json"; | |
$oauth_access_token = ""; | |
$oauth_access_token_secret = ""; | |
$consumer_key = ""; | |
$consumer_secret = ""; | |
$hashtag = utf8_decode( 'team974' ); | |
$oauth = array( 'oauth_consumer_key' => $consumer_key, | |
'oauth_nonce' => time(), | |
'oauth_signature_method' => 'HMAC-SHA1', | |
'oauth_token' => $oauth_access_token, | |
'oauth_timestamp' => time(), | |
'q' => $hashtag, | |
'count' => 20, | |
'oauth_version' => '1.0' ); | |
$base_info = buildBaseString( $url, 'GET', $oauth ); | |
$composite_key = rawurlencode( $consumer_secret ) . '&' . rawurlencode( $oauth_access_token_secret ); | |
$oauth_signature = base64_encode( hash_hmac( 'sha1', $base_info, $composite_key, true ) ); | |
$oauth['oauth_signature'] = $oauth_signature; | |
// Make Requests | |
$header = array( buildAuthorizationHeader( $oauth ), 'Expect:' ); | |
$options = array( CURLOPT_HTTPHEADER => $header, | |
//CURLOPT_POSTFIELDS => $postfields, | |
CURLOPT_HEADER => false, | |
CURLOPT_URL => $url."?q=$hashtag&count=20", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false ); | |
$feed = curl_init(); | |
curl_setopt_array( $feed, $options ); | |
$json = curl_exec( $feed ); | |
curl_close( $feed ); | |
if ( json_decode( $json ) != null ) : | |
$fp = fopen( 'wp-content/themes/twitter.txt', 'w' ); | |
fwrite( $fp, $json ); | |
fclose( $fp ); | |
endif; | |
callInstagram(); | |
} | |
function callInstagram() { | |
$tag = '974'; | |
$client_id = ""; | |
$urlinsta = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id.'&count=20'; | |
$ch = curl_init(); | |
curl_setopt_array( $ch, | |
array( | |
CURLOPT_URL => $urlinsta, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false, | |
CURLOPT_SSL_VERIFYHOST => 2 | |
) | |
); | |
$result = curl_exec( $ch ); | |
curl_close( $ch ); | |
if ( json_decode( $result ) != null ) : | |
$fp = fopen( 'wp-content/themes/instagram.txt', 'w' ); | |
fwrite( $fp, $result ); | |
fclose( $fp ); | |
endif; | |
} | |
function instastream() { | |
$result = file_get_contents( 'wp-content/themes/instagram.txt' ); | |
$result = json_decode( $result, true ); | |
return $result['data']; | |
} | |
function lifestream() { | |
$result = file_get_contents( 'wp-content/themes/twitter.txt' ); | |
$tweets = json_decode( $result ); | |
foreach ( $tweets->statuses as $status ) { | |
$retweet = strpos( $status->text, 'RT @' ); | |
if ( $retweet === false ): | |
$status->text = preg_replace( "#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $status->text ); | |
$status->text = preg_replace( "#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $status->text ); | |
$status->text = preg_replace( "/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $status->text ); | |
$status->text = preg_replace( "/#(\w+)/", "<a href=\"https://twitter.com/search?q=%23\\1\" >#\\1</a>", $status->text ); | |
$tweet[] = array( | |
'tweet' => 0, | |
'text' => $status->text, | |
'id' => $status->id, | |
'user' => $status->user, | |
'type' => $status->entities | |
/* 'imgurl' => $status->entities->media->media_url */ | |
) ; | |
endif; | |
} | |
return $tweet; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment