Advertisement
Guest User

ttrss - twitter

a guest
Jun 15th, 2013
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. <?php
  2. //v.5
  3.  
  4. $token            = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  5. $token_secret     = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  6. $consumer_key     = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  7. $consumer_secret  = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  8.  
  9. $host = 'api.twitter.com';
  10. $method = 'GET';
  11. $path = '/1.1/statuses/user_timeline.json'; // api call path
  12. $searchpath = '/1.1/search/tweets.json';
  13.  
  14. if (!isset( $_GET["screen_name"] ))
  15.    $_GET["screen_name"] = 'lem0nabg';
  16.    
  17. if (!isset( $_GET["count"] ))
  18.    $_GET["count"] = '20';
  19.  
  20. $sn = htmlspecialchars($_GET["screen_name"]);
  21. $cnt = htmlspecialchars($_GET["count"]);
  22. $q = htmlspecialchars($_GET["q"]);
  23.  
  24.  
  25. if($q) {
  26.   $query = array( // query parameters
  27.       'q' => $q,
  28.       'count' => $cnt,
  29.       'result_type' => 'recent'
  30.   );  
  31.  
  32. } else {
  33.   $query = array( // query parameters
  34.       'screen_name' => $sn,
  35.       'count' => $cnt,
  36.       'exclude_replies' => 'false'
  37.   );
  38. }
  39.  
  40. $oauth = array(
  41.     'oauth_consumer_key' => $consumer_key,
  42.     'oauth_token' => $token,
  43.     'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
  44.     'oauth_timestamp' => time(),
  45.     'oauth_signature_method' => 'HMAC-SHA1',
  46.     'oauth_version' => '1.0'
  47. );
  48.  
  49. $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
  50. $query = array_map("rawurlencode", $query);
  51.  
  52. $arr = array_merge($oauth, $query); // combine the values THEN sort
  53.  
  54. asort($arr); // secondary sort (value)
  55. ksort($arr); // primary sort (key)
  56.  
  57. // http_build_query automatically encodes, but our parameters
  58. // are already encoded, and must be by this point, so we undo
  59. // the encoding step
  60. $querystring = urldecode(http_build_query($arr, '', '&'));
  61.  
  62. if($q){
  63.   $url = "https://$host$searchpath";
  64. }else{
  65.   $url = "https://$host$path";
  66. }
  67.  
  68. // mash everything together for the text to hash
  69. $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
  70.  
  71. // same with the key
  72. $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
  73.  
  74. // generate the hash
  75. $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
  76.  
  77. // this time we're using a normal GET query, and we're only encoding the query params
  78. // (without the oauth params)
  79. $url .= "?".http_build_query($query);
  80. $url=str_replace("&amp;","&",$url); //Patch by @Frewuill
  81.  
  82. $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
  83. ksort($oauth); // probably not necessary, but twitter's demo does it
  84.  
  85. // also not necessary, but twitter's demo does this too
  86. function add_quotes($str) { return '"'.$str.'"'; }
  87. $oauth = array_map("add_quotes", $oauth);
  88.  
  89. // this is the full value of the Authorization line
  90. $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
  91.  
  92. // if you're doing post, you need to skip the GET building above
  93. // and instead supply query parameters to CURLOPT_POSTFIELDS
  94. $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
  95.                   //CURLOPT_POSTFIELDS => $postfields,
  96.                   CURLOPT_HEADER => false,
  97.                   CURLOPT_URL => $url,
  98.                   CURLOPT_RETURNTRANSFER => true,
  99.                   CURLOPT_SSL_VERIFYPEER => false);
  100.  
  101. // do our business
  102. $feed = curl_init();
  103. curl_setopt_array($feed, $options);
  104. $json = curl_exec($feed);
  105. curl_close($feed);
  106.  
  107. $twitter_data = json_decode($json, true);
  108.  
  109. function processString($s) {
  110.     return preg_replace('/https?:\/\/[\w\-\.!~#?&=+\*\'"(),\/]+/','<a href="$0">$0</a>',$s);
  111. }
  112.  
  113. if (isset( $_GET["test"] )){  
  114.    print('id: ' . gettype($twitter_data[0]['id']). '<br>'. PHP_EOL);
  115.    print('id_str: ' . gettype($twitter_data[0]['id_str']). PHP_EOL);
  116.    
  117.    if ($_GET["test"] == 'json')
  118.       $test = $json;
  119.    else
  120.       $test = $twitter_data;
  121.    
  122.    print("<pre>");
  123.    print_r($test);
  124.    print("</pre>". PHP_EOL);
  125. }
  126. if($q){
  127.   $arrLen = count($twitter_data['statuses']);
  128. } else {
  129.   $arrLen = count($twitter_data);
  130. }
  131. date_default_timezone_set('Europe/Sofia');
  132. $last_updated = date('Y-m-t\TH:i:sP');
  133. $screen_name = "";
  134. if($arrLen > 0){
  135.   $last_updated = DateTime::createFromFormat('D M d H:i:s O Y', $twitter_data[0]['created_at']);
  136.   $last_updated = date_format($last_updated, 'Y-m-d\TH:i:sP');
  137.   $screen_name = $twitter_data[0]['user']['screen_name'];
  138. }
  139.  
  140. if($q){
  141.   $title = $q .' - Twitter Search';
  142. }
  143. else {
  144.   $title = 'Twitter / ' . $sn;
  145. }
  146.  
  147. print('<?xml version="1.0" encoding="utf-8"?>' .PHP_EOL);
  148. print('<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">' .PHP_EOL);
  149. print('<title>' . $title .'</title>' .PHP_EOL);
  150. print('<id>tag:twitter.com,2007:Status</id>' .PHP_EOL);
  151. print('<link type="text/html" rel="alternate" href="http://twitter.com"/>' .PHP_EOL);
  152. print('<link type="application/atom+xml" rel="self" href="http://lem0na.net/twitrss.php" />' .PHP_EOL);
  153. print('<updated>' .$last_updated .'</updated>' .PHP_EOL);
  154.  
  155. for ($i=0; $i<$arrLen; $i++) {
  156.       if($q){
  157.         $item = $twitter_data['statuses'][$i];
  158.       }
  159.       else {
  160.         $item = $twitter_data[$i];
  161.       }
  162.       print(PHP_EOL. '   <entry>'. PHP_EOL);
  163.       print('      <id>https://twitter.com/'.$item['user']['screen_name'].'/statuses/'. $item['id_str'] .'</id>'. PHP_EOL);
  164.       print('      <link href="https://twitter.com/'.$item['user']['screen_name'].'/statuses/'. $item['id_str'] .'" rel="alternate" type="text/html"/>'. PHP_EOL);
  165.       print('      <title>'.$item['user']['screen_name'].': '.$item['text'].'</title>'. PHP_EOL);
  166.       print('      <summary type="html"><![CDATA['.$item['user']['screen_name'].': '.$item['text'].']]></summary>'. PHP_EOL);
  167.      
  168.       $feedContent = '      <content type="html"><![CDATA[<html><body><p></p><p>'.$item['text'].'</p></body></html>]]></content>';
  169.       $text = processString($feedContent);
  170.      
  171.       print($text . PHP_EOL);
  172.       //Sat Jun 15 04:24:10 +0000 2013
  173.       $last_updated = DateTime::createFromFormat('D M d H:i:s O Y', $item['created_at']);
  174.       $last_updated = date_format($last_updated, 'Y-m-d\TH:i:sP');      
  175.       print('      <updated>'.$last_updated.'</updated>'. PHP_EOL);
  176.       print('      <author><name></name></author>'. PHP_EOL);
  177.      
  178.       $hashLen = count($item['entities']['hashtags']);
  179.       if ($hashLen > 0){
  180.          for ($j=0; $j<$hashLen; $j++){
  181.             print('      <category term="'.$item['entities']['hashtags'][$j]['text'].'"/>'. PHP_EOL);
  182.          }
  183.       }
  184.      
  185.    print('   </entry>'. PHP_EOL);
  186. }
  187.  
  188. print('</feed>'. PHP_EOL);
  189. print('<!-- vim:ft=xml -->');
  190. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement