Context

I'm a big fan of Snipplr. It's an online code snippet storage/sharing service (similar to DZone Snippets or Snipt). Often when I'm writing code and I hit a wall I find myself traveling towards Snipplr, bypassing my usual Google search. I enjoy the succinctness of reading a code snippet as opposed to the bloat that blog posts can have. Code snippet sites also feel like they are solution repositories, you can literally feel the banged heads of past developers who have hit their own walls and been so annoyed by the time they spent solving a problem that they are compelled to help other developers out. I recently spent the good part of two hours trying to figure out some PHP code, when I found the solution to be as simple as needing an "@" symbol (see snippet), I had to post it to Snipplr and tweet it - just in case there was another poor soul like me who could be saved from their own frustration by reading it.

To the point

I wanted to give something back to the Snipplr community, so I've started writing a small PHP library for interacting with the Snipplr API. Currently it has two main methods "get" and "getAll" - I figured for listing snippets on your website/blog these would be the main ones you'd need, however the Snipplr API does also provide methods for adding and deleting, in which case please feel free to extend the library and commit back to GitHub. The library was developed as a CakePHP vendor (so you'll see some "cakeish" looking code) but works just as well in other PHP scripts. Tests have been added for both CakeMate and PHPUnit.

If you want to see an example of it working, check out my about page.

Get the code

Set it up

Get your API key from your Snipplr Settings page.

Snipplr Settings Screenshot

Add your Snipplr API key to snipplr.config.php


	define('SNIPPLR_API_KEY', 'yourapikeyhere');

Test it works

Feel free to dive right into the "Usage" below, but if you have phpunit installed than this two-liner could save some time.


	cd /path/to/snipplr/tests
	phpunit SnipplrCoreTest snipplr.core.phpunit.test.php

If you'd like to also see the output of each method, just uncomment the debug/print_r statements and run again. Also if anyone is rockin an installation of CakeMate then there is a duplicate version of the test available for your testing pleasure.

Usage

Throw these few lines of code in your projects. Also all of the usage details and example outputs are repeated in the README file.


	// CakePHP
	App::import('Vendor', 'SnipplrCore', array('file' => 'snipplr'.DS.'snipplr.core.php'));
	$SnipplrCore = new SnipplrCore();
	$snippet = $SnipplrCore->get('18153');
	$snippets = $SnipplrCore->getAll();
	$languages = $SnipplrCore->getAllLanguages();


	// PHP
	require_once "snipplr".DIRECTORY_SEPARATOR."snipplr.core.php";
	$SnipplrCore = new SnipplrCore();
	$snippet = $SnipplrCore->get('18153');
	$snippets = $SnipplrCore->getAll();
	$languages = $SnipplrCore->getAllLanguages();

Quirks

Here are some of the quirks I faced with the Snipplr API.

  • getAll: Multiple tags don't seem to work in the Snipplr API. Only the last tag will be used.
  • getAll: Limit of 0 or 1 appears to return all snippets (ie, results aren't limited).

Other Notes

I recommend caching your results (it reduced my about page load time from 1.63s to 125ms). In CakePHP this would look like:


	if(($snippets = Cache::read('snippets')) === false) {
		App::import('Vendor', 'SnipplrCore', array('file' => 'snipplr.core.php'));
		$SnipplrCore = new SnipplrCore();
		$snippets = $SnipplrCore->getAll();
		Cache::write('snippets', $snippets);
	}
	$this->set(compact('snippets'));

That's all folks

So there you have it. If you have any cool snippets (no matter how niche they are) that you want to share with everyone, please post your links and stories of frustration in the comments and we'll get some discussion going - who knows you might just save someone else from repeatably banging their head (which is usually a good thing).