Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Tenho um código que lê o xml do rss de um perfil no twitter, mas de uma hora pra outra ele começou a apresentar erros que eu não consigo dizer o que pode ser.
Segue o código:
<?php
date_default_timezone_set('America/Sao_Paulo');
$xml = simplexml_load_file("http://search.twitter.com/search.atom?q=+from%3Ashowrural&rpp=5");
// $xml = simplexml_load_file("twitter.xml");
if(count($xml->entry[0])>0){
for($a=0; $a < 2; $a++) {
$date = $xml->entry[$a]->updated;
$content = $xml->entry[$a]->content;
$link = $xml->entry[$a]->link[0]->attributes()->href;
$date = strtotime($date);
$datetime = date('d.m.Y \à\s H:i', $date);
echo "<div class='content'>";
echo "<p>".$content."</p>";
echo "<span><a href='".$link."' title='Link direto do tweet'>em ".$datetime."</a></span>";
echo "</div>";
} //foreach
} else {
echo "<div class='content'>";
echo "<p>Nenhum tweet publicado ainda</p>";
echo "</div>";
}
?>
Os erros:
>
Notice: Trying to get property of non-object in /sites/www/html/showrural/hospedagem/teste_twitter.php on line 8 Notice: Trying to get property of non-object in /sites/www/html/showrural/hospedagem/teste_twitter.php on line 9 Notice: Trying to get property of non-object in /sites/www/html/showrural/hospedagem/teste_twitter.php on line 10 Fatal error: Call to a member function attributes() on a non-object in /sites/www/html/showrural/hospedagem/teste_twitter.php on line 10
A Leitura da variavel $xml é:
object(SimpleXMLElement)#1 (5) { ["id"]=> string(49) "tag:search.twitter.com,2005:search/from:showrural" ["link"]=> array(4) { [0]=> object(SimpleXMLElement)#2 (1) { ["@attributes"]=> array(3) { ["type"]=> string(9) "text/html" ["href"]=> string(51) "http://search.twitter.com/search?q=from%3Ashowrural" ["rel"]=> string(9) "alternate" } } [1]=> object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(3) { ["type"]=> string(20) "application/atom+xml" ["href"]=> string(56) "http://search.twitter.com/search.atom?q=from%3Ashowrural" ["rel"]=> string(4) "self" } } [2]=> object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(3) { ["type"]=> string(37) "application/opensearchdescription+xml" ["href"]=> string(33) "http://twitter.com/opensearch.xml" ["rel"]=> string(6) "search" } } [3]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(3) { ["type"]=> string(20) "application/atom+xml" ["href"]=> string(84) "http://search.twitter.com/search.atom?since_id=251065721344380928&q=from%3Ashowrural" ["rel"]=> string(7) "refresh" } } } ["title"]=> string(31) "from:showrural - Twitter Search" ["updated"]=> string(20) "2012-09-26T21:08:27Z" ["entry"]=> object(SimpleXMLElement)#6 (7) { ["id"]=> string(46) "tag:search.twitter.com,2005:251065721344380928" ["published"]=> string(20) "2012-09-26T21:08:27Z" ["link"]=> array(2) { [0]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(3) { ["type"]=> string(9) "text/html" ["href"]=> string(56) "http://twitter.com/showrural/statuses/251065721344380928" ["rel"]=> string(9) "alternate" } } [1]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(3) { ["type"]=> string(9) "image/png" ["href"]=> string(68) "http://a0.twimg.com/profile_images/1693278216/logofanpage_normal.jpg" ["rel"]=> string(5) "image" } } } ["title"]=> string(75) "Tem algum imóvel para alugar no Show Rural? Cadastre: http://t.co/nA0FxjzD" ["content"]=> string(110) "Tem algum imóvel para alugar no Show Rural? Cadastre: http://t.co/nA0FxjzD" ["updated"]=> string(20) "2012-09-26T21:08:27Z" ["author"]=> object(SimpleXMLElement)#9 (2) { ["name"]=> string(31) "showrural (Show Rural Coopavel)" ["uri"]=> string(28) "http://twitter.com/showrural" } } }
Ao retirar os parenteses, desta linha,
$link = $xml->entry[$a]->link[0]->attributes()->href;
Não tive erro, e exibiu 2 links, mas não sei se é isso que deseja.
Supondo que a linha 10 seja a que se refere ao content, verifica se não foi alterado o nome dele oou algo do tipo no retorno do xml.
Dê um var_dump ou um print_r no $xml e confere se tudo está como esperado.