static function InsertTweetById($data, $settings, $marathon, $campaign, $database) { $id; $error = false; $errorMessage; $output; if (array_key_exists('Id', $data)) { $id = $data['Id']; } else { if (array_key_exists(0, $data)) { $id = $data[0]; } else { $error = true; $errorMessage = 'No id was provided.'; } } if ($database instanceof ZdpArrayObject) { $error = true; $errorMessage = $database['error']; } if (!$error || true) { $url = 'https://api.twitter.com/1.1/statuses/show.json'; $fields = '?id=' . $id . '&include_my_retweet=0'; $method = 'GET'; $twitter = new TwitterAPIExchange(['oauth_access_token' => $settings['troauthpub'], 'oauth_access_token_secret' => $settings['troauthsec'], 'consumer_key' => $settings['trpub'], 'consumer_secret' => $settings['trsec']]); $tweet = json_decode($twitter->setGetField($fields)->buildOauth($url, $method)->performRequest()); $sql = 'CALL sp_insert_twitterevent (:campaign_id, :twitter_user, :tweet_content, :tweet_date, :marathon_id, :campaign_id)'; $statement = $database->prepare($sql); //Attempts to process the insert statement of the Twitter event. if ($statement->execute([':campaign_id' => 1, ':twitter_user' => $tweet->user->screen_name, ':tweet_content' => $tweet->text, ':tweet_date' => strtotime($tweet->created_at), ':marathon_id' => $marathon, ':campaign_id' => $campaign])) { $output = new ZdpArrayObject(['result' => new ZdpArrayObject(['id' => $tweet->id_str])]); } else { $output = new ZdpArrayObject(['error' => $statement->errorInfo()]); } } else { $output = new ZdpArrayObject(['error' => $errorMessage]); } return $output; }
/* by Aldo Ginting */ require_once 'TwitterAPIExchange.php'; /* tokens */ $settings = array('oauth_access_token' => '102024246-fPvUBae3FMD1Ab4zQDxzi7SUSvOYPpkZnbaWGNHE', 'oauth_access_token_secret' => 'aBYuCbiItkU99caHK8S4mIzbUBnx8YSB7IP4HrbRpjYTg', 'consumer_key' => 'Sgym1Ov9J4opgdB7gNtQHC0tP', 'consumer_secret' => '5icBNUmq3GcWhT7seMzD34SLMBhAXbCDMBQiOBe4sqZ9gEiXrw'); // user timeline request settings $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $requestmethod = 'GET'; $getField = '?screen_name=aldogint&count=5'; // instance TwitterAPIExchange $twitterAPIExchange = new TwitterAPIExchange($settings); // throw request // then decode json to associative array $response = json_decode($twitterAPIExchange->setGetField($getField)->buildOauth($url, $requestmethod)->performRequest(), $assoc = true); // check for error if (isset($response["errors"])) { echo "<h3>Oops, we encountered a problem.</h3><p>Here's error message:</p><p><em>" . $response["errors"][0]["message"] . "</em></p>"; exit; } // echo "<pre>"; // echo var_dump($response) . "</pre>"; // display foreach ($response as $items) { echo "<hr>"; echo $items['user']['name'] . " (@" . $items['user']['screen_name'] . ") - " . $items['created_at']; echo "<br>" . screenNameAsLink(convertLink($items['text'])); echo "<hr>"; } // functions
while ($cursor != 0) { $i++; //Get the first 5,000 followers, use the cursor to step through $getfield = '?screen_name=' . $twUser . '&cursor=' . $cursor; $followers_json = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(); $followers_decoded = json_decode($followers_json); fwrite($handle, "FETCHING MORE LOOP NUMBER : " . $i . ' | ' . $cursor . "\n"); //the lookup.json method can only handle a max of 100 at a time $to_look_up = array_chunk($followers_decoded->ids, 100); foreach ($to_look_up as &$ids) { //nothing dupes, skip to the next 100 if (count($ids) == 0) { continue; } //hydrate the remaining ids $apiResp = $twitter->setGetField('?user_id=' . implode(',', $ids))->buildOauth('https://api.twitter.com/1.1/users/lookup.json', $requestMethod)->performRequest(); $users_list = json_decode($apiResp); set_time_limit(30); foreach ($users_list as $key => $user) { if (filter_var($user->profile_image_url, FILTER_VALIDATE_URL) !== false) { //download the image $filename = explode('/', $user->profile_image_url); $filename = end($filename); $filename = str_replace('_normal.', '_bigger.', $filename); $ext = explode('.', $filename); $ext = end($ext); $success = file_put_contents('images/' . $user->screen_name . '.' . $user->id . '.' . $ext, file_get_contents(str_replace('_normal.', '_bigger.', $user->profile_image_url))); $write = $user->id . " :: " . $user->screen_name . " :: " . $success . "\n"; fwrite($handle, $write); } else { $write = $user->id . " :: " . $user->screen_name . " :: " . $user->profile_image_url . " :: " . 'NO URL FOR IMAGE' . "\n";
<?php require_once 'TwitterAPIExchange.php'; require_once 'Geocode.php'; /** Set access tokens **/ $settings = array('oauth_access_token' => "363822513-CPj3YUXqvSiUjd6819PRqGmafXKiNVXyG6R69j3j", 'oauth_access_token_secret' => "8u3Iwa24EsoxynyJrsklgOOx0GJbNYrcnWe9lXIZviwiy", 'consumer_key' => "O6ltnFO2colJblrbNRewDPfNq", 'consumer_secret' => "QKFj889PAgPKoHbUUUTzd5lCFqHCknudBRsTu0JPBQPSB5rCaO"); $url = "https://api.twitter.com/1.1/search/tweets.json"; $requestMethod = "GET"; /*$since_id = "471479529731411968"; $max_id = "697189541694873600";*/ $getfield = '?q=%23#flintwatercrisis&src=tyah&result_type=recent&count=100'; $twitter = new TwitterAPIExchange($settings); $string = json_decode($twitter->setGetField($getfield)->buildOauth($url, $requestMethod)->performRequest(), $assoc = TRUE); if (file_exists('coordinates.json')) { $saved_coords = json_decode(file_get_contents('coordinates.json'), true); } else { $saved_coords['flint'] = array('lat' => 43.01, 'long' => 83.69); } $count = 0; foreach ($string["statuses"] as $statuses) { $count++; $location = strtolower($statuses['user']['location']); //check to see if we already have that location OR get it. if ($location && !array_key_exists($location, $saved_coords)) { $coordinates = geocode($location); $saved_coords[$location] = array('lat' => $coordinates['lat'], 'long' => $coordinates['long']); } $id = $statuses['id']; echo "Tweet_ID: " . $id . "<br />"; //convert date to mysql datetime format $twitter_date = $statuses['created_at'];