Facebook Credits, the Order ID, and dealing with large integers in 32 bit PHP

So, currently at work I’ve been rewriting all our Payment Gateways for Your Members. Making them more better Class based an abstracting out the common functions to a base class, to save memory footprint and code etc.

And came across a problem when rewriting the Facebook Credits handler.

The order ID that gets passed around, as well as the Application ID is too long for a 32 Bit PHP installation to handle when treating it as an Integer.

So a number like “239724439419867” was ending up as “1.7592246582797E+14” when stored/processed, or converted to a string.
Now the obvious way to process this is to convert it to a string another way, which is somewhat difficult since before you even start it is in a format that you can’t handle.

The saving grace here is the fact that data is passed to you as a JSON packet (aside from the fact the Order ID is in the $_POST variable and thus a string, so I could of used it from there).

This led to me to look at the JSON packet, which starts as a string but when decoded the large integers are still a larger integer than can be processed (whether you json_decode to a Array or to an Object, the problem persists). So I thought about how to process the raw JSON packet and make sure the order ID, and other large integers are treated as strings.

If you look at a Raw JSON packet you can easily spot whats a string, integer, object or array.

Take this example Facebook Credits JSON Packet, (wrapped for readability):

{"order_id":239724439419867,"buyer":197803678,"app":148748711865470,
"receiver":197803678,"amount":5,"update_time":1320075413,
"time_placed":1320075408,"data":"","items":[{"item_id":"<item_id>","title":"Post",
"description":"A New Purchasable Post",
"image_url":"<some url>","product_url":"<some other url>",
"price":5,"data":"<data>"}],"status":"<status>"}
  • order_id is an integer as it has no ” around it
  • status is a string as its surrounded by “
  • Items is an array as its surrounded by [, in this case containing a single object, entries/items are comma separated.

So after thinking about this I decided the best way to sort this out was to convert the integers in the raw JSON packet to strings before decoding.

In pseudo code.

Lop off the { and } from the start and end
Explode around ,
array walk each item
split on :
check if there are no " in the second bit
if none wrap in "
glue back together

Something alone the lines of:

function largeint($rawjson) {
  $rawjson = substr($rawjson, 1, -1);
  $rawjson = explode(',' , $rawjson);
  array_walk($rawjson, 'strfun');
  $rawjson = implode(',', $rawjson);
  $rawjson = '{', . $rawjson . '}';
  $json = json_decode($rawjson);
  return $json;
}

function strfun(&$entry, $key) {
  $data = explode(':', $entry);
  if (FALSE === strpos($data[1], '"')) {
    $data[1] = '"' . $data[1] . '"';
    $entry = implode(':', $data);
  }
}

I’m not sure in terms of memory footprint if its cheaper to do a substr of $data[1] and check if its a ” or not.
I suppose it could test the string length of the detected integer to see if its invalid/unable to process, but then you would be performing a string function on an integer and again the problem would arise.

But as a block of code is does the job, its obviously not ideal for all situations, since in this case I want the integer as a string, I’m not using it for math, but if I did, we probably need to do some bizarre unpack-ing or something.

Any opinions or improvements give us a comment below 😀

My Leeds Hack 2 Project

Thought I would write a blog post about what I built in 24 hours for Leeds Hack 2 last weekend. I kinda got distracted last week and not had the time to write this post.

You can look at all the projects that people finished over on the Leeds Hack Website.

So what did I write

I decided to write something that I had been pondering about for a while. It’s called Spotify Roulette and revolves around the idea of crowd sourcing a new artist or area of music to listen to.

Originally it started off with me posting on Twitter quite a while back asking for something new to listen to, to which @Stanton responded with Hybrid. (I can’t find the original tweet on twitter but it’s here on Spotwitfy) Which are actually quite nice to listen to and I have a nice Spotify playlist to listen to.

I was wondering if there was a way to automate this.

So thru the combination of the Twitter and Spotify Meta Data API’s, means I can post out to Twitter, await a response, parse out the Artist and then pop open Spotify with a random track by that Artist and if the requester wants a playlist can be generated, and thru a limitation in Spotify, drag and dropped into Spotify to listen further to the Artist.

In short

In short its pretty straight forward.
Just a handful of calls to a couple of different API end points to get an Artist ID from the name, then their albums and the tracks on these albums. Chuck in a little GeoIP to hopefully check the tracks are available in the requesters’ region.
Grab the first track, pop open Spotify and grab another 10 tracks to make a playlist.

Finally I used Nerf Guns to help demo! Russian Roulette stylee.

Responses

People on Twitter can either response with text, an artist which we parse out the @user and the hashtag word. Then look that up on Spotify.
Or a response can be either an HTTP open Spotify link or a Spotify protocol link. Either to the artist page, track or album.
if it’s the Artist, I can parse out the artist ID and look for albums and tracks.
Currently not playlist urls, but that’s easy to implement.

Technology

So we used,

  • Twitter API, using @Abraham Twitter oAuth Library
  • Spotify Meta Data API
  • MaxMind GeoIP
  • jQuery and jQuery UI
  • Rick Astley
  • Nerf Guns

Give it a Go

Give it a go and offer me some feedback.
Watch out for Rick Astley tho. He likes to crop up every now and again….

It’s still a little rough round the edges in terms of theme/layout.

Spotify Roulette (http://spotifyroulette.com/)

Future

Hopefully if people like it and use it I can expand further.

Perhaps use Facebook to share playlists, or another way to crowd source a new artist. And if Spotify release a HTTP API for generating and saving playlists then incorporate that too.

Travels of a Commercial WordPress Plugin Developer 1

My job at the moment is updating and coding YourMembers, which is a commercial plugin for WordPress.

Recently I’ve been working on a method to Auto Upgrade the plugin, since commercial plugins cannot go on Extend, and there is no commercial equivalent. Extend being the WordPress plugin repository.

Commercial plugins get no nice little “Plugin needs updating Circle” on the plugins tab, or a nice little message on the plugins page, let alone access to the auto updater!

Which leaves us (commercial plugin devs) to write our own version checkers and hook in accordingly.

So now I have a nice little PHP class which will detect whether it can fwrite/rename and if not if it can FTP instead, much the same as the WordPress updater does.

Now this is all well and good, but I needed a method to download and unzip the new plugin file. Grabbing the file is easy, either file_get_contents (heaven forbid), or curl (yay for curl), and write the file (being a zip) to the web servers temporary directory and process from there.

Now the problem here is how to unzip. A Brief look at how WordPress does it seems to show it has three different methods of unzipping. So dump that.

Didn’t feel the need to use PHP’s ZipArchive class, since I don’t need to edit the zip, just extract its contents.

Off to php.net I go and I find some nice handy functions, and quite surprised to find that the zip_read function can handily recurs into directories within the zip file to return their paths/names. Mildly annoying at the time as I had gone and written a recur loop to go inside directories…. and got thrown a error, nice headdesk moment!

So basically the Zip functions, the basic ones of PHP, are actually quite nice, handy and useful!

Heres my code snippet:

<?php
class someclass() {
	private function unpack() {
		$from = $this->tmp_write;
		$to = $this->tmp_out;
		@mkdir($to);
		
		$zip = zip_open($from);
		if (is_resource($zip)) {
			$this->readzip($zip);
			zip_close($zip);
		} else {
			// failed to open
			$this->error = 1;
		}
	}
	
	private function readzip($zip) {
		$from = $this->tmp_write;
		$to = $this->tmp_out;
		
		echo '</pre><textarea style="width: 100%; height: 100px;">';
		
		while (FALSE !== ($file = zip_read($zip))) {
			$name = zip_entry_name($file);
			echo $name . "\n";
			if (!strpos($name, '.')) {
				// is dir
				@mkdir($to . '/' . $name);
			} else {
				// it recurs into directorys on its own!
				$item = zip_entry_open($zip, $file);
				$size = zip_entry_filesize($file);
				
				$read = zip_entry_read($file, $size);
				$fp = fopen($to . '/' . $name, 'w');
				fwrite($fp, $read);
				fclose($fp);
			}
		}
		
		echo '</textarea><pre>';
	}
}
?>

I just installed this code snippet plugin, which I might need to change its background colours…..

Any suggestions for code snippet plugins greatly appreciated!

Summer

So that middle part of Summer has been a bit sucky. So we shall skip all that, don’t really want to go into it….

Now things are better!

I lost my mac for a week and a half, hard drive failed, followed by back for two hours, followed by hard drive fail.
In the end Clockwork (Leeds Apple Care), replaced the drive and the drive’s cable, and shes all good now!

I’m off to Moor Fest next week, I’m doing the lighting for the Green Room, with Chamsys and whatever fixtures I get given, learnt some useful things from their Chamsys Guide for that matter, (and I fixed up the tab box on the home page for them!) Nice bit of jQuery!

Made a shoutbox for Halo3Wheelmen its hosted on 360gaming.net so had to do a lot of fudging and built a mini phpBB api to get user/ban data out of the forum, its all built in jQuery and PHP, jQuery makes ajax stuff, and append/prepend so much easier than pure javascript.


Job wise I am still waiting to hear back from Firebox.com, but it looks like I shall be staying in Leeds for another year at least, being a Venue Tech, which should, with the new boss, hopefully lead to more external work, but I shall still be working as a freelance web developer. Tho there have been some interesting local jobs come up on the GeekUp jobs Board. (I need to start going to GeekUp again….)

In that vein I currently have a bit of work on building a Asset/Event Manager for LUU Events, details on that to follow, but as a project it keeps me out of trouble, since Google fell thru.

Hopefully I should be getting back into modding phpBB which is even easier now since they now use GitHub to host their repository, so I’m gonna have to get to grips with forks, and loading parent data into the child repo…. (If that makes sense).

So essentially things are busy, not perfect but good. Could be better could be worse…

Hopefully at some point I will get around to doing a Carlyon CMS release candidate, tho I am thinking I need to rip out my CSS boilerplate and thus the relevant core html template files, we shall see.

In other news, I now have completely new graphics for 360gaming [dot] net, I was approached and offered the services of O Sheep Dip, and now I have graphics! Woot!
360gaming.net now has a HogBall league tracker on top of the 1v1 challenger system, tho no one has tested it yet, and I only have the one team, people don’t seem interested…

Tho there is a BTBhub on the way, it was suggested to me that building a BTB league is the way to go, and now I have the core Bungie Code checker, and with the advent of Reach, its data API (POTATO!) and of course, its massive ForgeWorld, the future is bright for Halo and 360gaming.net, now all I need is to find more games with similar developer offerings, and getting myself access to the true Xbox API……

So that summarizes the later part of summer… O and Katie is now home.

Also checkout my tumbleblog

Catch you after Moor Fest, (which will be my first ever festival, I’ve never been to one, and now the first one I am going to is also going to be my first external lighting job….)

Off to watch Sherlock on BBC1

And Happy Birthday to Kayleigh (The GirlFriend) 🙂

Long Time Since I blogged

A General Update….

Again, its been a while. Things have been pretty busy.

Currently I just fixed the LSRfm.com Podcasts, they were downloading a minute or two of audio the PHP was dying, FAST CGI php5 was being ghey.

So after a quick chat with Dreamhost over live chat, my script now rather than munch the file, just passes it off to a http location, less of a memory muncher too.

SRAchart is its usual self, tho we did get a full house of Charts last week, (well near enough), Ellie Goulding and Owl City are dominating, and Glee Started Charting too.

(Yes I am a Glee Fan)

Handed in the Motion Graphics Brief.

So Uni Wise I just need to sit down and code code code, both for Project 2 (ala 360gaming.net enhancements), and start planning and researching what I need for project 2.

And start writing an essay for Futures and Strategies, fun times.

I’ve not had any spare time to play Halo, tho the Reach Beta has been announced! Hurrah!

Now I must get back to coding stuff for LSRfm.com fixed the Jukebox, now to do the music db, tho I do have a nice IRC bot sat in the new LSRfm.com Chatbox

Also <3 jQuery....