Seguidores de Twitter en PHP
Un simple y sencillo código con el que podemos conseguir la cantidad de seguidores que tenemos en Twitter.
<br /><!–?php<br /–>function string_getInsertedString($long_string,$short_string,$is_html=false){<br /> if($short_string>=strlen($long_string))return false;<br /> $insertion_length=strlen($long_string)-strlen($short_string);<br /> for($i=0;$i if($long_string[$i]!=$short_string[$i])break;<br /> }<br /> $inserted_string=substr($long_string,$i,$insertion_length);<br /> if($is_html && $inserted_string[$insertion_length-1]=='<‘){<br /> $inserted_string='<‘.substr($inserted_string,0,$insertion_length-1);<br /> }<br /> return $inserted_string;<br />}<br />function DOMElement_getOuterHTML($document,$element){<br /> $html=$document->saveHTML();<br /> $element->parentNode->removeChild($element);<br /> $html2=$document->saveHTML();<br /> return string_getInsertedString($html,$html2,true);<br />}<br />function getFollowers($username){<br /> $x = file_get_contents(«http://twitter.com/».$username);<br /> $doc = new DomDocument;<br /> @$doc->loadHTML($x);<br /> $ele = $doc->getElementById(‘follower_count’);<br /> $innerHTML=preg_replace(‘/^<[^>]*>(.*)<[^>]*>$/’,»\\1″,DOMElement_getOuterHTML($doc,$ele));<br /> return $innerHTML;<br />}<br />?><br />
El siguiente código, pegado donde queramos, nos va a mostrar el resultado con la cantidad de seguidores.
<br /><span><!–?php echo getFollowers(«sessionstudio»); ?–> Seguidores</span><br />