Search Unity

Reading from a RSS feedin Unity

Discussion in 'Multiplayer' started by ChisomoMbeza, Jun 14, 2017.

  1. ChisomoMbeza

    ChisomoMbeza

    Joined:
    Jun 14, 2017
    Posts:
    15
    I am trying to make a news feed in my unity program that reads from an RSS feed and once received, each item can be separated and added to the UI using GUI. I managed to set up a local feed using PHP and I was using xamp to host the site locally.

    Here is the PHP code:

    Code (CSharp):
    1. <?php
    2.     echo '<?xml version="1.0" encoding="UTF-8" ?>'
    3. ?>
    4.  
    5. <?php
    6.     $servername = "localhost";
    7.     $ServerUsername = "root";
    8.     $ServerPassword = "";
    9.     $dbName = "up_db";
    10.  
    11.     $db = new mysqli($servername, $ServerUsername, $ServerPassword, $dbName);
    12.  
    13.     //Get limit, if it hasn't been defined pr has beem defined as above 30 then set it as 10
    14.     $limit = (isset($_GET['limit']) === true && (int)$_GET['limit'] <= 30 && (int)$_GET['limit'] != 0) ? (int)$_GET['limit'] : 10;
    15.  
    16.     $query = "SELECT id, title, content, timestamp FROM posts ORDER BY timestamp DESC LIMIT $limit";
    17.  
    18.     $res = mysqli_query($db, $query) or die(mysqli_error($db));
    19.  
    20.     if (mysqli_num_rows($res) > 0)
    21.     {
    22.      ?>
    23.     <rss version="2.0">
    24.     <channel>
    25.       <title>UP News</title>    
    26.       <link>https://http://www.up.ac.za</link>
    27.      <description>The news feed</description>
    28.      <language>en-us</language>
    29.    
    30.       <?php
    31.        while ($row = mysqli_fetch_assoc($res))
    32.        {
    33.           ?>
    34.           <item>
    35.               <title><?php echo $row['title']; ?></title>
    36.             <description><?php echo $row['content']; ?></description>
    37.             <link>https://http://www.up.ac.za</link>
    38.             <pubDate><?php echo date('r', $row['timestamp']); ?></pubDate>
    39.             <guid> https://http://www.up.ac.za<?php echo "/" .$row['title']; ?></guid>
    40.           </item>
    41.           <?php
    42.         }
    43.       ?>
    44.     </channel>
    45.  
    46.     </rss>
    47.  
    48.     <?php
    49.     }
    50.     else
    51.     {
    52.         echo "Nothing to show";
    53.     }
    54.     ?>
    and here is the c# code:
    Code (CSharp):
    1.  string myText;
    2.     public UnityEngine.UI.Text newsFeed;
    3.     public string myNewsURL;
    4.     public string noNewsText;
    5.  
    6.  
    7.  
    8.     void Start()
    9.     {
    10.         StartCoroutine(GetNews());
    11.     }
    12.    
    13.     private IEnumerator GetNews()
    14.     {
    15.         WWW feed = new WWW(myNewsURL);
    16.         yield return feed;
    17.         myText = feed.text;
    18.         newsFeed.text = myText;
    19.  
    20.         newsFeed.text = " ";
    21.  
    22.         if (feed.isDone)
    23.         {
    24.             XmlDocument xmlDoc = new XmlDocument();
    25.             xmlDoc.LoadXml(feed.text);
    26.  
    27.             XmlNodeList xmlNodeList = xmlDoc.SelectNodes("rss/channel/item");
    28.  
    29.  
    30.             foreach (XmlNode _xmlNode in xmlNodeList)
    31.             {
    32.                 XmlNode title = _xmlNode.SelectSingleNode("title");
    33.                 XmlNode descriptionNode = _xmlNode.SelectSingleNode("description");
    34.  
    35.                 newsFeed.text += "\r\n" + "Title : " + title.InnerText + "\r\n"
    36.                                 + "Content : " + descriptionNode.InnerText + "\r\n";
    37.  
    38.             }
    39.         }
    40.  
    41.         if (newsFeed.text == "")
    42.         {
    43.             newsFeed.text = noNewsText;
    44.         }
    45.     }
    this iterated through the feed and returned every item separately but when I put it online the feed not only returns just one result but it returns the pure php code instead of the result. I have looked everywhere online and tried everything including replacing all the symbols like "" ' > < with alternatives like &quot; and soon... please help
     
  2. KingfisherWyvernStudio

    KingfisherWyvernStudio

    Joined:
    Oct 5, 2011
    Posts:
    324
    Sorry, I have no solution for you but I am following this, since it sure sounds interesting to include in my own game :)
     
  3. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    I have the feeling you have a problem with the hosting it self, can you access the feed correctly from your browser or any other client than your Unity client? because the way you worded it means the hosting is returning the file instead of executing it. You probably want to setup WAMP or LAMP depending on your OS (Windows / Linux), that's the fastest solution I know, but there are probably other ways to host PHP files.
     
  4. ChisomoMbeza

    ChisomoMbeza

    Joined:
    Jun 14, 2017
    Posts:
    15
    I hosted locally on xamp but once I put it on the internet that's when it doesn't return the correct stuff. But you could be right, it might be on the hosting side
     
  5. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Since it worked locally then the problem is likely in hosting, are you sure you setup a PHP hosting not a simple file hosting??
     
  6. ChisomoMbeza

    ChisomoMbeza

    Joined:
    Jun 14, 2017
    Posts:
    15
    The thing is other functions like a login and register from unity are working which means the PHP code being hosted is running properly and actually accessing the database and performing the correct functions.

    I have been looking at this asset: https://www.assetstore.unity3d.com/en/#!/content/1064 which seems to be exactly what I need, because the problem could be in how I am coding my PHP.