+ Reply to Thread
Results 1 to 30 of 30

Thread: How To: Get news on your site via RSS / PHP

  1. #1
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Arrow How To: Get news on your site via RSS / PHP

    I saw a thread on here where someone needed a little guidance on how to get a news feed to show news on their site.

    This is a very basic way, it will not archive the news, but it will show it on your site, and the users wont have to leave or view through google.

    Only 1 variable needs to be changed:

    $feed_url = "http://feed.url"; // change to the feed you wish to use!

    Here is the code:

    PHP Code:
    <?
    $feed_url 
    "http://feed.url" // change to the feed you wish to use!

    class RSSParser {

       var 
    $insideitem false;
       var 
    $tag "";
       var 
    $title "";
       var 
    $description "";
       var 
    $link "";

       function 
    startElement($parser$tagName$attrs) {
           if (
    $this->insideitem) {
               
    $this->tag $tagName;
           } elseif (
    $tagName == "ITEM") {
               
    $this->insideitem true;
           }
       }

       function 
    endElement($parser$tagName) {
           if (
    $tagName == "ITEM") {

               
               print 
    "
               <b>" 
    $this->title "</b>
               <div class=\"text\">" 
    $this->description "<br /><a href=\"" $this->link "\" target=\"_blank\">Read the Rest of This Story</a></div><br />";
                
               
    $this->title "";
               
    $this->description "";
               
    $this->link "";
               
    $this->insideitem false;
               

           }
       }

       function 
    characterData($parser$data) {
           if (
    $this->insideitem) {
               switch (
    $this->tag) {
                   case 
    "TITLE":
                   
    $this->title .= $data;
                   break;
                   case 
    "DESCRIPTION":
                   
    $this->description .= $data;
                   break;
                   case 
    "LINK":
                   
    $this->link .= $data;
                   break;
               }
           }
       }
    }
        
        
    $xml_parser xml_parser_create();
        
    $rss_parser = new RSSParser();
        
    xml_set_object($xml_parser,&$rss_parser);
        
    xml_set_element_handler($xml_parser"startElement""endElement");
        
    xml_set_character_data_handler($xml_parser"characterData");
        
    $fp fopen("$feed_url","r")
           or die(
    "Error reading RSS data.");
        while (
    $data fread($fp4096))
           
    xml_parse($xml_parser$datafeof($fp))
               or die(
    sprintf("XML error: %s at line %d",  
                   
    xml_error_string(xml_get_error_code($xml_parser)),  
                   
    xml_get_current_line_number($xml_parser)));
        
    fclose($fp); 
        
        
    xml_parser_free($xml_parser);
        

    ?>
    This code is very affective, although I have found that it doesnt EVER work well with google feeds for some reason. So if anything use Yahoo over Google.

    Enjoy, and if you have any questions I'm here to answer them.
    Last edited by developmy.mobi; 11-01-2007 at 04:26 PM.

  2. #2
    Mobility Regular texasgamer definitely knows something about .mobi texasgamer definitely knows something about .mobi texasgamer's Avatar
    Join Date
    Jun 2007
    Location
    Corpus Christi Texas
    Posts
    276

    Default

    Thank you very much DevelopeMy.mobi
    Rep added!

  3. #3
    Founding Member Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman is a .mobi legend Scandiman's Avatar
    Join Date
    Jun 2007
    Location
    NorCal
    Posts
    8,933

    Default

    Thanks DM! This input is much appreciated!

  4. #4
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Default

    No problem!

  5. #5
    MobiEnthusiast coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast is a .mobi legend coast's Avatar
    Join Date
    Jul 2007
    Location
    Southern California
    Posts
    4,402

    Default

    This is great. Rep Added.

    P.S. Have you tried it and does it work with the .mobi standards?

  6. #6
    Senior Member noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1's Avatar
    Join Date
    Jul 2007
    Location
    Wales, UK
    Posts
    2,840

    Default

    How do you get the rss feed to show on your website. I am just not getting it and i am going nuts

  7. #7
    Mobility Regular bondjpf knows how to spell .mobi
    Join Date
    Sep 2007
    Posts
    105

    Default

    How can i use it on my html page?

  8. #8
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Default

    You save your HTML page as a .php page and it should work.

  9. #9
    Mobility Regular bondjpf knows how to spell .mobi
    Join Date
    Sep 2007
    Posts
    105

    Default

    So you're saying :

    Insert this code into my code and then save the page as php?

    Or create a new file with this code only?

  10. #10
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Default

    Insert it into your current page wherever you want the new feed.

  11. #11
    Mobility Regular bondjpf knows how to spell .mobi
    Join Date
    Sep 2007
    Posts
    105

    Default

    Hi,
    So What I do is insert this code into my html page and then save itqth php extension.
    Then I just need to make a link from my main page(index.html) to this new.php page ?

  12. #12
    Senior Member noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1's Avatar
    Join Date
    Jul 2007
    Location
    Wales, UK
    Posts
    2,840

    Default

    I still cant get it to work either, i tried php and html it just will not do it

  13. #13
    Mobility Regular bondjpf knows how to spell .mobi
    Join Date
    Sep 2007
    Posts
    105

    Default

    Maybe it's your host that its configurated for windows.
    I'm using godaddy windows hosting and it doeasn't work.
    I have made my site using javascript http://capitalnews.mobi .
    It doesn't work on most mobiles but i don't know how to do it withou changing hosting.
    Maybe with v3 of mobisitegalore it will work ok.

  14. #14
    Senior Member noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1's Avatar
    Join Date
    Jul 2007
    Location
    Wales, UK
    Posts
    2,840

    Default

    I use hostgator.

  15. #15
    Senior Member GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa's Avatar
    Join Date
    Jul 2007
    Location
    Nieuw-Vennep, The Netherlands
    Posts
    2,717

    Default

    Quote Originally Posted by bondjpf View Post
    Maybe it's your host that its configurated for windows.
    I'm using godaddy windows hosting and it doeasn't work.
    I have made my site using javascript http://capitalnews.mobi .
    It doesn't work on most mobiles but i don't know how to do it withou changing hosting.
    Maybe with v3 of mobisitegalore it will work ok.
    PHP can work on a windows server, but you have to inform with your hosting company which dynamic language they support (if they do...). My hosting company doesn't support php but asp(.net)...and there is no way I can use the code above on my sites...

    Martin

  16. #16
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Default

    Please note, there is supposed to be a ; after $feed_url = "your url";

    It shows up in the editor, but not on here.

  17. #17
    Senior Member noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1 is a .mobi knight noonoo1's Avatar
    Join Date
    Jul 2007
    Location
    Wales, UK
    Posts
    2,840

    Default

    Quote Originally Posted by developmy.mobi View Post
    Please note, there is supposed to be a ; after $feed_url = "your url";

    It shows up in the editor, but not on here.

    Hey it works!!! i love you i love you i love you

    It didnt work in html but it did work in php.
    Last edited by noonoo1; 11-01-2007 at 05:03 PM.

  18. #18
    Mobility Regular bondjpf knows how to spell .mobi
    Join Date
    Sep 2007
    Posts
    105

    Default

    It gives me :

    Error reading RSS data.

  19. #19
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Default

    use a Yahoo feed - google feeds tend to suck with this code.

  20. #20
    Mobility Regular pilot is quite knowledgeable about .mobi pilot is quite knowledgeable about .mobi pilot is quite knowledgeable about .mobi pilot's Avatar
    Join Date
    Jun 2007
    Posts
    772

    Default

    Got similar code for asp? thanks developmy!

  21. #21
    Senior Member developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi is very knowledgeable in all things .mobi developmy.mobi's Avatar
    Join Date
    Aug 2007
    Location
    Western Mass
    Posts
    1,070

    Default

    GijsZePa might, I'm a LAMP guy.

  22. #22
    Mobility Regular pilot is quite knowledgeable about .mobi pilot is quite knowledgeable about .mobi pilot is quite knowledgeable about .mobi pilot's Avatar
    Join Date
    Jun 2007
    Posts
    772

    Default

    GijsZePa, where you at?!

    Quote Originally Posted by developmy.mobi View Post
    GijsZePa might, I'm a LAMP guy.

  23. #23
    Senior Member GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa's Avatar
    Join Date
    Jul 2007
    Location
    Nieuw-Vennep, The Netherlands
    Posts
    2,717

    Default

    Here I am..had to go into my archives because I don't work with classic asp anymore...(try asp.net Pilot...it will blow you away)

    The rss-writer (you can publish rss with this code. Wrote this myself):

    NOTE: change the session.lcid=1043 into your own country code...

    Code:
    <%@ Language=VBScript %>
    <%option explicit%>
    <% 
    session.lcid=1043
    Response.ContentType = "text/xml"
     
    DIM adoCon, adoRec
    Function ApplyXMLFormatting(strInput)
     
      strInput = Replace(strInput,"&", "&amp;")
      strInput = Replace(strInput,"'", "&apos;")
      strInput = Replace(strInput,"""", "&quot;")
      strInput = Replace(strInput, ">", "&gt;")
      strInput = Replace(strInput,"<","&lt;")
      ApplyXMLFormatting = strInput
     
    End Function 
     
     
    Function StartConnection()
     
        Dim strCon
        strCon =     "<your connection>"
        Set adoCon = Server.CreateObject("Adodb.COnnection") 
        adoCon.Open strCon
        Set adoRec = Server.CreateObject( "ADODB.RecordSet")
        adoRec.ActiveConnection = adoCon
     
    End Function
     
    Function EndConnection()
     
        Set adoRec=Nothing
        adoCon.close
        Set adoCon=Nothing
     
    End Function
     
     
    'GetRows Method
     
    Function GetRecords(strSQL)
     
        ' getrows 
        startConnection()
        adoRec.open strSQL, adoCon
        If Not (adoRec.BOF AND adoRec.EOF) Then
            GetRecords = adoRec.GetRows()
        End If
        adoRec.Close
        EndConnection()
     
    End Function
    %>
     
     
    <?xml version="1.0" encoding="ISO-8859-1" ?> 
    <rss version="2.0"> 
    <channel> 
    <title>Mijn Rss Feed creator</title> 
    <link>http://www.mylink.nl</link> 
    <description>Mijn omschrijving</description> 
    <language>nl</language>
    <copyright>Copyright 2004 Martin Jansen. All Rights Reserved.</copyright>
    <lastBuildDate><%=Now()%></lastBuildDate>
     
    <% 
     
    'Create rows
    Dim intTeller, aArtikel, strSQL
    strSQL = "SELECT nId, dDate, cTitle, cDescription FROM myArticles ORDER BY dDate DESC"
    aArtikel = getRecords(strSQL)
     
    If isArray(aArtikel) then
        For intTeller = 0 to Ubound(aArtikel,2)
            response.write "     <item>" & VbCrLf
            response.write "        <pubDate>" & aArtikel(1,intTeller) & "</pubDate>" & vbCrLf
            response.write "        <title>" & ApplyXMLFormatting(aArtikel(2,intTeller)) & "</title>" & VbCrLf 
            response.write "        <link>http://www.mylink.nl/artikelen.asp?ID=" &  aArtikel(0,intTeller) & "</link>" & VbCrLf 
            response.write "        <description>" & ApplyXMLFormatting(aArtikel(3,intTeller)) & "</description>" & VbCrLf 
            response.write "    </item>" & VbCrLf 
        Next 
     
    End If
     
    %> 
    </channel> 
    </rss>
    And this is a reader method (grabbed this from the net):

    Code:
    <%
    response.ContentType="text/html"
    dim objXML, objXSL
    set objXML=server.CreateObject("MSXML2.DOMDocument")
    set objXSL=server.CreateObject("MSXML2.DOMDocument")
    objXML.async=False
    objXSL.async=False
    objXML.setProperty "ServerHTTPRequest",true
    objXML.load http://thefeed.com
    objXSL.load Server.MapPath("rssStyle.xslt")
    response.write "<html><head><title>RSS Feed Reader</title></head><body>"
    response.write objXML.transformNode(objXSL)
    response.write "</body></html>"
    set objXML=nothing
    set objXSL=nothing
    %>

    And this is the XSLT file (named rssStyle.xslt)
    Code:
    <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" omit-xml-declaration="yes" /> <xsl:template match="/rss/channel"> <b><xsl:value-of select="title" disable-output-escaping="yes" /></b> <xsl:for-each select="item"> <li> <a> <xsl:attribute name="href"> <xsl:value-of select="link" /> </xsl:attribute> <xsl:attribute name="target">_blank</xsl:attribute> <xsl:value-of select="title" disable-output-escaping="yes" /> </a> (<xsl:value-of select="pubDate" />) </li> </xsl:for-each> </xsl:template> </xsl:stylesheet>
    For customizing the Feed layout simply change the XSLT file!

    Martin
    Last edited by GijsZePa; 11-02-2007 at 10:10 AM. Reason: small note

  24. #24
    Senior Member GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa's Avatar
    Join Date
    Jul 2007
    Location
    Nieuw-Vennep, The Netherlands
    Posts
    2,717

    Default

    Oh...and for the asp.net programmers...just follow this link...cool toolkit:

    http://www.codeplex.com/ASPNETRSSToolkit

    Martin

  25. #25
    Mobility Regular bondjpf knows how to spell .mobi
    Join Date
    Sep 2007
    Posts
    105

    Default

    Can you tell me where do I put the first code?
    Thanks

  26. #26
    Senior Member GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa's Avatar
    Join Date
    Jul 2007
    Location
    Nieuw-Vennep, The Netherlands
    Posts
    2,717

    Default

    Quote Originally Posted by bondjpf View Post
    Can you tell me where do I put the first code?
    Thanks
    Well..where you want in your site...

    example: cut and past the code into a new asp file: for example articles.asp in your root: www.site.com/articles.asp

    People can use that url as RSS source in their readers or websites..

    Martin

  27. #27
    Mobility Regular italiandragon knows how to spell .mobi italiandragon's Avatar
    Join Date
    Jul 2007
    Location
    Australia
    Posts
    186

    Default

    does this work for .com sites too ?

    Thanks

  28. #28
    Senior Member GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa is a .mobi master GijsZePa's Avatar
    Join Date
    Jul 2007
    Location
    Nieuw-Vennep, The Netherlands
    Posts
    2,717

    Default

    Quote Originally Posted by italiandragon View Post
    does this work for .com sites too ?

    Thanks
    It works on every site..but depends on your platform.
    You have to check with your hosting company if or which dynamic platform they support..

    Martin

  29. #29
    Mobility Member SerpaDesigns knows how to spell .mobi
    Join Date
    Dec 2007
    Location
    Northern California USA
    Posts
    30

    Default

    I have some php code that is small, very easy to put in your code, it uses require once to get the rest of the code from another file (the read rss code) and then displays it (display code). The page that has the display code has to be php5, the other (read) file can be php4 or 5, let me know if you'd like the code...


    Oh PS

    to check it out in action see:
    WorldAfterOil.com

  30. #30
    First time poster! Vahooli knows how to spell .mobi
    Join Date
    Apr 2008
    Posts
    1

    Default

    hi.

    im not very advanced in PHP.can anyone teach me how can i edit this code in private message (for example for this RSS : (news.parseek.ir/rss )? or say any site too convert RSS to HTML?

+ Reply to Thread

Similar Threads

  1. BBC NEWS/ The Whole Of The Net In Your Hand
    By Binaryman in forum News & Discussion
    Replies: 6
    Last Post: 10-01-2007, 07:48 PM
  2. MORE .MOBI NEWS
    By Binaryman in forum News & Discussion
    Replies: 0
    Last Post: 09-08-2007, 05:21 PM
  3. RxD.mob making news :)
    By thebiffenator in forum News & Discussion
    Replies: 8
    Last Post: 08-31-2007, 03:47 AM
  4. How to add News to your site?
    By texasgamer in forum Mobile Web Design
    Replies: 8
    Last Post: 08-06-2007, 04:40 PM
  5. All this news about BofA has got me going!
    By noonoo1 in forum News & Discussion
    Replies: 23
    Last Post: 07-19-2007, 04:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
A Yup.mobi Mobile Site
Mobile Dating