Rss with image: $image value

Ask about a PHP problem here.
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Rss with image: $image value

Post by hostingebc »

I have 2 problems:

1. Image in rss reader.
I manage to read the image url, and to show it, but I have a problem when I read a variety of RSS feeds. the best you will see the problem on the link below

http://moravaprom.com/ebchost/

vait to open all rss source to the end, and then look at the latest to look at the end. in the latest rss reading I have all the same images an the end of rss item. I tried to solve this problem with $urlslike=""; (fortranslation $urlslike is $image... ) but with no success

2. speed of the code execution of
As you have seen, is slow is executed.
Note: This page was only for administrators, and not for visitors.

hiar is the code
Code:

<?php
header('Content-Type: text/html; charset=utf-8');
    $konekcija = mysql_connect("nesto", "nesto", "nesto") or die ("Parametri za povezivanje sa bazom nisu dobri, ili baza ne postoji!");
    $baza = mysql_select_db("nesto", $konekcija) or die ("Na zalost, server nije u mogucnosti da pronadje bazu podataka!");
    
    $sajtovi = mysql_query ("SELECT * FROM rss_sajtovi") or die(mysql_error());
    while($row = mysql_fetch_array( $sajtovi ))
        {
            $id_sajta = $row['ID_SAJTA'];
            $naziv = $row['NAZIV'];
            $url = $row['URL'];
            $rss = $row['RSS'];
            $kategorija = $row['kategorija'];
            echo "<h1>$id_sajta / $naziv / $url/ $rss / $kategorija </h1><hr></br>";
                $rss = new SimpleXMLElement($rss, null, true);
        
                foreach($rss->xpath('channel/item') as $item)
                    {
                        $dom = new DOMDocument();
                             @$dom->loadHTMLFile($item->link);
                             $xpath = new DOMXPath($dom);
                             $entries = $xpath->query('//div[@class="img"]//img');
                             
                             foreach($entries as $e)
                             $image = "<img src='".$e->getAttribute("src")."'alt='' width='150'/>";
                             
                             $naslov = $item->title;
                        $opis = $item->description;
                        $sadrzaj = $item->content;
                        $izvor = "<a href='{$item->link}'>{$rss->channel->title}</a>";
                        $url = $item->link;
                        $datum1 = $item->pubDate;
                        $datum2 = $item->pubdate;
                        $urlslike = $e->getAttribute("src");
                        echo "$naslov, $opis, $sadrzaj, $izvor,, $url, $datum1, $datum2, <img src='$urlslike'/> </br></br>";
                        $urlslike = "";

                            // this code is turn of for writing in database
                           //$sql="INSERT INTO rss_vesti VALUES('','$id_sajta','$naslov','$urlslike','$opis','$url','$datum2')";
                           //mysql_query($sql,$konekcija);
                
                    }
        }
?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

hostingebc wrote: vait to open all rss source to the end, and then look at the latest to look at the end. in the latest rss reading I have all the same images an the end of rss item.
I don't see the same image more than once :? Where should I be looking ?
hostingebc wrote:As you have seen, is slow is executed.
The slowness is most likely caused by the downloading of all the rss feeds, if that is the case it's pretty hard to do anything about. How slow is the page ?
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

Now i delete 3 rss link, an now i have 2 rss link

try now, you will takes about 10 to 15 seconds to load all

then you will clearly see everything.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

I see the same thing I saw last time :?

Should I be doing something ? or just visiting the page ?
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

Only visit

I tried on 10 computers, is same

You can see in picture problem i talking about.
Attachments
rssproblem.jpg
rssproblem.jpg (104.54 KiB) Viewed 2393 times
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

Okay I was on the wrong page, I was looking at index.php not main.php.

I don't see anything in the code that would cause the same image to appear more than once, are you sure the feed does not actually have the same image more than once ?

And as for the other images (the ones with the blue arrows) it's possible that they are just used in the article body, if that is the case the only way to remove the would be using the strip_tags() function which will remove anything that looks like a html tag from a string.
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

for red line picture, the rss source is http://www.naslovi.net/rss/sport/
picture is in <content: encoded> part of rss file


for blue line picture, the rss source is http://www.vesti-online.com/rss/Sport
here you are right, <decription> part of rss is part of article body


my question is, how to catch the url of only one image, no matter where it is located, and put it in the variable $urlslike ?

NOTE 1: I know that there is RSS, which has no picture, in this case the value of $urlslike take some url pictures value that I specify.

Note 2: apologize for drawing and painting, because I have a language barrier, but I understand perfectly what you write to me
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

hostingebc wrote:Note 2: apologize for drawing and painting, because I have a language barrier, but I understand perfectly what you write to me
It's fine, I get what you mean :)
hostingebc wrote:my question is, how to catch the url of only one image, no matter where it is located, and put it in the variable $urlslike ?
Okay so
$urlslike = $e->getAttribute("src");
Is getting the red images, and that $e comes from here
@$dom->loadHTMLFile($item->link);
$xpath = new DOMXPath($dom);
$entries = $xpath->query('//div[@class="img"]//img');
so could you first try this, and then try looking in the article body, so something like this maybe
@$dom->loadHTMLFile($item->link);
$xpath = new DOMXPath($dom);
$entries = $xpath->query('//div[@class="img"]//img');

if (empty($entries)){
    @$dom->loadHTMLFile($item->description);
    $xpath = new DOMXPath($dom);
    $entries = $xpath->query('//div[@class="img"]//img');
}
That is probably not exactly the right code, but the idea could work, I have never used the DOM things in PHP so it's a little hard to follow. But basically you want to try the article body if there are no images there then try the next place.
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

no, is still same problem
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

hostingebc wrote:no, is still same problem
That was not mean tot be a copy and paste solution, what did you try ?
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

I make somthing new in my code, i put $kontrola varialble (translation $control)

and i make switch

this is code
switch ($urlslike)
								{
									case $urlslike == $kontrola:
  											echo "$naslov, $opis, $sadrzaj, $izvor,, $url, $datum1, $datum2 </br></br>";
  											//echo " <h2>ista je</h2>: $urlslike | $kontrola <hr>";
  										break;
									case $urlslike != $kontrola:
 											echo "$naslov, $opis, $sadrzaj, $izvor,, $url, $datum1, $datum2, <img src='$urlslike'/> </br></br>";	
 											//echo "$urlslike | $kontrola <hr>";
 											$kontrola = $e->getAttribute("src");
  										break;
  								}
this is great solution, but....

i have new problem

if RSS do not have <content:encoded> my program stop working :)

any advice ? :)
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

sorry, in this example i have <content: encoded> sompthing else is not right.
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

depending on the source, in this line of code
$urlslike = $e->getAttribute("src");
1. for source http://www.rts.rs/page/sport/sr/rss.html do not work
2. for sorce http://www.naslovi.net/rss/sport working exelent

hiar is short basic code of rss reader (starting code for me)
You can try it :)
<?php
header('Content-Type: text/html; charset=utf-8');
	        
$rss = new SimpleXMLElement('http://www.naslovi.net/rss/sport', null, true);
       
                foreach($rss->xpath('channel/item') as $item)
                    {
                        $dom = new DOMDocument();
                             @$dom->loadHTMLFile($item->link);
                             $xpath = new DOMXPath($dom);
                             $entries = $xpath->query('//div[@class="img"]//img');
                             
                             foreach($entries as $e)
                             $image = "<img src='".$e->getAttribute("src")."'alt='' width='150'/>";
                             
                             $naslov = $item->title;
                        $opis = $item->description;
                        $sadrzaj = $item->content;
                        $izvor = "<a href='{$item->link}'>{$rss->channel->title}</a>";
                        $url = $item->link;
                        $datum1 = $item->pubDate;
                        $datum2 = $item->pubdate;
                        $urlslike = $e->getAttribute("src");
                        echo "$naslov, $opis, $sadrzaj, $izvor,, $url, $datum1, $datum2, <img src='$urlslike'/> </br></br>";
                        $urlslike = "";
        }
?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

$entries = $xpath->query('//div[@class="img"]//img');
will this line match the image elements of both feeds ?

you may need to make it more generic.
$entries = $xpath->query('//img');
or something like that.
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

i have 3 rss source
check hiar: http://www.moravaprom.com/ebchost/delete.php

but only one rotation (only 1 show an http://www.moravaprom.com/ebchost/main.php)

I have no errors, i check all

if i delete RTS - SPORT rss source i see two other with no problem
<?php
header('Content-Type: text/html; charset=utf-8');

	$konekcija = mysql_connect("nesto", "nesto", "nesto") or die ("Parametri za povezivanje sa bazom nisu dobri, ili baza ne postoji!");
	$baza = mysql_select_db("morava_rss", $konekcija) or die ("Na zalost, server nije u mogucnosti da pronadje bazu podataka!");
	
	$sajtovi = mysql_query ("SELECT * FROM rss_sajtovi") or die(mysql_error());
    
    while($row = mysql_fetch_array( $sajtovi ))
        {
            $id_sajta = $row['ID_SAJTA'];
            $naziv = $row['NAZIV'];
            $url = $row['URL'];
            $rss = $row['RSS'];
            $kategorija = $row['kategorija'];
            echo "<h1>$id_sajta  - <a href='$rss'>$naziv</a></h1><hr></br>";
               
		$rss = new SimpleXMLElement($rss, null, true);
       
                foreach($rss->xpath('channel/item') as $item)
                    {
                        $dom = new DOMDocument();
                             @$dom->loadHTMLFile($item->link);
                             $xpath = new DOMXPath($dom);
                             $entries = $xpath->query('//img');
                             
                             foreach($entries as $e)
                             $image = "<img src='".$e->getAttribute("src")."'alt='' width='150'/>";
                             
                             $naslov = $item->title;
                        $opis = $item->description;
                        $sadrzaj = $item->content;
                        $izvor = "<a href='{$item->link}'>{$rss->channel->title}</a>";
                        $url = $item->link;
                        $datum1 = $item->pubDate;
                        $datum2 = $item->pubdate;
                        $urlslike = $e->getAttribute('src');
                        echo "$naslov, $opis, $sadrzaj, $izvor,, $url, $datum1, $datum2, <img src='$urlslike'/> </br></br>";
                        
        		}
        }
?>
	
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

Oh are you trying to display all of the images, because if so you need to use { and } with the foreach loop
foreach($entries as $e){
    $image = "<img src='".$e->getAttribute("src")."'alt='' width='150'/>";
    
    $naslov = $item->title;
    $opis = $item->description;
    $sadrzaj = $item->content;
    $izvor = "<a href='{$item->link}'>{$rss->channel->title}</a>";
    $url = $item->link;
    $datum1 = $item->pubDate;
    $datum2 = $item->pubdate;
    $urlslike = $e->getAttribute('src');
    
    echo "$naslov, $opis, $sadrzaj, $izvor,, $url, $datum1, $datum2, <img src='$urlslike'/> </br></br>";
}
What you have currently will just loop to the last image and then use that further on.
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

i have 2 tables

First is rss_source - web pages with rss link

next table is rss_news - hiar i put data from rss.

i want to automaticli, insert all data from all rss web pages (rss_source) i have in rss_news table.

for example:
I have three RSS link in the first table, i need to make three rounds of the loop, the main loop (which works with RSS characters) - in order to enter

1 circle in loop
Source - RTS SPORT
- rss article 1, image
- rss article 2, image
- ...
- rss article n, image
-...
2 circle in loop
Source - vesti-online - sport
- rss article 1, image
- rss article 2, image
- ...
- rss article n, image
-...
3 circle in loop
Source - NASLOVI - SPORT
- rss article 1, image
- rss article 2, image
- ...
- rss article n, image
-...

n circle in loop
Source - nnnnnnnnnnnnn
- rss article 1, image
- rss article 2, image
- ...
- rss article n, image
-...




for now, i use Echo to se my resolt, after corect echo, i wil inser data in database with out echo :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

So why cant you do that ?

I don't really see what you are saying sorry :(
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

I want to make multyuser online news reader, but with pictures,with no pictures this is not a problem to do

I have already made​​, the user an owen profile can choose, or add sites that will follow, or to delete a site from list.

at the same time will be allowed to have its own news web page.

and visitors will be able to choose, for example, want to see any sports news or just want to see football news (that is not problem, I was the only porblem capture images)

Reading from the database is much faster than the RSS, and I know I do one of two ways that i known to prevent duplication of news from the same source when entering news in database.

I hope you clearly what is my only problem, and what is my goal. :)

NOTE:
for each RSS link from the table RSS_Source, I want to write, with of that link in a table RSS_news
The Structure of database, you can see below
rssdatabase.jpg
rssdatabase.jpg (78.82 KiB) Viewed 2334 times
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Rss with image: $image value

Post by jacek »

Okay, well why not just use the article body then, as that has the image tags in it and they are most likely at a point in the article that makes sense with the text around them.

So instead of trying to extract the images form the item you just use them as they are ?
Image
hostingebc
Posts: 28
Joined: Wed Jun 08, 2011 10:31 am

Re: Rss with image: $image value

Post by hostingebc »

ok. is not problem

i alredy done this code, and work greate :)

two questions:
1. how to get (or separate the) image url from the text. It is necessary for me more beautiful site design (I'll be able to change the dimensions of the image that I want, and not as provided in the content

2. how to using php-I notice a difference between the RSS and the one that has no content encoded

i write this code teoreticli
If rss have item pictures
       {
            code for rss with image in item
       }
       else
               //code with <content:encoded>
                  if have pictures
                               {
                                        Code for contetn encoded pictures
                               }
                                 else 
                                       code with no pictures
Post Reply