View image from another site

Asked

Viewed 203 times

0

Good afternoon! , I am using a php code to display news from a site, it turns out that I am also trying to display the image that has in it, however, I am not getting, the title and date of posting I am able to display normal, however, that I can not

<?php
// Mostrar Data na Tela.
$titulo = array();
$data = array();
$link = array();
$quantos = 0;
$exibir = 1;
$limite_title = 100;

foreach(simplexml_load_file("http://cidades.gov.br/ultimas-noticias?format=feed&type=rss")->channel->item as $item)
{
$imagem[] = $item->src;
$titulo[] = utf8_decode(substr($item->title, 0, $limite_title)."...");
$link[] = $item->link;
$data[] = utf8_decode($item->pubDate);
$quantos++;
}

for($i = $quantos-($exibir+1); $i < $quantos-1; $i++)
{
if($titulo[$i]!="")
{
echo
'
<li>
<figure><img src="'.$imagem[$i].'"></figure>
<a href="'.$link[$i].'" target="_blank" title="Leia mais clicando aqui!">'.utf8_encode($titulo[$i]).'
<small style="font-size:11px;color:#999;"><br/></a>'.str_replace(" ", " as ", date('d/m/Y H:m:s', strtotime($data[$i]))).'</small>
</li>
';
}
}
?>

What I did wrong ?

  • $item->src exists?

  • $image[] = $item->src;

  • Navigate to the Description node and get the img tag, then you assign $image[]

  • I don’t understand, can you explain ?

  • I put together a script and I don’t know if that’s what you want, see this link http://kithomepage.com/sos/abrepagina.php. Of course it’s not HTML formatted.

  • I also developed one that brings everything. velja http://kithomepage.com/sos/abrepaginaFull.php

  • Hello, Leo can forward me this script ? exactly the one I want the image

Show 2 more comments

1 answer

1


The problem is that the feed is using HTML along with <![CDATA[...]]>:

<description><![CDATA[<div><img style="margin-left: 5px; border-radius: 5px; float: right;" title="Foto: Rafael Luz/ MCidades" src="http://cidades.gov.br/images/galeria_em_artigos/amupe_interna.jpeg" alt="amupe interna" />O ministro das Cidades...</div>]]></description>

The CDATA is not considered "part of XML", or rather is like a text only and will not be processed unless you request, the way to do it would be by parsing the content of description also, so:

<?php
// Mostrar Data na Tela.
$titulo = array();
$data = array();
$link = array();
$quantos = 0;
$exibir = 1;
$limite_title = 100;

$items = simplexml_load_file("http://cidades.gov.br/ultimas-noticias?format=feed&type=rss")->channel->item;

$doc = new DOMDocument;

foreach($items as $item) {
    $titulo[] = utf8_decode(substr($item->title, 0, $limite_title)."...");
    $link[] = $item->link;
    $data[] = utf8_decode($item->pubDate);
    $quantos++;

    //Converte o objeto para string
    $desc = (string) $item->description;

    //Faz o parse da string html para DOMElement
    if ($doc->loadHTML($desc)) {

        //Pega todas tags img
        $imgs = $doc->getElementsByTagName('img');

        //Verifica se existe ao menos uma imagem
        if ($imgs->length) {

            //Salva a imagem no array
            $imagem[] = $imgs->item(0)->getAttribute('src');

            //para o loop atual evitando que chegue até o "$imagem[] = null;"
            continue;
        }
    }

    //Se não houver description ou img então envia o valor como null
    $imagem[] = null;
}

//E no teu for pode fazer assim:

for ($i = $quantos-($exibir+1); $i < $quantos-1; $i++) {
    if($titulo[$i]!="") {
       echo '<li>';

       //Se a imagem existir a exibe, caso contrário não
       if ($imagem[$i] !== null) {
           echo '<figure><img src="'.$imagem[$i].'"></figure>';
       }

        echo '<a href="'.$link[$i].'" target="_blank" title="Leia mais clicando aqui!">'.utf8_encode($titulo[$i]).'
        <small style="font-size:11px;color:#999;"><br/></a>'.str_replace(" ", " as ", date('d/m/Y H:m:s', strtotime($data[$i]))).'</small>
        </li>
        ';
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.