Category Archives: Social Networking

Creating a JSON API

I was assisting someone recently in building an API for their website and it occurred to me that while the current trend is generally using XML/REST/SOAP for APIs, there is quite a bit of benefit to using plain old Javascript and JSON.  Most users won’t venture into API territory, so if your goal is to make your API accessible – and this goes double if your primary purpose is embedding content in a third party site – it’s hard to argue with Javascript.

The Phish.net API, for example, is a simple HTTP request to an endpoint that returns JSON.  If you provide a callback function name as an argument, and then pre-define that function, it will return the contents wrapped in a function call.  In short, if you define a function called “example()” that accepts JSON an array/object as an argument, then by requesting the API with a callback of “example”, the response will be returned like so:

example(json response);

The benefit, of course, is that it allows a user to embed your code easily. If you host your own callback functions, you can very easily walk a user through a data embed. For example, on Phish.net, we offer the band’s latest setlist.  So we host a callback called pnet3setlist().  Then we offer the API response.  Embedding the setlist is literally as easy as this:

<script src="http://api.phish.net/callbacks/pnet3-setlist.js" type="text/javascript"></script>
<script src="https://api.phish.net/api.json?method=pnet.shows.setlist.recent&amp;amp;callback=pnet3setlist" type="text/javascript"></script>

I’m going to dig more into this later, but the gist of this is that the API can be constructed in a way that allows novice users to interact with it in their WordPress- or Blogger-blogs with little to no modification, it allows semi-skilled users to modify it slightly and tweak it for display purposes, and it allows advanced users to control virtually all aspects. Since virtually every major web language can encode and decode JSON (Javascript, PHP, Ruby, ASP, etc), it’s a near-universal way to exchange data. It doesn’t carry the overhead or complexity of XML, nor does it have the limited scope of something like serialized PHP.

Javascript-based APIs may not be the ultimate solution to a fully interconnected web, but they’re certainly going to be one of the best and simplest methods of data exchange for the foreseeable future.

The Wobbly World of Corporate Tweeting

Update: Please see the bottom of this post for the latest.

I am feeling spurned.  I used to follow my local news station, Central Florida News 13, on Twitter.  A few days ago, I realized that I wasn’t seeing their tweets anymore.  Anyone who knows Twitter knows that its performance has been a huge pile of suck lately, so I figured it was one of those Twitter bugs.  I went to their page and it was full of garbage: pages and pages of the same nonsense message.  Had they been hacked? Caught that Twitter worm?I tried to follow them about 5 times, but it wouldn’t let me.  Broken Twitter.  Nothing new.

Today I saw that they had posted a new update because someone re-tweeted it.  So I navigated over to their page and found that they had protected their updates.  I hit the button to  request to follow them, only to be greeted with the following:

Blocked!

What the eff? They blocked me? Why? Why would they block me?

So let me ask: in what case does it make sense for a corporation or business to block a normal user.  I must say, I am pretty upset, upset enough that I am seriously thinking that I won’t be watching channel 13, which I normally watch almost every single day, anymore. If they will single me out as someone who can no longer enjoy their service, why would I be a patron in front of my TV?

This is probably trivial and I should ignore it, but instead, I’m going to blog my new-found hatred for Central Florida News 13 and their Twitter team. Boooooooo!

Bitch, Please!

So, knowing that doing so has lost them a viewer, does that make the world of Twitter a little scarier? Would people stop listening to a band, watching an actor, reading an author, if they expressed a feeling or idea with which the reader didn’t agree? Would companies feel that Twitter is safe ground knowing that tweeting the wrong thing or blocking a user could result in loss of business or audience?

Update, 4/20/09: Turns out it was – unsurprisingly – a Twitter error. I got a DM and a tweet from them, and they followed me. I can’t understand why Twitter can’t seem to get their crap together. You KNOW they aren’t getting as much traffic as Facebook. With $55 million+ in the bank, you’d think they could build the right infrastructure.

How to Add Latest Tweet to WordPress (Without a Plugin)

I decided to add my latest “tweet” from Twitter to the sidebar of my WordPress blog. Rather than use yet another plugin that adds yet another hook – and there are many that do this with lots of code, I decided to use a homegrown solution, dependant only on PHP4+ and cURL  (most webhosts already have cURL compiled in, if not, you should request it).  Adding the following to any of the files in your WordPress theme will print out your current Twitter status and cache the results so you don’t hammer their system.

First, snag your Twitter user id.  Then, open up your theme file.  I put mine in sidebar.php found in /wp-content/themes/<THEMENAME>/.    Use the below code.  If you want the output wrapped in a list, you would need to put <ul> and <li> tags around this code.

Carefully set your variables.  The cache file should be writable.  Note that you can use a decimal value for $tw_BlankAfter and $tw_Minutes if necessary.   That’s it.

Due to what must be a bug in WordPress, please ignore the closing “</text></created_at>” at the end of this post.  It’s trying be smart and “fix” broken tags, but the code is right.

NOTE (2/20/09): I have updated the below code.  The new version can be found at “Posting Your Latest Tweet in WordPress“.

