Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //v.5
- $token = 'xxxxxxxxxxxxxxxxxxxxxxxx';
- $token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
- $consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxx';
- $consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
- $host = 'api.twitter.com';
- $method = 'GET';
- $path = '/1.1/statuses/user_timeline.json'; // api call path
- $searchpath = '/1.1/search/tweets.json';
- if (!isset( $_GET["screen_name"] ))
- $_GET["screen_name"] = 'lem0nabg';
- if (!isset( $_GET["count"] ))
- $_GET["count"] = '20';
- $sn = htmlspecialchars($_GET["screen_name"]);
- $cnt = htmlspecialchars($_GET["count"]);
- $q = htmlspecialchars($_GET["q"]);
- if($q) {
- $query = array( // query parameters
- 'q' => $q,
- 'count' => $cnt,
- 'result_type' => 'recent'
- );
- } else {
- $query = array( // query parameters
- 'screen_name' => $sn,
- 'count' => $cnt,
- 'exclude_replies' => 'false'
- );
- }
- $oauth = array(
- 'oauth_consumer_key' => $consumer_key,
- 'oauth_token' => $token,
- 'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
- 'oauth_timestamp' => time(),
- 'oauth_signature_method' => 'HMAC-SHA1',
- 'oauth_version' => '1.0'
- );
- $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
- $query = array_map("rawurlencode", $query);
- $arr = array_merge($oauth, $query); // combine the values THEN sort
- asort($arr); // secondary sort (value)
- ksort($arr); // primary sort (key)
- // http_build_query automatically encodes, but our parameters
- // are already encoded, and must be by this point, so we undo
- // the encoding step
- $querystring = urldecode(http_build_query($arr, '', '&'));
- if($q){
- $url = "https://$host$searchpath";
- }else{
- $url = "https://$host$path";
- }
- // mash everything together for the text to hash
- $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
- // same with the key
- $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
- // generate the hash
- $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
- // this time we're using a normal GET query, and we're only encoding the query params
- // (without the oauth params)
- $url .= "?".http_build_query($query);
- $url=str_replace("&","&",$url); //Patch by @Frewuill
- $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
- ksort($oauth); // probably not necessary, but twitter's demo does it
- // also not necessary, but twitter's demo does this too
- function add_quotes($str) { return '"'.$str.'"'; }
- $oauth = array_map("add_quotes", $oauth);
- // this is the full value of the Authorization line
- $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
- // if you're doing post, you need to skip the GET building above
- // and instead supply query parameters to CURLOPT_POSTFIELDS
- $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
- //CURLOPT_POSTFIELDS => $postfields,
- CURLOPT_HEADER => false,
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false);
- // do our business
- $feed = curl_init();
- curl_setopt_array($feed, $options);
- $json = curl_exec($feed);
- curl_close($feed);
- $twitter_data = json_decode($json, true);
- function processString($s) {
- return preg_replace('/https?:\/\/[\w\-\.!~#?&=+\*\'"(),\/]+/','<a href="$0">$0</a>',$s);
- }
- if (isset( $_GET["test"] )){
- print('id: ' . gettype($twitter_data[0]['id']). '<br>'. PHP_EOL);
- print('id_str: ' . gettype($twitter_data[0]['id_str']). PHP_EOL);
- if ($_GET["test"] == 'json')
- $test = $json;
- else
- $test = $twitter_data;
- print("<pre>");
- print_r($test);
- print("</pre>". PHP_EOL);
- }
- if($q){
- $arrLen = count($twitter_data['statuses']);
- } else {
- $arrLen = count($twitter_data);
- }
- date_default_timezone_set('Europe/Sofia');
- $last_updated = date('Y-m-t\TH:i:sP');
- $screen_name = "";
- if($arrLen > 0){
- $last_updated = DateTime::createFromFormat('D M d H:i:s O Y', $twitter_data[0]['created_at']);
- $last_updated = date_format($last_updated, 'Y-m-d\TH:i:sP');
- $screen_name = $twitter_data[0]['user']['screen_name'];
- }
- if($q){
- $title = $q .' - Twitter Search';
- }
- else {
- $title = 'Twitter / ' . $sn;
- }
- print('<?xml version="1.0" encoding="utf-8"?>' .PHP_EOL);
- print('<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">' .PHP_EOL);
- print('<title>' . $title .'</title>' .PHP_EOL);
- print('<id>tag:twitter.com,2007:Status</id>' .PHP_EOL);
- print('<link type="text/html" rel="alternate" href="http://twitter.com"/>' .PHP_EOL);
- print('<link type="application/atom+xml" rel="self" href="http://lem0na.net/twitrss.php" />' .PHP_EOL);
- print('<updated>' .$last_updated .'</updated>' .PHP_EOL);
- for ($i=0; $i<$arrLen; $i++) {
- if($q){
- $item = $twitter_data['statuses'][$i];
- }
- else {
- $item = $twitter_data[$i];
- }
- print(PHP_EOL. ' <entry>'. PHP_EOL);
- print(' <id>https://twitter.com/'.$item['user']['screen_name'].'/statuses/'. $item['id_str'] .'</id>'. PHP_EOL);
- print(' <link href="https://twitter.com/'.$item['user']['screen_name'].'/statuses/'. $item['id_str'] .'" rel="alternate" type="text/html"/>'. PHP_EOL);
- print(' <title>'.$item['user']['screen_name'].': '.$item['text'].'</title>'. PHP_EOL);
- print(' <summary type="html"><![CDATA['.$item['user']['screen_name'].': '.$item['text'].']]></summary>'. PHP_EOL);
- $feedContent = ' <content type="html"><![CDATA[<html><body><p></p><p>'.$item['text'].'</p></body></html>]]></content>';
- $text = processString($feedContent);
- print($text . PHP_EOL);
- //Sat Jun 15 04:24:10 +0000 2013
- $last_updated = DateTime::createFromFormat('D M d H:i:s O Y', $item['created_at']);
- $last_updated = date_format($last_updated, 'Y-m-d\TH:i:sP');
- print(' <updated>'.$last_updated.'</updated>'. PHP_EOL);
- print(' <author><name></name></author>'. PHP_EOL);
- $hashLen = count($item['entities']['hashtags']);
- if ($hashLen > 0){
- for ($j=0; $j<$hashLen; $j++){
- print(' <category term="'.$item['entities']['hashtags'][$j]['text'].'"/>'. PHP_EOL);
- }
- }
- print(' </entry>'. PHP_EOL);
- }
- print('</feed>'. PHP_EOL);
- print('<!-- vim:ft=xml -->');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement