Buscando posts do Facebook

Desenvolvi este script para o site www.adcw.com.br, ele busca os posts na fan-page da agência no facebook e retorna em um ajax, aqui simplifiquei um pouco, utilizando “echo” mesmo…

 

<?

//@include("config.php");
define("CFG_FACEBOOK_FPID", "157126511023182");

//verifica o arquivo de cache
$CACHEFILE = "fbcache.tmp";
if ((!file_exists($CACHEFILE)) || ((time() - filemtime($CACHEFILE)) > 1800)) {

$targetdomain = 'www.facebook.com';
$hostIP = gethostbyname($targetdomain);
$fp = fsockopen($hostIP, 80, $errno, $errstr, 10);

$getString = "GET /feeds/page.php?id=".CFG_FACEBOOK_FPID."&amp;amp;amp;amp;    format=rss20 HTTP/1.1\r\nHost: $targetdomain\r\nConnection: close\r\n";
$getString .= "User-agent: Mozilla\r\nAccept: text/plain,text/html\r\n";
$getString .= "\r\n";

$feed_contents = "";
fputs($fp,$getString);
while ( (!feof($fp))  &&  ($line = fread( $fp, 8192 )) ) {
$feed_contents.=$line;
}

$fp = @fopen($CACHEFILE, 'w+');
fwrite($fp, $feed_contents);
fclose($fp);
$xml = $feed_contents;

}
else {
//se não puxou um novo arquivo do facebook faz a leitura da cache
$fp = fopen($CACHEFILE, 'r');
$xml = fread($fp, filesize($CACHEFILE));
fclose($fp);
}

//interpreta o XML
$i=0;
$items=array();

$xml = explode("\n",$xml);
for ($x=0; $x <= count($xml); $x++) {
$line = trim($xml&#91;$x&#93;);
if ($line == "<item>") { $i++; }
if ($i > 0) {
if (substr($line,0,7) == "<title>") { $items[$i]["title"] = htmlentities(trim(strip_tags($line)),  ENT_QUOTES|ENT_IGNORE,"UTF-8"); }
if (substr($line,0,6) == "<link>") { $items[$i]["link"] = htmlentities(trim(strip_tags($line)), ENT_QUOTES|ENT_IGNORE,"UTF-8"); }
if (substr($line,0,8) == "<author>") { $items[$i]["author"] = htmlentities(trim(strip_tags($line)), ENT_QUOTES|ENT_IGNORE,"UTF-8"); }

//-------------------------------
// pubDate
//-------------------------------

if (substr($line,0,9) == "<pubDate>") {
//formata a data para o formato da ADCW (Original: Wed, 06 Jul 2011 17:14:41)
//Thu, 14 Jul 2011 20:45:19 +0100
$data = htmlentities(strip_tags($line), ENT_QUOTES|ENT_IGNORE,"UTF-8");

$data = explode(" ",$data);
$items[$i]["pubDate"] = $data[1]." ".strtoupper($data[2])."<br>".$data[3];
}

//-------------------------------
// Links
//-------------------------------
$items[$i]["link"] = str_replace("&amp;amp;amp;amp;amp;","&amp;amp;amp;amp;",$items[$i]["link"]);

//-------------------------------
// description
//-------------------------------
if (substr($line,0,13) == "<description>") {

$data = htmlentities(trim(strip_tags(html_entity_decode($line),"<br>")),ENT_QUOTES|ENT_IGNORE,"UTF-8");

//titulo
if (substr($data,0,strlen($items[$i]["title"])) == $items[$i]["title"]) {
$data = trim(substr($data,strlen($items[$i]["title"])));
}
//autor
if (trim(substr($data,0,strlen($items[$i]["author"]))) == $items[$i]["author"]) {
$data = trim(substr($data,strlen($items[$i]["author"])));
}
//vimeo (retira url do inicio)
if (trim(substr($data,0,18)) == "vimeo.comvimeo.com") {
$data = trim(substr($data,18,(strlen($data)-18)));
}
//youtube (retira url do inicio)
if (trim(substr($data,0,30)) == "www.youtube.comwww.youtube.com") {
$data = trim(substr($data,30,(strlen($data)-30)));
}

//tira as quebras de linha do inicio do texto
if (substr($data,0,12) == "&amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;gt;") { $data = trim(substr($data,12)); }
if (substr($data,0,12) == "&amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;gt;") { $data = trim(substr($data,12)); }
//tira a mensagem FOTOS DO MURAL
if (substr($data,-14) == "Fotos do mural") {
$data = substr($data,0,(strlen($data)-14));
}
//--
$items[$i]["description"] = $data;
}

//-------------------------------
// image -> este campo ?xtraido do rss/description do fb
//-------------------------------
if (substr($line,0,13) == "<description>") {

$data = trim(strip_tags(html_entity_decode($line),"<img>"));
$data = explode(chr(34),$data);
for ($y=0; $y <= count($data); $y++) {
if ((substr($data&#91;$y&#93;,0,7) == "http://") &amp;amp;amp;amp;&amp;amp;amp;amp; (substr($data&#91;$y&#93;,-4) == ".jpg")) {
$data&#91;$y&#93; = str_replace("_s.jpg","_n.jpg",$data&#91;$y&#93;); //troca imagem pequena do RSS pela imagem tamanho normal
$items&#91;$i&#93;&#91;"image"&#93; = $data&#91;$y&#93;;
}
}
}
//-------------------------------

//-------------------------------
// video(vimeo) -> este campo extraido do rss/description do fb
//-------------------------------
if (substr($line,0,13) == "<description>") {
$data = trim(strip_tags(html_entity_decode($line),"<a>"));
$data = explode(chr(34),$data);
for ($y=0; $y <= count($data); $y++) {
if (substr($data&#91;$y&#93;,0,17) == "http://vimeo.com/") {
$items&#91;$i&#93;&#91;"video"&#93; = $data&#91;$y&#93;;
}
}
}
//-------------------------------

//-------------------------------
// video(youtube) -> este campo extraido do rss/description do fb
//-------------------------------
if (substr($line,0,13) == "<description>") {
$data = trim(strip_tags(html_entity_decode($line),"<a>"));
$data = explode(chr(34),$data);
for ($y=0; $y <= count($data); $y++) {
if (substr($data&#91;$y&#93;,0,31) == "http://www.youtube.com/watch?v=") {
$items&#91;$i&#93;&#91;"video"&#93; = $data&#91;$y&#93;;
}
}
}
//-------------------------------

}
}

//injeta o item mais recente no site usando jquery e eval
if (count($items) > 0) {

//echo $items[1]["description"];

echo "<h2>".$items[1]["pubDate"]."</h2>\n";
echo "<h1><a href=\"".$items&#91;1&#93;&#91;"link"&#93;."\">".$items[1]["title"]."</a></h1>\n";

if ($items[1]["video"] != "") {
if (substr($items[1]["video"],0,17) == "http://vimeo.com/") {
echo "<iframe id=\"facebook_image\" src=\"http://player.vimeo.com/video/".substr($items&#91;1&#93;&#91;"video"&#93;,17,(strlen($items&#91;1&#93;&#91;"video"&#93;)-17))."\" width=\"530\" height=\"310\" frameborder=\"0\" allowfullscreen></iframe>";
}
elseif (substr($items[1]["video"],0,31) == "http://www.youtube.com/watch?v=") {
echo "<iframe id=\"facebook_image\" width=\"530\" height=\"310\" src=\"http://www.youtube.com/embed/".substr($items&#91;1&#93;&#91;"video"&#93;,31,(strlen($items&#91;1&#93;&#91;"video"&#93;)-31))."\" frameborder=\"0\" allowfullscreen></iframe>";
}
else {
echo "<img id=\"facebook_image\" src=\"".$items&#91;1&#93;&#91;"image"&#93;."\" />\n";
}
}
else {
echo "<img id=\"facebook_image\" src=\"".$items&#91;1&#93;&#91;"image"&#93;."\" />\n";
}

}
else {
echo "Não foi possível buscar os dados...\n";
}

?>