Last active
August 29, 2015 14:09
-
-
Save agarzon/804a686753f35a5ac92a to your computer and use it in GitHub Desktop.
Search images, galleries and media through Twitter AP.
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 | |
/** | |
* Twitter API search images | |
* | |
* @author Alexander Garzon <http://about.me/agarzon> | |
* @license MIT License | |
* @require http://github.com/j7mbo/twitter-api-php | |
*/ | |
//header('Access-Control-Allow-Origin: *'); | |
require_once 'TwitterAPIExchange.php'; | |
class TwitterSearch { | |
private $twitter; | |
public function __construct(array $settings) { | |
$this->twitter = new TwitterAPIExchange($settings); | |
} | |
public function search($query = "#sunset", $qty = 100) { | |
$query = rawurlencode($query); | |
$getfield = "?q={$query}&result_type=recent&count={$qty}&include_entities=true"; | |
$url = 'https://api.twitter.com/1.1/search/tweets.json'; | |
$response = json_decode($this->twitter->setGetfield($getfield)->buildOauth($url, 'GET')->performRequest()); | |
return $this->_loopTweets($response->{'statuses'}); | |
} | |
/** | |
* Iterate tweets | |
* | |
* @param object $Obj tweet as object | |
* @return array | |
*/ | |
protected function _loopTweets($Obj) { | |
$count = count($Obj); | |
$array = array(); | |
for ($i = 0; $i < $count; $i++) { | |
// If tweet contain images | |
if (isset($Obj[$i]->entities->media) && !empty($Obj[$i]->entities->media)) { | |
$array[$i]['id'] = $Obj[$i]->{'id_str'}; | |
$array[$i]['account'] = $Obj[$i]->{'user'}->{'screen_name'}; | |
$array[$i]['text'] = $Obj[$i]->{'text'}; | |
$array[$i]['datetime'] = date('Y-m-d H:i:s', strtotime($Obj[$i]->{'created_at'})); | |
$array[$i]['medias'] = $Obj[$i]->entities->media; | |
} | |
} | |
return $array; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Twitter Gallery</title> | |
</head> | |
<body> | |
<?php | |
$settings = array( | |
'oauth_access_token' => 'xxx-xxx', | |
'oauth_access_token_secret' => 'xxx', | |
'consumer_key' => 'xxx', | |
'consumer_secret' => 'xxx' | |
); | |
$result = new TwitterSearch($settings); | |
?> | |
<div id="slides"> | |
<ul class="slides-container"> | |
<?php foreach ($result->search('futbol') as $key => $value) { | |
echo '<li> | |
<a href="https://twitter.com/' . $value['account'] . '/status/' . $value['id'] . '" target="_blank"> | |
<img src="' . $value['medias'][0]->media_url_https . '" alt=""> | |
</a> | |
</li>'; | |
} ;?> | |
</ul> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment