PHP не знаю, знаю кучу других языков программирования.
Для меня он похож на С++.
Был сайт... Заброшенный... Решил его снова задействовать. Вот он.
Я захотел вставить в свой сайт данные с блога на яндексе.
Для этого решил использовать RSS. Путь к нему такой: http://vitaliytokarev[dot]ya[dot]ru/rss/posts[dot]xml
Так как просто так RSS не вставить, я решил, что пора свою страничку переделывать с HTML на PHP. Нашёл парсер RSS, сделал отдельный файл "PARSER.PHP" и добавил его вызов в страничку сайта.
Там были мелкие ошибки. Изучив матчасть и некоторые функции я их исправил и немного допилил парсер. Всё заработало, но ставка идёт в неправильной кодировке.
Я нашёл функцию iconv() и попробовал изменить кодировку с её помощью, но не получилось. Теперь данные для вывода не находятся. Что я делаю не так?
Функцию использую так: iconv("UTF-8", "WIN-1251", curl_exec($ch));
Вот текст парсера:
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class rssReader {
- var $rssFeeds;
- // Class constructor
- function rssReader() {
- // Here are the feeds - you can add to them or change them
- 0 => "http://vitaliytokarev.ya.ru/rss/posts.xml",
- //1 => "Второй фид",
- //2 => "Третий",
- //3 => "Четвертый",
- //4 => "Пятый и.т.д",//По аналогии копируете строчку и вставляете сколько угодно ссылок на фиды
- );
- }
- function checkCache($rss_url) {
- $ttl = 60*60;// 60 secs/min for 60 minutes = 1 hour(360 secs)
- {
- //$feed = file_get_contents($cachefilename);
- //TVA vvv
- //$feed = iconv("UTF-8", "WIN-1251", curl_exec($ch));
- //TVA ^^^
- }
- else
- {
- //$feed = file_get_contents($rss_url);
- //TVA vvv
- //$feed = iconv("UTF-8", "WIN-1251", curl_exec($ch));
- //TVA ^^^
- // $fp = fopen($cachefilename, 'w');
- //TVA vvv
- //$data = iconv("UTF-8", "WIN-1251", curl_exec($ch));
- //TVA ^^^
- //$xmldata = split("\n",$xmldata);
- //foreach ($xmldata as $data);
- //fwrite($data, $feed);
- //fclose($data);
- }
- return $feed;
- }
- //
- // Creates HTML from a FeedID in the array
- // it makes $howmany entries
- //
- function createHtmlFromFeed($feedid, $howmany) {
- // Now we make sure that we have a feed selected to work with
- $rss_url = $this->rssFeeds[$feedid];
- $this->createHtmlFromRSSUrl( $rss_url, $howmany );
- }
- //
- // Create HTML from an RSS URL
- // it makes $mowmany entires
- //
- function createHtmlFromRSSUrl( $rss_url, $howmany )
- {
- // Now we get the feed and cache it if necessary
- $rss_feed = $this->checkCache($rss_url);
- // Now we replace a few things that may cause problems later
- // If there is an image node remove it, we aren't going to use
- // it anyway and it often contains a <title> and <link>
- // that we don't want to match on later.
- // Now we get the nodes that we're interested in
- //
- // Now that the RSS/XML is parsed.. Lets Make HTML !
- //
- // If there is not at least one title, then the feed was empty
- // it happens sometimes, so lets be prepared and do something
- // reasonable
- {
- echo "No news at present, please check back later.<br><br>";
- }
- else
- {
- // OK Here we go, this is the fun part
- // Well do up the top 3 entries from the feed
- for ($counter = 1; $counter <= $howmany; $counter++ )
- {
- // We do a reality check to make sure there is something we can show
- {
- // Then we'll make a good faith effort to make the title
- // valid HTML
- // The description often has encoded HTML entities in it, and
- // we probably don't want these, so we'll decode them
- // Now we make a pretty page bit from the data we retrieved from
- // the RSS feed. Remember the function FormatRow from the
- // beginning of the program ? Here we put it to use.
- $row = $this->FormatEntry($title[$counter][1],$description[$counter][1],$link[$counter][1]);
- // And now we'll output the new page bit!
- echo $row;
- }
- }
- }
- }
- function FormatEntry($title, $description, $link) {
- return <<<HTML
- <p class="feed_title">{$title}</p>
- <p class="feed_description">{$description}</p>
- <a class="feed_link" href="{$link}" rel="nofollow" target="_blank">Read more...</a>
- <p> </p>
- <hr size=1>
- HTML;
- }
- function GetrssFeeds() {
- return $this->rssFeeds;
- }
- function SetrssFeeds($rssFeeds) {
- $this->rssFeeds = $rssFeeds;
- }
- }// END OF THE CLASS
- ?>