Mar 01 2010

PHP curl bot to update Facebook status

Category: Php, Spiders & webbotsadmin @ 10:30 pm

I’ve found this great mini bot from Alste blog, and I’ve decided to add it to the mini bot class. This bot uses curl to connect to facebook mobile (m.facebook.com) and perform the login. Then it saves the cookies received from mobile facebook and go to the facebook mobile homepage where it sets the status making a post.
I’ve tried to make the same thing with the normal facebook, but it didn’t work. I think that mobile facebook is simpler and easier to make bots working. I’ve added this bot to the Mini Bot PHP class.

	//
	// change Facebook status with curl
	// Thanks to Alste (curl stuff inspired by nexdot.net/blog)
	public function setFacebookStatus($status, $login_email, $login_pass) {
		$debug = false;
		//CURL stuff
		//This executes the login procedure
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=' . urlencode($login_email) . '&pass=' . urlencode($login_pass) . '&login=' . urlencode("Log in"));
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//make sure you put a popular web browser here (signature for your web browser can be retrieved with 'echo $_SERVER['HTTP_USER_AGENT'];'
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
		curl_exec($ch);

		//This executes the status update
		curl_setopt($ch, CURLOPT_POST, 0);
		curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
		$page = curl_exec($ch);

		curl_setopt($ch, CURLOPT_POST, 1);
		//this gets the post_form_id value
		preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
		//we'll also need the exact name of the form processor page
		preg_match("/form action=\"(.*?)\"/", $page, $form_num);

		curl_setopt($ch, CURLOPT_POSTFIELDS, 'post_form_id=' . $form_id[1] . '&status=' . urlencode($status) . '&update=' . urlencode("Update status"));
		//set url to form processor page
		curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $form_num[1]);
		curl_exec($ch);

		if ($debug) {
			//show information regarding the request
			print_r(curl_getinfo($ch));
			echo curl_errno($ch) . '-' . curl_error($ch);
			echo "<br><br>Your Facebook status seems to have been updated.";
		}
		//close the connection
		curl_close($ch);
	}
  • Share/Bookmark

Tags: , , , , ,


Mar 01 2010

PHP to get twitter infos and avatar

Category: Php, Spiders & webbotsadmin @ 10:23 pm

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;
	}
  • Share/Bookmark

Tags: , , , , ,


Jan 20 2010

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

Category: Php, Spiders & webbotsadmin @ 11:53 pm

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

Follow the link, watch demos and download the class from the Mini Bots Php Class page:

  1. addeded twitterInfo method to retrieve the numbers of following, followers, and lists.
  2. addeded url2pdf method to convert and save locally a page to a pdf (thanks to pdfmyurl.com service).
  3. addeded webpage2txt method to extract all the text from a url, stripping html but also scripts and styles.

What else do you need? Suggest some usefull small bots. Comment to suggest.

  • Share/Bookmark

Tags: , , , , , ,


Next Page »