Tag Archives: nanowrimo

On Writing

I realize this update is long overdue, but I’ve been so busy I haven’t been thinking about posting on my blog. I apologize for that, and I’ll try (again) to post more frequent updates. Meanwhile, I thought I’d talk about something that has been taking up some of my time recently. I recently started actively pursuing my writing again, wrapping up my first full length novel. (I have a couple short novels written as well, but this is my first 100k+ piece.) Several of you already know about this, since you’re involved. Others may have seen me tweeting about it, but I hope you’ll still enjoy hearing about it. Read more »

Simple NaNoWriMo API Class

As anyone who knows me probably realizes (because I talk about it incessantly) it’s NaNoWriMo time again. Which means I’m revamping my wordwar tool for this year. This time, I’m giving it a complete overhaul. In the process, I looked at how I was using the API provided by NaNoWriMo, and decided I needed to improve it. After all the coding I’ve done in CakePHP in the last year, I realized there was a very very simple way to do this. Not much to say about it, so here’s the code:

class WordCountApiComponent extends Object {
	function __call($name, $arguments) {
		if (count($arguments) == 1) {
			$xml = $this->_request("http://www.nanowrimo.org/wordcount_api/".$name."/".$arguments[0]);
		} else {
			$xml = $this->_request("http://www.nanowrimo.org/wordcount_api/".$name);
		return $xml;
	private function _request($url) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		return new SimpleXMLElement($output);

That’s it. Just two methods. (And I could reduce it to 1, but I prefer to keep the CURL logic in it’s own method.)

Using it is just as simple. All NaNoWriMo API methods use either 0 or 1 arguments, and the method names are simply part of the URL. So, to call the ‘wc’ API, that returns the current wordcount for a single user, taking their UID as it’s only parameter, you’d do this:

$uid = 78110; // My UID
$api = new WordCountApiComponent();
$wc = $api->wc($uid);
echo $wc->uname; // "utoxin"
echo $wc->user_wordcount; // '0'

Hope this helps someone! It’s certainly a nice simple way to interface with the API.