Posted By

ginoplusio on 03/01/10


Tagged

curl php twitter avatar


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

ginoplusio
alvincrespo
dantreacy


Retrieve Twitter info from php


 / Published in: PHP
 

URL: http://www.barattalo.it/2010/03/01/php-to-get-twitter-infos-and-avatar/

PHP function of mini bot class that lets you retrieve twitter infos from twitter nickname. ($this->attr method is part of the Mini Bot Class)

  1. /* this function is part of the Mini Bot Class */
  2.  
  3. //
  4. // get twitter infos from nickname
  5. // and get avatar url
  6. public function twitterInfo($nick) {
  7. $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
  8. $ch = curl_init(); // initialize curl handle
  9. curl_setopt($ch, CURLOPT_URL, "http://twitter.com/$nick"); // set url to post to
  10. curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
  11. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
  13. curl_setopt($ch, CURLOPT_PORT, 80); //Set the port number
  14. curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
  15. curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  16. $document = curl_exec($ch);
  17. preg_match_all('#<div class="stats">(.*)</div>#Uis', $document, $stats);
  18. preg_match_all('#<span[^>]*?>(.*)</span>#Uis', $stats[1][0], $spans);
  19. $o = array();
  20. for ($i=0;$i<count($spans[0]);$i++) {
  21. if ($this->attr($spans[0][$i],"id")=="following_count") $o['following'] = $spans[1][$i];
  22. if ($this->attr($spans[0][$i],"id")=="follower_count") $o['follower'] = $spans[1][$i];
  23. if ($this->attr($spans[0][$i],"id")=="lists_count") $o['lists'] = $spans[1][$i];
  24. }
  25. $o['avatar'] = "";
  26. preg_match_all('#<img [^>]*?>#Uis', $document, $t);
  27. for ($i=0;$i<count($t[0]);$i++) if (attr($t[0][$i],"id")=="profile-image") $o['avatar'] = attr($t[0][$i],"src");
  28. return $o;
  29. }

Report this snippet  

You need to login to post a comment.