~ overflow ~

Tag: custom

Custom twitter data fetching in php with cache

by z3n on May.28, 2010, under Coding, Tips & Hints

Problem:

I had to fit the twitter posts (“tweets”) on a site using a special layout, without all the regular twitter crapola

Solution:

There’s this old script that return contents as json, although i could simply add it to the javascript, it would slow down the site, everytime a new page was loaded user would need to fetch tweets all over again, so i did a little php function to fetch it cache and add to the output:

function _ld_twitter($profile=_twitter_profile,$tweets=_twitter_tweets) { // v1.0
	// check cache
	$hash=md5(md5($profile).md5($tweets));
	if (_twitter_cache && file_exists(_twitter_cache_path.$hash.".idx") && file_exists(_twitter_cache_path.$hash) && _fs(_twitter_cache_path.$hash.".idx") > getmicrotime())
		return _fs(_twitter_cache_path.$hash);

	// build
	$x=file_get_contents("http://twitter.com/statuses/user_timeline/".$profile.".json?callback=x&count=".$tweets);
	if ($x != "") {
		$x=json_decode(substr($x,2,-2));
		$b=_fs(templates."twitter".file_ext);$r="";$i=count($x)-1;
		foreach ($x as $k => $v)
			$r.=str_replace(array(
				"##WHO##","##IMG##",
				"##WHEN##","##MSG##",
				"##DELAY##","##LAST##"
				),array(
					$v->user->screen_name,$v->user->profile_image_url,
					date(_dt2,strtotime($v->created_at)),not_utf8($v->text),
					duration(getmicrotime()-strtotime($v->created_at),1,0),$k == $i ? " id='last'" : ""
			),$b);
		// do cache
		if (_twitter_cache) {
			_fw(_twitter_cache_path.$hash.".idx",(_twitter_cache_life*60) + getmicrotime(),"w");
			_fw(_twitter_cache_path.$hash,$r,"w");
		}
		// return
		return $r;
	} else {
		return false;
	}
}

// defines -- yes not formatted correctly
"_twitter_profile" => "your_tweeter_profile_name",										// twitter profile
"_twitter_tweets" => 3,															// how many tweets to load
"_twitter_cache_life" => 15,												// twitter cache life in minutes
"_twitter_cache_path" => "lib/cache/",							// twitter cache path
"_twitter_cache" => 1,															// enables / disables twitter cache
"_dt2" => "d M Y",
"templates" => "templates/",
"file_ext" => ".html"

/*
functions (not added here)
those functions are enhanced versions of basic functions, to try this script you can just take :
_fs = file_get_contents
_fw = file_put_contents
duration = a function to return a number of seconds into years, months, days, hours, minutes, seconds notation
*/

If you want to implement it as javascript, you can use the url:
http://twitter.com/statuses/user_timeline/”.$profile.”.json?callback=x&count=”.$tweets

where the callback is the function where the json data will be sent to.

1 Comment :, , , , , more...

Custom Google Search “API” by PHP (No API Needed)

by z3n on Jun.16, 2009, under Coding, Tips & Hints

Problem:

I was looking for an API to do searches by google using a custom made php script, so i could display the results the way i want. I found out about a google search api that work with SOAP, however it’s been deprected and they only have the useless AJAX option, which don’t really serve my propouse since the searches would be client-side processed at AJAX, and i needed them server-side processed.

Solution:

This might looks like a hack, but there’s nothing much else we can do, although, for me, it was much easier than doing the research on the original api and finally finding out it was deprected. This PHP script will do a regular request to google, then it will process the data using DOM, cut it removing google ads and other crap, and finally return only the urls of the search. This will also make you able to search on country specific google, just change the domain and filter to get the results you want.

So here’s the PHP source that don’t depend on google happyness to work:

// (c) z3n – R1V1@090616 – z3n666@gmail.com – www.overflow.biz
  1.  
  2. if (!isset($argv[1])) { echo "Usage: ".$_SERVER['PHP_SELF']."  [start]";die; } else { $s=$argv[1]; }
  3.  
  4. $e=(isset($argv[2])) ? "&start=".$argv[2] : "";
  5.  
  6. $x=file_get_contents("http://www.google.com/search?q=".$s."&num=100".$e);
  7.  
  8. // apply DOM
  9.  
  10. $dom=new DOMDocument();
  11. @$dom->loadHTML($x);
  12.  
  13. $xpath=new DOMXPath($dom);
  14. $a=$xpath->evaluate("/html/body//a");
  15. for ($i=$k=0;$i<$a->length;$i++) {
  16.  $y=$a->item($i);
  17.  $z=$y->getAttribute('href');
  18.  // filter out google crap, wikipedia and youtube
  19.  if (($z{0} != "/") && (strpos($z,"q=cache:") === false) && (strpos($z,"youtube.com/") === false) && (strpos($z,"wikipedia.org/") === false) && (strpos($z,".google.com/") === false)) {
  20.   $_url[$k]=$z;$k++;
  21.  }
  22. }
  23.  
  24. print_r($_url);
  25. echo "Found: ".$k." urls.";

* Note: You need DOM Object support in order to run this script; This example has been developed to accept input from the CLI, you can easly change it to work inside your script or/and accept $_GET or $_POST.


Leave a Comment :, , , , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!