Aug 18 2011

How to use Instagr.am photos on your site

Category: Php,Spiders & web botsGiulio Pons @ 4:11 pm

The question is: how can I bring my instagram photos on my personal web site?

With this tutorial you can make this.

I’m addicted to Instagram, I make photos quite every day with it. But I’m also a developer, and I already have a personal portfolio for my photos… which became obsolete since it’s a year that I don’t update it. Instagr.am is only on iPhone. There isn’t your page with photos online, but there are few APIs that can be used to retrieve your photos and infos and, there isn’t any public feed to retrieve photos.

So long: how can I bring my instagram photos on my personal web site? Thanks to followgram.me you can create your “vanity url” that is to say that you can register simply by logging in with your instagram account (oAuth is performed) and create a simple and nice url with your photos, like followgram.me/giuliopons. This page, with his customizable UI, can be OK for many users, but not for me. :-) I want only photos, and nothing else matters.
Since on my vanity URL there is also a feed that publishes, in a public feed, my pictures I’ve decided that this will be my door to my photos, without using the instagram API and their oAuth integration.

I’ve written the following code that read the feed, parse it, get pictures links and infos, store data (not pictures) in a local file (and periodically check if there are new photos without overloading followgram.me server) and output it in a minimalistic html good for any pc, iphone or ipad.

This is the result: www.ku-ku.it

So, if you want to do the same, make your vanity url on Followgram.me and use this code in your site:

This is the content of my index.php file.

<?
ini_set('default_charset', 'UTF-8');

// ----------------------------------------------------------------------
// CONFIG
$instagram_user = "giuliopons"; // your instagram username
$cachetime = 2; // 2 hours
$file = $instagram_user."_instagram.txt"; // file used to cache content
$TITLE = "Foto di Giulio Pons e Roberta Casaliggi con instagr.am"; // your page title
// ----------------------------------------------------------------------

function getFollowgram($u) {
	// function read instagram feed through followgram.me service, thanks Fabio Lalli
	// twitter @fabiolalli
	$url = "http://followgram.me/".$u."/rss";
	$s = file_get_contents($url);
	preg_match_all('#<item>(.*)</item>#Us', $s, $items);
	$ar = array();
	for($i=0;$i<count($items[1]);$i++) {
		$item = $items[1][$i];
		preg_match_all('#<link>(.*)</link>#Us', $item, $temp);
		$link = $temp[1][0];
		preg_match_all('#<pubDate>(.*)</pubDate>#Us', $item, $temp);
		$date = date("d-m-Y H:i:s",strtotime($temp[1][0]));
		preg_match_all('#<title>(.*)</title>#Us', $item, $temp);
		$title = $temp[1][0];
		preg_match_all('#<img src="([^>]*)">#Us', $item, $temp);
		$thumb = $temp[1][0];
		$ar['date'][$i] = $date;
		$ar['image'][$i] = str_replace("_5.jpg","_6.jpg",$thumb);
		$ar['bigimage'][$i] = str_replace("_5.jpg","_7.jpg",$thumb);
		$ar['link'][$i] = $link;
		$ar['title'][$i] = $title;
	}
	return $ar;
}

function checkValidFile($f,$hours) {
	// Function thar check if a file is older than X hours
	$durata=60*60 * $hours;
	$daquanto=$durata+1;
	if (file_exists($f)) $daquanto=time()-filemtime($f); else return false;
	if ($daquanto<=$durata) {
		// existing file is still valid
		$t = ($durata-$daquanto);
		$s = $t % 60;
		$m = floor($t/60) % 60;
		$h = floor($t/3600) % 24;
		$g = floor($t/3600/24);
		return true;
	} else {
		// existing file is old
		return false;
	}
}

// -----------------------------------------------
// load cached file
if (!file_exists($file)) $archive=""; else {
	$rHandle = fopen($file, 'r');
	$archive = fread($rHandle, filesize($file));
	fclose($rHandle);
}

// -----------------------------------------------
// check for new feed every X hours
if(!checkValidFile($file, $cachetime)) {
	$r = getFollowgram($instagram_user);
	// add new images to archive file
	for ($i=floor(count($r,COUNT_RECURSIVE)/count($r)); $i>=0; $i--) {
		if($r['image'][$i]) {
			$temp = "<li><img src='".$r['image'][$i]."'/><span>".$r['date'][$i]."<br/>".$r['title'][$i]."</span></li>";
			if(!stristr($archive,basename($r['image'][$i]))) $archive = $temp.$archive;
		}
	}
	// save new file
	$f = fopen($file,'w');
	fwrite($f,$archive);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="it" />
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
	<meta content="yes" name="apple-mobile-web-app-capable" />
	<meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no" name="viewport" />
	<title><?=$TITLE?></title>
	<style>
		body { background-color:#000; margin:0;padding:3px;}
		ul,li {list-style:none;margin:0 auto;padding:0;}
		li { float:left; width:306px;height:306px;position:relative;}
		li span { position:absolute; left:0;bottom:0;background-color:#000;color:#fff; height:auto;font-family:trebuchet ms,trebuchet;font-size:12px;width:300px;padding:3px;}
	</style>
	<script>
		$(document).ready(function() {
			$('li span').hide();
			$('li').mouseenter(function(){ $(this).find('span').show(); });
			$('li').mouseleave(function(){ $(this).find('span').hide(); });
		} );
	</script>
</head>
<body>
<ul><?=$archive?></ul>
</body>
</html>

I hope this will be enaugh for you to use it on your site. :)

Share

Tags: , , , , , ,


Aug 29 2010

PHP bot to get wikipedia definitions

Category: Php,Spiders & web botsGiulio Pons @ 3:14 pm

Wikipedia, the collaborative and multilingual encyclopedia project, has a lot of usefull terms defined in its database, you can find informations on artists, cities, medical terms, cars, brands… quite everything.
If you need to add some content to your pages without having that content in your database you can use Wikipedia API or Google define query (probably there’s also a Google API). You can, for example, need to add automatically a simple description to a city name, or to a band name. Or you could need to add the definition of some technological terms. You can do all of this things using Wikipedia, since Wikipedia has an API that easily lets you do it.

The php job is simple: we use CURL to call the API that returns an XML response, we parse it and the get the defnition.
Here is the code that make it for italian wikipedia, you can modify the url to match your wikipedia country site:

function wikidefinition($s) {
	$url = "http://it.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
	curl_setopt($ch, CURLOPT_POST, FALSE);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_NOBODY, FALSE);
	curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
	curl_setopt($ch, CURLOPT_REFERER, "");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
	$page = curl_exec($ch);
	$xml = simplexml_load_string($page);
	if((string)$xml->Section->Item->Description) {
		return array((string)$xml->Section->Item->Text, (string)$xml->Section->Item->Description, (string)$xml->Section->Item->Url);
	} else {
		return "";
	}
}

This code will be added to the MINI BOTS CLASS.

Share

Tags: , , , , , ,


Aug 09 2010

Send push notification to iPhone with PHP and pushme.to

Category: Php,Spiders & web botsGiulio Pons @ 4:48 pm

Push service is a technology that allows you to send alerts/notifications to a mobile device. Blackberry has its own push service, iPhone has its own, and also Android devices have their own push services (you can learn more on Wikipedia page).
Push notifications are like “sms” but they are free if you have a flat service contract on your mobile.
I want to send push to my iPhone, but I’m not able to develop an iPhone application that receive push alerts, and I don’t want to send only email alerts.
Ok, let’s recap: I have an iPhone and I need to send notifications to my mobile from my PHP scripts.
So I’ve looked deeply in the appStore and I’ve found pushme.to application. This application is made to talk with friends trought web sites and iPhones.
Pushme.to is good because after you’ve registered you can go on their widget page and create a widget for your account to put on your web site. Well, in this post, I will not use this widget for my web site (I don’t want users to write me), but I will use their widget with a mini web bot to send alerts directly to my device through a PHP function.
It happens this way: the cron job calls my php script, my script uses a small bot to call pushme.to widget, pushmeTo sends alerts to my iPhone.

So, if you want to send push to your iPhone thorught a PHP script, download the pushme.to app (it’s free) on your iPhone, register an account and get your pushme.to widget. From the widget code extract the url called in the iframe, it’s something like this:


http://pushme.to/q/widget/export/?hash=51ff0b6e3c1ce3a7a7e473198e1b6d9a

Then, use this php bot function in your scripts:

function pushMeTo($widgeturl,$text,$signature) {
	$agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12";
	if (!function_exists("curl_init")) die("pushMeTo needs CURL module, please install CURL on your php.");
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $widgeturl);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	$page = curl_exec($ch);
	preg_match("/form action=\"(.*?)\"/", $page, $form_action);
	preg_match("/textarea name=\"(.*?)\"/", $page, $message_field);
	preg_match("/input type=\"text\" name=\"(.*?)\"/", $page, $signature_field);
	$ch = curl_init();
	$strpost = $message_field[1].'=' . urlencode($text) . '&'.$signature_field[1].'=' . urlencode($signature);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );
	curl_setopt($ch, CURLOPT_URL, $form_action[1]);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	$page = curl_exec($ch);
}

Call the function when you want, for example after a database check:

<?
// ... send an alert if database is down...
$url = "http://pushme.to/q/widget/export/?hash=51ff0b6e3c1ce3a7a7e473198e1b6d9a";
if (!mysql_connect( "domain", "user", "password" )) {
	pushmeTo ($url,"Mysql server 'domain' is down!","Your Bot");
}
?>

This small PHP bot “pushmeto” is included in the Mini Bots Class!

Share

Tags: , , , , , , , , ,


Next Page »