/* ~~~~ Custom Twitter Bit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~ Adam S, firsttube.com, twitter @sethadam1 ~~~~~~~~~~~~~~~~~~~~ */
 
$tw_File = '/path/to/a/static/writable/file/twitter.html';
$tw_Userid='XXXXXXX'; //set to your Twitter user id
$tw_BlankAfter = 30; //blank out status if it's older than this many days
$tw_Minutes = 10; //minutes between reloads
 
$tw_Offset = FALSE; //leave as is
// uncomment below time if you want to allow a manual reset via ?twitter-reset
// if($_SERVER['QUERY_STRING']=='twitter-reset') { $tw_Offset=0; } 
 
/* Do not edit below this line */
if(filemtime($tw_File)&gt;time()-floatval($tw_Offset)) {
	include $tw_File;
} else {
	if(is_writable($tw_File)) { $tw_iswritable=1; }
	$tw_time = (86400*floatval($tw_BlankAfter));
	if($tw_Offset) { $tw_time=$tw_Offset; }
	$tw_hyperlinks = true;
	$tw_c = curl_init();
	curl_setopt($tw_c, CURLOPT_URL,
		"http://twitter.com/statuses/user_timeline/"
		.intval($tw_Userid).".xml");
	curl_setopt($tw_c, CURLOPT_RETURNTRANSFER, 1);
	$tw_src = curl_exec($tw_c);
	curl_close($tw_c);
	preg_match('/(.*)&lt; \/created_at&gt;/', $tw_src, $tw_d);
	if(strtotime($tw_d[1]) &gt; time()-$tw_time) {
		preg_match('/(.*)&lt; \/text&gt;/', $tw_src, $tw_m);
		$tw_status = htmlentities(str_replace("&amp;","&amp;",$tw_m[1]));
		if( $tw_hyperlinks ) {
			$tw_status = ereg_replace(
			"[[:alpha:]]+://[^&lt;&gt;[:space:]]+[[:alnum:]/]",
			"<a href="\">\\0</a>",
			$tw_status);
		}
		$tw_output = $tw_status;
	} else {
		if($tw_iswritable==1) {file_put_contents($tw_File,''); }
	} 
 
	if($tw_iswritable==1) { file_put_contents($tw_File,$tw_output); }
	echo $tw_output;
}
/* ~~~ /Custom Twitter Bit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Please note that portions of this code come from the twtter_status() function that was not written by me, but is available from various sources online.

Update: Removed function and put code inline.

Back to Google Reader

I’ve chronicled my adventures with Bloglines before, several times, in fact.  I was not happy when their new “beta” was released, but after several revisions, it proved to be a worth successor.  Some time ago, I switched over full time to the beta version and never looked back.  It’s better looking, smoother, with a much more modern feel to it.  However, from time to time, it’s done weird things.  

Most recently, I realized that it simply stopped updating certain feeds.  One, in particular, was TUAW.  I later found that TUAW had moved their feed to Google, and were 301 redirecting requests to their RSS URL, http://tuaw.com/rss.xml.  Bloglines is supposed to follow 301s, but in this case, it just stopped updating the feed.  Other feeds has items that were clearly missing.  All of this came to a head yesterday when I was having regular troubles just getting into Bloglines at all. 

The lack of any sort of Sync API and the lack of tools being developed around Bloglines forced me to make a decision: am I going to stick to Bloglines, which has worked well for me for a long time now, or jump ship? 

Suffice it to say, I’m back on Google Reader.  The things that really annoyed me are mostly fixed: the site is much faster and smoother than before.  My only gripe is that when I click on a feed, the items must be scrolled past in order to be marked read.  I preferred the Bloglines “classic” way, which was clicking on a feed immediately marked all items as “read.”  

Anyway, we’ll see how things go with Google Reader.  You can be certain I’ll report back on the situation.

Facebook Blows My Mind

Everyday, I’m a little more amazed and impressed by Facebook. The way it works, the way information is connected is just so useful and really smart.

First of all, I am simply blown away at the “friend suggestions” that Facebook offers me. I am constantly seeing people I really do know or names I recognize. I understand it’s probably not a very complex algorithm – see who my friends have recently friended who is not on my friend list and present them to me – but you know what? – it works. And that other site is miles behind, tech-wise.

I’ve also noticed that the number of people in my age group on Facebook is seemingly tripling every day.  I can’t believe the people I’m coming across these days – people I haven’t even thought of in over a decade are cropping up regularly.  And the fact that the “mini-feed” is keeping me informed of what they do via pictures and blurbs is just genius.

There are plenty of concerns about Facebook, from their Beacon debacle to the privacy issues to the data mining they are presumably doing, but the truth is that they are providing an amazing service in the meantime.

In the past few days, I’ve had some really interesting conversations pop up on some photos I posted.  The interesting part is that these are people, in some cases, I haven’t spoken to in years, and others I still talk to, but in all cases, it feels like a mini reunion.  I get to keep up with my friends easily.  I’ve not experienced another social network that has delivered like this, for me.