PHP to get twitter infos and avatar

I’ve just updated the Mini Bot Php Class with an improved version of the twitterInfo function, here is the code…

March 1, 2010

I’ve just updated the Mini Bot Php Class with an improved version of the twitterInfo function, here is the code of the new function, I’ve added the avatar image url:

/* this function is part of the Mini Bot Class */

	//
	// get twitter infos from nickname
	// and get avatar url
	public function twitterInfo($nick) {
		$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
		$ch = curl_init();    // initialize curl handle
		curl_setopt($ch, CURLOPT_URL, "http://twitter.com/$nick"); // set url to post to
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
		curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
		curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
		$document = curl_exec($ch);
		preg_match_all('#<div class="stats">(.*)</div>#Uis', $document, $stats);
		preg_match_all('#<span[^>]*?>(.*)</span>#Uis', $stats[1][0], $spans);
		$o = array();
		for ($i=0;$i<count($spans[0]);$i++) {
			if ($this->attr($spans[0][$i],"id")=="following_count") $o['following'] = $spans[1][$i];
			if ($this->attr($spans[0][$i],"id")=="follower_count") $o['follower'] = $spans[1][$i];
			if ($this->attr($spans[0][$i],"id")=="lists_count") $o['lists'] = $spans[1][$i];
		}
		$o['avatar'] = "";
		preg_match_all('#<img [^>]*?>#Uis', $document, $t);
		for ($i=0;$i<count($t[0]);$i++) if (attr($t[0][$i],"id")=="profile-image") $o['avatar'] = attr($t[0][$i],"src");
		return $o;	
	}

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “PHP to get twitter infos and avatar”

2 thoughts

  1. Kinduff says:

    Great script, but I got this error:
    Fatal error: Using $this when not in object context

    Can’t fix it, any ideas?

  2. Giulio Pons says:

    Try to use the minibots.class.php, this is a part of the code needed (the main, but it needs the attr function).

Comments are closed

Recommended

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

October 19, 2012

get MySpace events with a PHP function

Here is a function to read the concerts for a myspace band page. This code retrieves the “shows page” for…

February 21, 2011

New version of Mini Bots PHP Class (v.1.4)

I’ve added three more bots to the Mini Bots Php Class, now the version number is 1.4 and it has…

January 20, 2010

PHP Web page to text function

I’ve found this nice small bot on the www.php.net site, thanks to the author of the script on the preg_replace…

January 16, 2010

Bot that retrieves url meta data and other infos

From a given url this function retrieves page title, meta description, keywords, favicon, and an array of 5 images to…

January 12, 2010

PHP bot to grab meteo information from Google

Google has many usefull functions that give you data fast, such as cinema infos, or for meteo forecasts. I think…

December 24, 2009