Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post

    Unhappy RSS Feed Won't Display

    Hi Everyone,

    I'm trying to pull an rss feed from twitter and put it on my website. I have code that seems to work so far but I can only get it to display outside of my html. I can't seem to figure out how to output the array in my html. I keep getting the error:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/77/7650177/html/index.php on line 138

    Here is the code I'm working with:
    Code:
    <?php
    	
    	
    	
    function fetch_tweets($username, $maxtweets) {
         //Using simplexml to load URL
         $tweets = simplexml_load_file("http://twitter.com/statuses/user_timeline/" . $username . ".rss");
    
         $tweet_array = array();  //Initialize empty array to store tweets
         foreach ( $tweets->channel->item as $tweet ) { 
              //Loop to limitate nr of tweets.
              if ($maxtweets == 0) {
                   break;
              } else {
                   $twit = $tweet->description;  //Fetch the tweet itself
    
                   //Remove the preceding 'username: '
                   $twit = substr(strstr($twit, ': '), 2, strlen($twit));
    
                   // Convert URLs into hyperlinks
                   $twit = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">\\0</a>", $twit);
    
                   // Convert usernames (@) into links 
                   $twit = preg_replace("(@([a-zA-Z0-9\_]+))", "<a href=\"http://www.twitter.com/\\1\">\\0</a>", $twit);
    
                   // Convert hash tags (#) to links 
                   $twit = preg_replace('/(^|\s)#(\w+)/', '\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>', $twit);
    
                   //Specifically for non-English tweets, converts UTF-8 into ISO-8859-1
                   $twit = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $twit);
    
                   //Get the date it was posted
                   $pubdate = strtotime($tweet->pubDate); 
                   $propertime = gmdate('F jS Y, H:i', $pubdate);  
    
                   //Store tweet and time into the array
                   $tweet_item = array(
                         'desc' => $twit,
                         'date' => $propertime,
                   );
                   array_push($tweet_array, $tweet_item);
    
                   $maxtweets--;
              }
         }
         //Return array
         return $tweet_array;
    }
    
    
    
    $content = <<<EOD
    	
    	<div id = "homeContent">
    
    <?php
    
    $mytweets = fetch_tweets('kalilaks', 3);
    
    echo '<ul class="twitter-updates">';
    foreach ($mytweets as $k => $v) {
         echo '<li>';
         echo '<span class="update">' .$v['desc']. '</span>';
         echo '<span class="date">' .$v['date']. '</span>';
         echo '</li>';
    }
    echo '</ul>';
    ?>
    
    
    </div>
    	
    EOD;
    
    ?>

    I'm 99% sure the issue is located in the $content = <<<EOD ... EOD; area and that it has to do with the fact that I am putting php code into html but I can't figure out a workaround. I tried to manipulate the code to the following but still got errors:

    Code:
    <ul class="twitter-updates"><?php foreach($mytweets as $k => $v) { ?><li> <span class="update"><?$v['desc']}?></span></li></ul>
    I also tried to come up with the following without success:
    Code:
    <ul class="twitter-updates"><?php foreach($mytweets as $k => $v) { '. <li> <span class="update"> . '<?$v['desc']}?></span></li></ul>
    Any help would be greatly appreciated!! Thank you!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Try something like this ....

    PHP Code:
    <?php
        
    function fetch_tweets($username$maxtweets) {
         
    //Using simplexml to load URL
         
    $tweets simplexml_load_file("http://twitter.com/statuses/user_timeline/" $username ".rss");

         
    $tweet_array = array();  //Initialize empty array to store tweets
         
    foreach ( $tweets->channel->item as $tweet ) { 
              
    //Loop to limitate nr of tweets.
              
    if ($maxtweets == 0) {
                   break;
              } else {
                   
    $twit $tweet->description;  //Fetch the tweet itself

                   //Remove the preceding 'username: '
                   
    $twit substr(strstr($twit': '), 2strlen($twit));

                   
    // Convert URLs into hyperlinks
                   
    $twit preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/""<a href=\"\\0\">\\0</a>"$twit);

                   
    // Convert usernames (@) into links 
                   
    $twit preg_replace("(@([a-zA-Z0-9\_]+))""<a href=\"http://www.twitter.com/\\1\">\\0</a>"$twit);

                   
    // Convert hash tags (#) to links 
                   
    $twit preg_replace('/(^|\s)#(\w+)/''\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>'$twit);

                   
    //Specifically for non-English tweets, converts UTF-8 into ISO-8859-1
                   
    $twit iconv("UTF-8""ISO-8859-1//TRANSLIT"$twit);

                   
    //Get the date it was posted
                   
    $pubdate strtotime($tweet->pubDate); 
                   
    $propertime gmdate('F jS Y, H:i'$pubdate);  

                   
    //Store tweet and time into the array
                   
    $tweet_item = array(
                         
    'desc' => $twit,
                         
    'date' => $propertime,
                   );
                   
    array_push($tweet_array$tweet_item);

                   
    $maxtweets--;
              }
         }
         
    //Return array
         
    return $tweet_array;
    }

    ?>

        
    <div id = "homeContent">

    <?php

    $mytweets 
    fetch_tweets('kalilaks'3);

    echo 
    '<ul class="twitter-updates">';
    foreach (
    $mytweets as $k => $v) {
         echo 
    '<li>';
         echo 
    '<span class="update">' .$v['desc']. '</span>';
         echo 
    '<span class="date">' .$v['date']. '</span>';
         echo 
    '</li>';
    }
    ?>

    </ul>
    </div>

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    mlseim,

    The option you provided above does not seem to be working for my page. While the feed displays, I am only seeing the html. I think it is because my site is built in php and based on includes files and includes templates. If I separate the php from the html, then none of the includes files get applied (such as my css, js, nav, footer, ect). It seems I have to keep the html inside the php but somehow further insert the php code necessary for displaying the array into the html.

    Any ideas?

  • #4
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    OK, so I figured out that if I include this bit in my PHP...

    PHP Code:
    $mytweets fetch_tweets('synchronyhc'3);

    foreach(
    $mytweets as $k => $v) {
      
    $loopResult .= $v['desc'].$v['date'];

    ...and put $loopResult in my html, I get an output of my rss feed in my html!

    Now, the only problem now is that I can't format each post. They all sort of run together.

    But if I try to put any sort of <li> or <span> tags I get errors, for example:

    PHP Code:
    $loopResult .= <span class="tweet">$v['desc'].$v['date']</span>; 
    or when I try messing with the single/double quotes..
    PHP Code:
    $loopResult .= '<span class="tweet">'$v['desc'].$v['date']'</span>'
    Every attempt to get <span> or <li> tags in there is giving me errors.

    Does anyone know how write this line of code a better way so that each loop result gets wrapped in a <span> or <li> tag (so that they can be spread apart and stylized)?

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    $loopResult .= "<span class='tweet'>".$v['desc'].$v['date']."</span>";

  • #6
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    I swear I thought I tried all of the possible options but yours worked! Thanks!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •