Twitter Updates
  • someone offered my something pretty cool. my brain now has big decisions to make 17 hours ago

Category Archives: APIs

Really Simple WordPress Queries

When I was learning about WordPress and how to manipulate data, it seemed like everyone had an opinion on how to do what is actually very simple queries. Take for example; Running a custom query to get posts from a certain category. If you were to Google that you would be returned with pretty much thousands of example code, some working, some not. My 2 pence; Its this simple:

<?php $bloggy = new WP_Query("cat=5&showposts=5"); while($bloggy->have_posts()) : $bloggy->the_post();?>
	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
	<?php the_excerpt(); ?>
<?php endwhile; ?>

Where cat=5; change to the category ID which you want to get the posts from & showsposts=5 should be changed to however many posts you would like to see per page. It really is that simple. Now think of the possibilities of it if you were to create a shortcode from it and pass variables like [showposts cat=5 posts=10] anywhere on any page.

DailyBooth API: Most Recent Snap

The DailyBooth API is a land yet to be chartered when it comes to developing on it. Its very much still in beta, and Jon orRyan might even shout at me for starting to document into at this stage. But i’m willing to risk it!

Overall, DailyBooth API is pretty straight forward and really quite simple, which is great. Ill quickly show you how to grab your most recent snap via the API.

        ## GET THE USERS USER ID VIA THERE USERNAME
	$userID = json_decode(file_get_contents('http://api.dailybooth.com/v1/user/id/paul.json'));

	## NOW COLLECT THE IMAGES FROM THERE FEED, VIA THERE USERID
	$feed = json_decode(file_get_contents('http://api.dailybooth.com/v1/user/pictures/'.$userID[0].'.json'));

	## PRINT OUT THE IMAGE
	echo '<img src="'.$feed[0]->urls->medium.'" />';

Twitter @Anywhere Intergration For WordPress 3.0

Just playing to try learn more about WordPress as I have taken a drastic disliking to it as of recent, which is probably due to the fact I haven’t really given it the time of day so far.

This adds javascript to your header that will activate Twitters @Anywhere API stuff and also add hovercards.

	/*
	Plugin Name: Twitter @Anywhere
	Description: Add Twitters @Anywhere to WordPress 3.0
	Version:     0.1
	Author:      Paul Fraser
	Plugin URI:  http://www.paulOr.net
	Author URI:  http://www.paulOr.net
	*/

	function add_twitter_anywhere() {
		echo '<script type="text/javascript" src="http://platform.twitter.com/anywhere.js?id=<-- API KEY -->&v=1"></script>'."\n";
		echo '<script type="text/javascript">'."\n\t";
		echo 'twttr.anywhere(function(twitter) {'."\n\t";
		echo 'twitter.hovercards();'."\n";
		echo '});'."\n";
		echo '</script>'."\n";
	}

	add_action( 'wp_head', 'add_twitter_anywhere' );

Using Google Latitude API w/ Google Maps

Google, Google, Google eh! I’ve known about Google Latitude for a while now, I’ve been signed up for ages, just never used it!

I decided to take a look the other day and found it pretty nice how it does passive “check-ins”, thats more my style, none of this fourwalla rubbish!

Heres an easy way to plot your current location on Google Maps :]

<!--?php<br /--> ## QUERY STRING
$latitude = json_decode(file_get_contents('http://www.google.com/latitude/apps/badge/api?user=&lt;-- USER ID --&gt;&amp;type=json'));
## CURRENT LOCATION
echo $latitude-&gt;features[0]-&gt;properties-&gt;reverseGeocode;
## TIME AT LOCATION
echo $latitude-&gt;features[0]-&gt;properties-&gt;timeStamp;
## IMAGE URL :]
echo $latitude-&gt;features[0]-&gt;properties-&gt;placardUrl;

?&gt;
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=&lt;-- API KEY --">// <![CDATA[
 " type="text/javascript">
// ]]></script>
<script src="http://paulor.net/inc/uploads/5552mapit.js" type="text/javascript"></script> <script type="text/javascript">// <![CDATA[
	window.onload = function(){
		var mymap = new MeOnTheMap({
			container: "map",
			html: "<span id=\"location\"></span>

<img src=\"\" />",
			address: "",
			zoomLevel: 13
		});
		mymap.adjustMapCenter({
			x: 0,
			y: -80
		});
	}
// ]]></script>
<!-- .map { width: 710px; height: 300px; overflow: hidden; } #location { display: block; background-color: #111111; color: #ffffff; } -->

You will first of all ofcourse have to go sign up to Google Latitude aswell as get yourself a nice API Key from Google Maps for your domain name. Once this is done, fill in your User ID in the JSON query string and the API Key in the Google Maps javascript call.

Sending Text Messages w/ PHP

Sending text messages from your web site can be great for security, It would be nice to beable to make a return on it aswell, so your not forking out hundreds of pounds on text messaging, but thats up to you to manage.

ClickATell offer an amazingly simple set of APIs for sending text messages via their network, along with very affordable text messaging bundles.

The thing I use text messaging for is again, server monitoring. Whenever a server of mine goes above a certain load level, a text message is fired off to me to alert me. This actually saved the day – on Christmas day – last year, when there was a hard drive failure on a clients server, I got a text at the breakfast table.

	## DEFINE ACCOUNT USERNAME & PASSWORD
	$user = '<-- username -->';
	$password = '<-- password -->';

	## DEFINE ACCOUNTS API ID
	$api_id = '<-- api id -->';

	## BASE URL
	$baseurl ='http://api.clickatell.com';

	## MESSAGE TO BE SENT
	$text = urlencode('<-- message to be sent by text -->');

	## PHONE NUMBER TO SEND TO, STARTING WITH COUNTRY CODE (4477425235235)
	$to = '<-- recipients phone number, starting with country code -->';

	$url = $baseurl.'/http/auth?user='.$user.'&password='.$password.'&api_id='.$api_id;
	$ret = file($url);
	$sess = split(':',$ret[0]);

	if($sess[0] == 'OK') {

		$sess_id = trim($sess[1]);
		$url = $baseurl.'/http/sendmsg?session_id='.$sess_id.'&to='.$to.'&text='.$text;
		$ret = file($url);
		$send = split(':', $ret[0]);

		if($send[0] == 'ID') {
			echo 'Message Sent';
		} else {
			echo 'Message Failed To Send.';
		}

	} else {
		echo 'Auth failure :/';
		exit();
	}