Twitter RSS Feeds

Support requests, bug reports, etc. go here. Dedicated servers / VDS hosting only
AngryChris
Bear Rating Master
Bear Rating Master
Posts: 135
Joined: 08 Apr 2013, 02:42

Re: Twitter RSS Feeds

Postby AngryChris » 14 Jun 2013, 01:04

jdelamater99 wrote:OSX is based off Debian.

I know this doesn't really add anything terribly useful to the discussion, but OS X is not based on Debian in any way, shape, or form. OS X is based on a Mach microkernel taken from NeXTStep (called XNU) sitting on a mostly FreeBSD-ish userland (as in forked off during the FreeBSD 5 timeframe) with added SysV-isms, Apple special sauce, and some GNU goodness (that's all the Darwin portion, what you see with 'uname -a' in Terminal.app). Most of the "guts" of what one would call "OS X" (i.e.; anything that drives or supports the desktop) are written in Objective-C (another carry-over from NEXT). This stuff has been around way way longer than Debian(*) and has nothing to do with Linux.

Code: Select all

[email protected]:~$ uname -a
Darwin artemis.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May  1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
[email protected]:~$

Here's the kernel license: http://www.opensource.apple.com/source/ ... LE_LICENSE

You can download those portions that are open source here where you'll note that XNU is licensed under both the BSD license and the APSL (Apple Public Source License) license.

Some common commands:

Code: Select all

[email protected]:~$ for command in ls grep ; do man $command | head -2 | tail -1 ;  done
LS(1)                     BSD General Commands Manual                    LS(1)
GREP(1)                   BSD General Commands Manual                  GREP(1)
[email protected]:~$

All of this is perhaps unnecessary to say, but I see this "OS X is based on <insert Linux flavor here>" misconception all over the place and it starts to irk me at a certain point.

(*) FreeBSD traces its roots back to 386BSD which traces its roots back to 4.4BSD-Lite.

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 05:35

I meant BSD, not Debian. Long day at work. :oops:

And, yes, I did over simplified it. massively.

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 05:50

Updated the code to v.5

Removed ini_set
Remove preg_replace from $twitter_data
Removed strval() from id calls. didn't work and by redundant anyway by using id_str
Removed favicon, since it didn't work.

updated the test code
updated links to use https for the twitter status links.

AngryChris
Bear Rating Master
Bear Rating Master
Posts: 135
Joined: 08 Apr 2013, 02:42

Re: Twitter RSS Feeds

Postby AngryChris » 14 Jun 2013, 06:31

jdelamater99 wrote:I meant BSD, not Debian. Long day at work. :oops:

And, yes, I did over simplified it. massively.

Well, if you'd just said that!
(And to be fair, I've been guilty of exactly the same thing.)

:)

Anyway, no harm, no foul! Hopefully it's been educational for some!

twitterfan
Bear Rating Trainee
Bear Rating Trainee
Posts: 9
Joined: 13 Jun 2013, 01:58

Re: Twitter RSS Feeds

Postby twitterfan » 14 Jun 2013, 13:20

Can nobody help me? I am not familiar with all these code-snippets. I just copied the code from the beginning, all posts of twitter are shown in my RSS-Widget in WordPress now - but the links are all wrong, leading to an error-message of twitter.com.

What can I do to make those links work?

Code: Select all

<?php
$token = 'XXXXXXXXXXXX';
$token_secret = 'XXXXXXXXXXXXXXX';
$consumer_key = 'XXXXXXXXXXXXXX';
$consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path

if (!isset( $_GET["screen_name"] ))
   $_GET["screen_name"] = 'MYUSERNAMEOFCOURSE';
   
if (!isset( $_GET["count"] ))
   $_GET["count"] = '5';
$twitter_data = json_decode(preg_replace ('/:\s?(\d{14,})/', ': "${1}"', $json), true);
$sn = htmlspecialchars($_GET["screen_name"]);
$cnt = htmlspecialchars($_GET["count"]);

$query = array( // query parameters
    'screen_name' => $sn,
    'count' => $cnt,
    'exclude_replies' => 'true'
);

$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, '', '&'));

$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("&amp;","&",$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);
}

print('<?xml version="1.0" encoding="utf-8"?>'. PHP_EOL);
print('<?xml-stylesheet type="text/xsl" href="atom-to-html.xsl"?>'. PHP_EOL);

print('<feed xmlns="http://www.w3.org/2005/Atom">'. PHP_EOL);

$arrLen = count($twitter_data);
for ($i=0; $i<$arrLen; $i++) {
   print(PHP_EOL. '<entry>'. PHP_EOL);
      print('<id>http://twitter.com/'.$twitter_data[$i]['user']['screen_name'].'/statuses/'. $twitter_data[$i]['id'] .'</id>'. PHP_EOL);
      print('<link href="http://twitter.com/'.$twitter_data[$i]['user']['screen_name'].'/statuses/'. $twitter_data[$i]['id'] .'" rel="alternate" type="text/html"/>'. PHP_EOL);
      print('<title>'.$twitter_data[$i]['user']['screen_name'].': '.$twitter_data[$i]['text'].'</title>'. PHP_EOL);
      print('<summary type="html"><![CDATA['.$twitter_data[$i]['user']['screen_name'].': '.$twitter_data[$i]['text'].']]></summary>'. PHP_EOL);
     
      $feedContent = '<content type="html"><![CDATA[<html><body><p></p><p>'.$twitter_data[$i]['text'].'</p></body></html>]]></content>';
      $text = processString($feedContent);
     
      print($text . PHP_EOL);
      print('<updated>'.$twitter_data[$i]['created_at'].'</updated>'. PHP_EOL);
      print('<author><name></name></author>'. PHP_EOL);
     
      $hashLen = count($twitter_data[$i]['entities']['hashtags']);
      if ($hashLen > 0){
         for ($j=0; $j<$hashLen; $j++){
            print('<category term="'.$twitter_data[$i]['entities']['hashtags'][$j]['text'].'"/>'. PHP_EOL);
         }
      }
     
   print('</entry>'. PHP_EOL);
}

print('</feed>'. PHP_EOL);
print('<!-- vim:ft=xml -->');

/*
print("<pre>");
print_r($twitter_data);
print("</pre>");
*/

?>

virgo
Bear Rating Trainee
Bear Rating Trainee
Posts: 37
Joined: 12 Jun 2013, 22:14

Re: Twitter RSS Feeds

Postby virgo » 14 Jun 2013, 15:12

twitterfan wrote:Can nobody help me? I am not familiar with all these code-snippets. I just copied the code from the beginning, all posts of twitter are shown in my RSS-Widget in WordPress now - but the links are all wrong, leading to an error-message of twitter.com.

What can I do to make those links work?


Just use code from fist post and only change authentication part of it (it has been updated with fixes).
From your code posting it seems, that you tried to add changes into code by yourself, but you did it at wrong places - instead of changing existing line, you added line new before the original line. And because of this it could not work, because original code was still run (and actually at the time $json was even undefined).

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 15:13

Do you links look like twitter.com/USERNAME/statuses/3.452746090311E+17 ?

If that's the case, try the new version (v.5 as of right now). The code in the original post on the first page has been updated.

virgo
Bear Rating Trainee
Bear Rating Trainee
Posts: 37
Joined: 12 Jun 2013, 22:14

Re: Twitter RSS Feeds

Postby virgo » 14 Jun 2013, 15:17

jdelamater99 wrote:Removed favicon, since it didn't work.

I just noticed, that in my php script it was

Code: Select all

 print('<link href="//abs.twimg.com/favicons/favicon.ico" rel="shortcut icon" type="image/x-icon" />'. PHP_EOL);


Could the missing "https:" be reason for not working? And your post about adding it was also exactly like that.

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 15:58

Yeah, that's protocol-less URLs. I had only heard about them recently. Basically, the theory is, you leave off the http/https, and the browser auto figures out which one it needs.

I copied that link directly from Twitter's source code. It obviously works on Twitter, but doesn't work for me. I had the same issue with the mobile tt-rss version I use. The author was using protocol-less URLs, and it wasn't working. As soon as I added the protocol to the URLs, it worked for me.

That said, I tested the twitter favicon with http and https, and it still didn't work.

cqrt
Bear Rating Disaster
Bear Rating Disaster
Posts: 70
Joined: 05 Apr 2013, 00:37

Re: Twitter RSS Feeds

Postby cqrt » 14 Jun 2013, 19:54

jdelamater99 wrote:Do you links look like twitter.com/USERNAME/statuses/3.452746090311E+17 ?

If that's the case, try the new version (v.5 as of right now). The code in the original post on the first page has been updated.


I'd strongly recommend you throw it up on GitHub and move your non configurable code into a library file.

twitterfan
Bear Rating Trainee
Bear Rating Trainee
Posts: 9
Joined: 13 Jun 2013, 01:58

Re: Twitter RSS Feeds

Postby twitterfan » 14 Jun 2013, 19:56

I now took the original code from the first updated post. It still does not work. Look at my code (it is exactly the one from the first post):

Code: Select all

<?php
//v.5
$token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX;
$consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path

if (!isset( $_GET["screen_name"] ))
   $_GET["screen_name"] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
   
if (!isset( $_GET["count"] ))
   $_GET["count"] = '5';

$sn = htmlspecialchars($_GET["screen_name"]);
$cnt = htmlspecialchars($_GET["count"]);

$query = array( // query parameters
    'screen_name' => $sn,
    'count' => $cnt,
    'exclude_replies' => 'true'
);

$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, '', '&'));

$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("&amp;","&",$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);
}

print('<?xml version="1.0" encoding="utf-8"?>'. PHP_EOL);
print('<?xml-stylesheet type="text/xsl" href="atom-to-html.xsl"?>'. PHP_EOL);

print('<feed xmlns="http://www.w3.org/2005/Atom">'. PHP_EOL);
print('<title>@'.$twitter_data[0]['user']['screen_name'].'</title>'. PHP_EOL);

$arrLen = count($twitter_data);
for ($i=0; $i<$arrLen; $i++) {
   print(PHP_EOL. '   <entry>'. PHP_EOL);
      print('      <id>https://twitter.com/'.$twitter_data[$i]['user']['screen_name'].'/statuses/'. $twitter_data[$i]['id_str'] .'</id>'. PHP_EOL);
      print('      <link href="https://twitter.com/'.$twitter_data[$i]['user']['screen_name'].'/statuses/'. $twitter_data[$i]['id_str'] .'" rel="alternate" type="text/html"/>'. PHP_EOL);
      print('      <title>'.$twitter_data[$i]['user']['screen_name'].': '.$twitter_data[$i]['text'].'</title>'. PHP_EOL);
      print('      <summary type="html"><![CDATA['.$twitter_data[$i]['user']['screen_name'].': '.$twitter_data[$i]['text'].']]></summary>'. PHP_EOL);
     
      $feedContent = '      <content type="html"><![CDATA[<html><body><p></p><p>'.$twitter_data[$i]['text'].'</p></body></html>]]></content>';
      $text = processString($feedContent);
     
      print($text . PHP_EOL);
      print('      <updated>'.$twitter_data[$i]['created_at'].'</updated>'. PHP_EOL);
      print('      <author><name></name></author>'. PHP_EOL);
     
      $hashLen = count($twitter_data[$i]['entities']['hashtags']);
      if ($hashLen > 0){
         for ($j=0; $j<$hashLen; $j++){
            print('      <category term="'.$twitter_data[$i]['entities']['hashtags'][$j]['text'].'"/>'. PHP_EOL);
         }
      }
     
   print('   </entry>'. PHP_EOL);
}

print('</feed>'. PHP_EOL);
print('<!-- vim:ft=xml -->');
?>


So what is still wrong? The feeds are shown, scrooling correctly (as I said I use it for a WordPress Widget), but the links to the original posts are still wrong: ERROR on the twitter-site.

http://twitter.com/XXXXXXXXX/statuses/3 ... 902627E+17

This link (with the correct username) does not work and leads to a twitter-sitenotfound-site.

virgo
Bear Rating Trainee
Bear Rating Trainee
Posts: 37
Joined: 12 Jun 2013, 22:14

Re: Twitter RSS Feeds

Postby virgo » 14 Jun 2013, 20:35

twitterfan wrote:I now took the original code from the first updated post. It still does not work. Look at my code (it is exactly the one from the first post):


The code seems fine now.

twitterfan wrote:So what is still wrong? The feeds are shown, scrooling correctly (as I said I use it for a WordPress Widget), but the links to the original posts are still wrong: ERROR on the twitter-site.

http://twitter.com/XXXXXXXXX/statuses/3 ... 902627E+17

Are those newly downloaded articles? I don't really know, how this WordPress Widget works and when/how often it updates. And if it's on top of tt-rss, then tt-rss needs to update feeds too before the change has effect.

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 20:43

Something isn't right, might be caching, because your twitter link should be a https link now. Try clearing your cache, try in private/incognito mode, or a different browser.

If that still doesn't work try this older version.
Attachments
twitter_rss_parser_v.4.php.zip
(2.61 KiB) Downloaded 81 times

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 20:44

virgo wrote:Are those newly downloaded articles? I don't really know, how this WordPress Widget works and when/how often it updates. And if it's on top of tt-rss, then tt-rss needs to update feeds too before the change has effect.


That's a good point. Updating the code on server won't fix any items that have already been imported into tt-rss or wordpress.

jdelamater99
Bear Rating Master
Bear Rating Master
Posts: 109
Joined: 11 Apr 2013, 17:45

Re: Twitter RSS Feeds

Postby jdelamater99 » 14 Jun 2013, 21:02

cqrt wrote:
jdelamater99 wrote:Do you links look like twitter.com/USERNAME/statuses/3.452746090311E+17 ?

If that's the case, try the new version (v.5 as of right now). The code in the original post on the first page has been updated.


I'd strongly recommend you throw it up on GitHub and move your non configurable code into a library file.


good call. I'll try and get around to that at some point.


Return to “Support”

Who is online

Users browsing this forum: No registered users and 3 guests