Created
January 26, 2015 18:50
-
-
Save cbertelegni/e32dd92f96da77e8f662 to your computer and use it in GitHub Desktop.
Search de twitter con php. Usando la libreria : TwitterAPIExchange
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 | |
ini_set('display_errors', 1); | |
require_once('lib/TwitterAPIExchange.php'); | |
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/ | |
require "conf.php"; | |
define("LAST_METADATA_SEARCH_FILE", "app/data/last_metadata_search.txt"); | |
define("TUITS_FILE", "app/data/tuits_nisman.txt"); | |
/** Perform a GET request and echo the response **/ | |
/** Note: Set the GET field BEFORE calling buildOauth(); **/ | |
// https://dev.twitter.com/rest/reference/get/search/tweets | |
function get_data($next_results = false){ | |
$settings = array( | |
'oauth_access_token' => OAUTH_TOKEN, | |
'oauth_access_token_secret' => OAUTH_SECRET, | |
'consumer_key' => TWITTER_CONSUMER_KEY, | |
'consumer_secret' => TWITTER_CONSUMER_SECRET | |
); | |
$url = 'https://api.twitter.com/1.1/search/tweets.json'; | |
if($next_results){ | |
$getfield = $next_results; | |
}else{ | |
$getfield = '?q=nisman'; | |
$getfield .= '&count=100'; | |
} | |
$requestMethod = 'GET'; | |
$twitter = new TwitterAPIExchange($settings); | |
$response = $twitter->setGetfield($getfield) | |
->buildOauth($url, $requestMethod) | |
->performRequest(); | |
$json = json_decode($response); | |
save_response($json); | |
} | |
function save_response($json){ | |
print "saving twitts...\n"; | |
if(isset($json->statuses[0])){ | |
$r_old_tw = @fread(fopen(TUITS_FILE, "r"), filesize(TUITS_FILE)); | |
$old_tws = @json_decode($r_old_tw); | |
$gestor = fopen(TUITS_FILE, "w"); | |
if( is_array($old_tws)){ | |
print "Merging Arrays... \n"; | |
print "The last tw saved is date: " . end($old_tws)->created_at; | |
print "\n"; | |
print "merging with new request: " . $json->statuses[0]->created_at; | |
print "\n"; | |
print "Merging Array...\n"; | |
fwrite($gestor, json_encode(array_merge($old_tws, $json->statuses))); | |
print "Count actuals TWS: " . count($old_tws) + 100; | |
}else{ | |
print "************************\n"; | |
print "\tCreating doc..\n"; | |
print "************************\n"; | |
fwrite($gestor, json_encode($json->statuses)); | |
} | |
fclose($gestor); | |
if(isset($json->search_metadata->next_results)){ | |
// """save metadata""" | |
print "Saving metadata serch..\n"; | |
print $json->search_metadata->next_results."\n"; | |
$gestor2 = fopen(LAST_METADATA_SEARCH_FILE, "w"); | |
fwrite($gestor2, json_encode($json->search_metadata)); | |
fclose($gestor2); | |
print "Getting more data...\n"; | |
print "sleeping 6 seconds...\n"; | |
sleep(10); | |
get_data($json->search_metadata->next_results); | |
}else{ | |
print "No more data\n"; | |
} | |
}else{ | |
print("No response. \n"); | |
print(json_encode($json)); | |
} | |
} | |
print "Start app...\n"; | |
$last_metadata = @json_decode(fread(fopen(LAST_METADATA_SEARCH_FILE, "r"), filesize(LAST_METADATA_SEARCH_FILE))); | |
if(isset($last_metadata->next_results)){ | |
print "Getting data from last search...\n"; | |
print "If you want get data from now, press 'ctrl+c' and delete this file: ".LAST_METADATA_SEARCH_FILE."...\n"; | |
get_data($last_metadata->next_results); | |
}else{ | |
// print "Getting data from the newest results.\n"; | |
get_data(); | |
}; | |
// get_data($json->search_metadata->next_results); | |
/* formt response: | |
{ | |
statuses: [], | |
search_metadata: { | |
completed_in: 0.09, | |
max_id: 558361186623045600, | |
max_id_str: "558361186623045632", | |
next_results: "?max_id=558361164116398079&q=nisman&include_entities=1", | |
query: "nisman", | |
refresh_url: "?since_id=558361186623045632&q=nisman&include_entities=1", | |
count: 15, | |
since_id: 0, | |
since_id_str: "0" | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment