943,106 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 4599
  • PHP RSS
Feb 4th, 2010
0

PHP script to post feeds on Twitter

Expand Post »
Hi!
I have a PHP script I've downloaded from somewhere (don't remember where, but the developers link is dead) and I want to modify it. Actually I want an extra page.
The script has 2 files, tw.php and parse.php. In tw.php you should enter your twitter account (user+pass) and the feed you want to publish on twitter. That's it, just open it in the browser.
This is very useful for me, but... I want to share it with my site's visitors, so I need a third page where people can submit their informations (twitter, pass, feed) to publish their feed on their sites. A form, basically, nothing more. But since I don't know PHP I cannot do it myself.
So, can someone, please, make the third page for me or modify the tw.php in order to allow it to do that?
Thank you very much! if is not possible, I hope at least it will be useful to others.
TW.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. /*
  3. RSS to Twitter v0.1
  4. by paul stamatiou
  5. of http://paulstamatiou.com
  6. based on code from
  7. http://morethanseven.net/posts/posting-to-twitter-using-php
  8. */
  9. include('parse.php');
  10. $uname = '';//example "blah" for twitter.com/blah, or your email address
  11. $pwd = '';
  12.  
  13. $twitter_url = 'http://twitter.com/statuses/update.xml';
  14. $feed = ""; //the feed you want to micro-syndicate
  15. $rss = new lastRSS;
  16. if ($rs = $rss->get($feed)){
  17. $title = $rs[items][0][title];
  18. $url = $rs[items][0][link];
  19. } else { die('Error: RSS file not found, dude.'); }
  20. $tiny_url = file_get_contents("http://tinyurl.com/api-create.php?url=" . $url);
  21. $status = $title . " " . $tiny_url;
  22. echo $status; //just for status if you are directly viewing the script
  23. $curl_handle = curl_init();
  24. curl_setopt($curl_handle,CURLOPT_URL,"$twitter_url");
  25. curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  26. curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  27. curl_setopt($curl_handle,CURLOPT_POST,1);
  28. curl_setopt($curl_handle,CURLOPT_POSTFIELDS,"status=$status");
  29. curl_setopt($curl_handle,CURLOPT_USERPWD,"$uname:$pwd");
  30. $buffer = curl_exec($curl_handle);
  31. curl_close($curl_handle);
  32. if (empty($buffer)){echo '<br/>message';}else{echo '<br/>success';}?>
Parse.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. /*
  3.  ======================================================================
  4.  lastRSS 0.9.1
  5.  
  6.  Simple yet powerfull PHP class to parse RSS files.
  7.  
  8.  by Vojtech Semecky, webmaster @ webdot . cz
  9.  
  10.  Latest version, features, manual and examples:
  11.   http://lastrss.webdot.cz/
  12.  
  13.  ----------------------------------------------------------------------
  14.  LICENSE
  15.  
  16.  This program is free software; you can redistribute it and/or
  17.  modify it under the terms of the GNU General Public License (GPL)
  18.  as published by the Free Software Foundation; either version 2
  19.  of the License, or (at your option) any later version.
  20.  
  21.  This program is distributed in the hope that it will be useful,
  22.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24.  GNU General Public License for more details.
  25.  
  26.  To read the license please visit http://www.gnu.org/copyleft/gpl.html
  27.  ======================================================================
  28. */
  29.  
  30. /**
  31. * lastRSS
  32. * Simple yet powerfull PHP class to parse RSS files.
  33. */
  34.  
  35. class lastRSS {
  36. // -------------------------------------------------------------------
  37. // Public properties
  38. // -------------------------------------------------------------------
  39. var $default_cp = 'UTF-8';
  40. var $CDATA = 'nochange';
  41. var $cp = '';
  42. var $items_limit = 0;
  43. var $stripHTML = False;
  44. var $date_format = '';
  45.  
  46. // -------------------------------------------------------------------
  47. // Private variables
  48. // -------------------------------------------------------------------
  49. var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs');
  50. var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source');
  51. var $imagetags = array('title', 'url', 'link', 'width', 'height');
  52. var $textinputtags = array('title', 'description', 'name', 'link');
  53.  
  54. // -------------------------------------------------------------------
  55. // Parse RSS file and returns associative array.
  56. // -------------------------------------------------------------------
  57. function Get ($rss_url) {
  58. // If CACHE ENABLED
  59. if ($this->cache_dir != '') {
  60. $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url);
  61. $timedif = @(time() - filemtime($cache_file));
  62. if ($timedif < $this->cache_time) {
  63. // cached file is fresh enough, return cached array
  64. $result = unserialize(join('', file($cache_file)));
  65. // set 'cached' to 1 only if cached file is correct
  66. if ($result) $result['cached'] = 1;
  67. } else {
  68. // cached file is too old, create new
  69. $result = $this->Parse($rss_url);
  70. $serialized = serialize($result);
  71. if ($f = @fopen($cache_file, 'w')) {
  72. fwrite ($f, $serialized, strlen($serialized));
  73. fclose($f);
  74. }
  75. if ($result) $result['cached'] = 0;
  76. }
  77. }
  78. // If CACHE DISABLED >> load and parse the file directly
  79. else {
  80. $result = $this->Parse($rss_url);
  81. if ($result) $result['cached'] = 0;
  82. }
  83. // return result
  84. return $result;
  85. }
  86.  
  87. // -------------------------------------------------------------------
  88. // Modification of preg_match(); return trimed field with index 1
  89. // from 'classic' preg_match() array output
  90. // -------------------------------------------------------------------
  91. function my_preg_match ($pattern, $subject) {
  92. // start regullar expression
  93. preg_match($pattern, $subject, $out);
  94.  
  95. // if there is some result... process it and return it
  96. if(isset($out[1])) {
  97. // Process CDATA (if present)
  98. if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag)
  99. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  100. } elseif ($this->CDATA == 'strip') { // Strip CDATA
  101. $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>''));
  102. }
  103.  
  104. // If code page is set convert character encoding to required
  105. if ($this->cp != '')
  106. //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]);
  107. $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]);
  108. // Return result
  109. return trim($out[1]);
  110. } else {
  111. // if there is NO result, return empty string
  112. return '';
  113. }
  114. }
  115.  
  116. // -------------------------------------------------------------------
  117. // Replace HTML entities &something; by real characters
  118. // -------------------------------------------------------------------
  119. function unhtmlentities ($string) {
  120. // Get HTML entities table
  121. $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
  122. // Flip keys<==>values
  123. $trans_tbl = array_flip ($trans_tbl);
  124. // Add support for &apos; entity (missing in HTML_ENTITIES)
  125. $trans_tbl += array('&apos;' => "'");
  126. // Replace entities by values
  127. return strtr ($string, $trans_tbl);
  128. }
  129.  
  130. // -------------------------------------------------------------------
  131. // Parse() is private method used by Get() to load and parse RSS file.
  132. // Don't use Parse() in your scripts - use Get($rss_file) instead.
  133. // -------------------------------------------------------------------
  134. function Parse ($rss_url) {
  135. // Open and load RSS file
  136. if ($f = @fopen($rss_url, 'r')) {
  137. $rss_content = '';
  138. while (!feof($f)) {
  139. $rss_content .= fgets($f, 4096);
  140. }
  141. fclose($f);
  142.  
  143. // Parse document encoding
  144. $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content);
  145. // if document codepage is specified, use it
  146. if ($result['encoding'] != '')
  147. { $this->rsscp = $result['encoding']; } // This is used in my_preg_match()
  148. // otherwise use the default codepage
  149. else
  150. { $this->rsscp = $this->default_cp; } // This is used in my_preg_match()
  151.  
  152. // Parse CHANNEL info
  153. preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
  154. foreach($this->channeltags as $channeltag)
  155. {
  156. $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
  157. if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
  158. }
  159. // If date_format is specified and lastBuildDate is valid
  160. if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
  161. // convert lastBuildDate to specified date format
  162. $result['lastBuildDate'] = date($this->date_format, $timestamp);
  163. }
  164.  
  165. // Parse TEXTINPUT info
  166. preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
  167. // This a little strange regexp means:
  168. // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
  169. if (isset($out_textinfo[2])) {
  170. foreach($this->textinputtags as $textinputtag) {
  171. $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
  172. if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
  173. }
  174. }
  175. // Parse IMAGE info
  176. preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
  177. if (isset($out_imageinfo[1])) {
  178. foreach($this->imagetags as $imagetag) {
  179. $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
  180. if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
  181. }
  182. }
  183. // Parse ITEMS
  184. preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
  185. $rss_items = $items[2];
  186. $i = 0;
  187. $result['items'] = array(); // create array even if there are no items
  188. foreach($rss_items as $rss_item) {
  189. // If number of items is lower then limit: Parse one item
  190. if ($i < $this->items_limit || $this->items_limit == 0) {
  191. foreach($this->itemtags as $itemtag) {
  192. $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
  193. if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
  194. }
  195. // Strip HTML tags and other bullshit from DESCRIPTION
  196. if ($this->stripHTML && $result['items'][$i]['description'])
  197. $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
  198. // Strip HTML tags and other bullshit from TITLE
  199. if ($this->stripHTML && $result['items'][$i]['title'])
  200. $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
  201. // If date_format is specified and pubDate is valid
  202. if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
  203. // convert pubDate to specified date format
  204. $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
  205. }
  206. // Item counter
  207. $i++;
  208. }
  209. }
  210.  
  211. $result['items_count'] = $i;
  212. return $result;
  213. }
  214. else // Error in opening return False
  215. {
  216. return False;
  217. }
  218. }
  219. }
  220. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
wattaman is offline Offline
90 posts
since May 2009
Feb 4th, 2010
0
Re: PHP script to post feeds on Twitter
You want people to give you their twitter login details? Why? Personally, I would never use such a service as it is a cobbled solution and therefore open to security issues. Unless it was similar to a 'Facebook Connect'.
You use cURL, so it should be ok-ish, but I wouldn't trust it. Somebody providing this sort of functionality could be into id theft - sending info to their DB before logging the user on to twitter.
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,620 posts
since Oct 2006
Feb 4th, 2010
0
Re: PHP script to post feeds on Twitter
OK, perhaps I was a bit harsh. How about a form that allows up to 140 characters in a textarea:

Send the form - the post variable validates (and cleans if necessary) and redirects (via header) to something like:


PHP Syntax (Toggle Plain Text)
  1. $input = stripslashes($_POST['tweet']);
  2. header("Location=http://twitter.com/home?status=$input");

THis *should* work if the user is logged in to twitter.
Last edited by ardav; Feb 4th, 2010 at 7:38 pm.
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,620 posts
since Oct 2006
Feb 7th, 2010
1
Re: PHP script to post feeds on Twitter
Thanks, but I don't need it. I can do what you wrote using only a form a javascript code, but what I want is (you haven't tried the script, haven't you?!) is to allow anybody to submit their feeds into their twitter accounts. I don't care about their passwords, actually if the autentification can be done through the twitter's api it would be better for me, too... but all I could find is this script and I don't know to modify it for my needs. I'm using it for myself now as a cron and it works perfect.

About the privacy, don't worry about twitter , worry about Google's decision to share its user information with the government agencies under the pretext of China's hacking attack. That's a dumb reason, IMHO. Next step is a CPU inserted in our ass to track us everywhere we go - that's the privacy you should worry about.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
wattaman is offline Offline
90 posts
since May 2009
Feb 7th, 2010
0

Hi, I thik below link will be useful for you.

http://forum.developeritonline.com/viewtopic.php?f=6&t=3
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amitsingh0542 is offline Offline
1 posts
since Feb 2010
Feb 7th, 2010
0
Re: PHP script to post feeds on Twitter
Its also worth noting that API-password authentication is being phased out over the next 18 months so you are much better off setting up an application using the o-auth API
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Feb 7th, 2010
0
Re: PHP script to post feeds on Twitter
http://forum.developeritonline.com/viewtopic.php?f=6&t=3
This looks better, but how do I make it a form so anyone can submit his/hers own thoughts?... in their accounts.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
wattaman is offline Offline
90 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: zip or rar a folder to download
Next Thread in PHP Forum Timeline: update links are not shown inside the table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC