Parsing PUT requests in PHP

I’m working on the next generation data access layer for Digg right now, which is basically a REST layer built on top of a partitioned and multihomed database setup. The general idea is that we’ll send GET, POST, PUT and DELETE requests to URI’s on our services layer to access and manipulate data. PHP makes accessing GET and POST easy via $_GET and $_POST. DELETE isn’t an issue since what we’re deleting is just the entity defined by the URI (e.g. Sending DELETE to /2.0/User/1234.xml will delete User 1234).

After a few days work I can create, fetch and delete entities from this setup. Today I started working on implementing the PUT method. I always knew PHP wasn’t exactly top notch when it came to PUT support, but I had no idea how annoying it would be to find a simple solution for parsing PUT information. After some digging around this is what I’ve figured out.

$put = array();
parse_str(file_get_contents('php://input'), $put);

That should parse everything into a native PHP array, including arguments like foo[bar]=1&foo[baz]=2. If anyone knows of a more native way of doing this please let me know.

Presentations from MySQL and FOWA

I’ve put up my slides for both MySQL and FOWA in a newly minted presentations directory on my site. I also have almost all three hours of audio from my FOWA workshop. You can also view and embed my slides by checking out my page on SlideShare.

I don’t have slides from SXSW because it was a panel without slides. If anyone has audio from my SXSW panel or my MySQL presentation please contact me at joe [at] joestump [dot] net.

Army and Marines welcome terrorists into their ranks

In what has to be the richest ironic twist I’ve seen in my life a CNN article titled “Army, Marines give wavers to more felons” it’s reported that both the Army and the Marines have decided to go ahead and let known and convicted terrorists join them while fighting their fellow terrorists.

Recruits were allowed to enlist after having been convicted of crimes including assault, burglary, drug possession and making terrorist threats.

It's not the language stupid

I’ve said it once, I’ve said it twice, I’ve screamed it from the top of mountains and yet nobody listens. I’m sitting in a session at the MySQL Conference and the person presenting just said, “You have to have well written code to avoid bottlenecks.” This is, put bluntly, stupid and patently false. Let me explain.

  • Your true bottlenecks when scaling are very rarely, if ever, because of your language. Sure Ruby is slower than PHP or Perl or Python, but only incrementally so and it’s only going to get faster. Even if your language is your problem it’s the easiest part of your architecture to scale; add more hardware.
  • Just because your code is well written doesn’t mean it will perform well and, conversely, just because you write shitty code doesn’t mean your code will not perform well. I’ve seen some seriously shitty PHP code that’s blazing fast because it’s so simple.
  • Depending on your application, as you grow you’ll find that your scaling issues come down to one fundamental problem: I/O. DB I/O, file system / disk I/O, network traffic, etc, etc. Ask anyone who’s written a large scale application where their growing pains were and I’ll bet my last dollar it wasn’t “PHP/Python/Ruby/Perl/Java/COBOL is slow”. I’m betting they’ll say something along the lines of “MySQL took a crap on us after we hit 200,000,000 records and had to do date range scans.” Or they’ll say, “I was storing user generated content and NFS couldn’t scale to the amount of requests for that content.”

I’m sick and tired of the language zealots who say PHP is slower than Perl or Ruby is slower than PHP or Java¬† sucks because which language you’re using has zero to do with that missing index on your table or the fact that you can’t store all of that user generated content.

It comes down to your architecture and, despite what the zealots would have you believe, the language you choose is only one component of your overall architecture. Choose what you know and run with it.

Testing PHP/MySQL Applications with PHPUnit/DbUnit

  • You have until August 8, 2008 to move away fromPHP4.
  • We already use PHPUnit and PHPT at Digg, but this DBUnit thingy has me interested.
  • What should you test?
    • Backend: business logic, data access layers, reusable component/libraries, etc.
    • Frontend: form processing, templates, rich interfaces (AJAX/JSON), feed, web services, etc.
  • Use browser based testing (Selenium / Watir) for interfaces in your acceptance tests.
  • Use unit tests (PHPUnit or PHPT) for parseable results / libraries.
  • Developer tests are tests that developers make to ensure the code works as designed. Acceptance tess are tests that someone makes to ensure it works as the client expects it to.
  • Requirements for proper testing include a reusable test environment, automatic execution of test code, easy to learn/use, etc.
  • Unit tests: PHPUnit, SimpleTest
  • System tests: Selenium (PHPUnit + Selenium RC), Watir
  • Non-Functional tests: ab, httperf, JMeter, Grinder, OpenSTA, etc.
  • Security: Chorizo.
  • PHPUnit_Extensions_Database_TestCase is a port of the DBUnit extension for JUnit to PHPUnit.
    • Used for DB driven projects
    • Puts your DB into a known state between test runs.
    • Avoids problems with one test corrupting the database for other tests.
    • Has the ability to export and import your database to and from XML datasets.
    • You can use tableEquals() to compare a table created / modified by a test to either an array of expected records, an XML file or another control table. Hot.
    • It outputs diff’s between the two tables for inspection. Hot.
    • Avoid testing against MySQL (use SQLite) to avoid using up server resources, inter-process communication, etc. Make sure your SQL is compatible with SQLite. Tests are much faster when ran against SQLite.
    • Tests that only test one thing are more informative that tests where failure can come from many sources.
    • Check out the book xUnit Test Patterns.

One thing that's wrong with America

From an article on Mother Jones titled “Am I a Torturer“:

In survey after survey, as many as two-thirds of Americans say torture is justified when it’s used to get information from terrorists. In an abc/Washington Post poll in the wake of the 2004 scandal, 60 percent of respondents classified what happened at Abu Ghraib as mere abuse, not torture. And as recently as last year, 68 percent of Americans told Pew Research pollsters that they consider torture an acceptable option when dealing with terrorists.

If standing someone up on a box, putting a hood on their head and then attaching electrodes to various parts of their body isn’t torture I sure as hell don’t know what is (Satar Jabar, by the way, was charged with car jacking and not terrorism).

There’s a troubling trend in America, in my not-so-humble opinion, and that’s that we’re in a sort of dark ages about what’s considered “liberal”. The following things will get you labeled, at best, as an idealistic hippie and, at worst, a terrorist sympathizer in this country.

  • That everyone, everywhere should have access to clean air and clean water and, in order to guarantee such things, we should put heavy environmental restrictions on both corporations and the federal government (the worst polluter in the world by most standards). As a guy who heavily supported using environmentally friendly detergents once told me, “Republicans don’t want to regulate what corporations and the government puts into the water supply. Who the fuck doesn’t want clean water?” Indeed, who doesn’t?
  • That war is bad. Since when did we go from being a neutral country to being the global bully willing to blow anyone and anybody up? I don’t disagree with war. I fully support what we did in Afghanistan and the recent strikes in Somalia. I just don’t see why everyone is so pumped up about war. It’s shitty and leads to us being perceived as total assholes.
  • That everyone should have basic access to some sort of health care. Humans are exceptionally empathetic. We’re always willing to lend a hand to someone who’s sick, but Americans will be damned if we help those lazy welfare bastards stay healthy so they can just mooch more off of our hard earned money. Guess what? I know people who make “good money” ($40, 000 to $60,000) that can’t afford health care for themselves and their families. We also have about 8,000,000 uninsured children.
  • That the death penalty is both hypocritical and inhumane. I, literally (and horribly), can’t wait for the first innocent person to be executed. Personally, I think it’s already happened and we just don’t know it yet. But, mark my words, in our (Gen X’ers) lifetime we’ll see a man or woman who has been killed (murdered?) by the government posthumously exonerated with DNA or other forensic evidence. I, personally, can’t stand the thought that my government might kill someone someday who was innocent. I’d much rather we just lock them up and throw away the key. Also, the death penalty cases cost us a bunch of money in comparison to just locking them away for life.
  • That the War on Drugs isn’t helping. We spend about $19 billion a year on the War on Drugs (and untold more on prostitution). I say we legalize it, regulate it and tax the shit out of it. I’m a huge fan of vice taxes as we’re basically taxing the people who have the highest burden on state services. To put it another way; we spent just over half of that ($56 billion) on the entire Department of Education in 2006.
  • That you don’t have a right to tell someone else what they can and can’t do with their body. This covers everything from who inserts what into whom, who has which procedures and what people put knowingly into their own body. Stay the hell away from my body. It’s not yours; it’s mine.

Call me a tree hugging, terrorist loving, dirty ass hippie if you want, but I believe the above and think it’s ludicrous that I’m a “dirty hippie” rather than a “a person with common sense”. So, how do we fix it? I think the following would be a pretty good start.

  • Abolish the death penalty with a Constitutional amendment.
  • Legalize drugs and prostitution. Once that’s done heavily regulate them in the same manner as tobacco and alcohol along with heavy taxes.
  • Sign the Kyoto Protocol along with drastically increasing the EPA’s funding. Interesting side note is that the EPA was created by Richard Nixon (I use this little nugget to warp my fellow liberals’ minds).
  • Drastically cut military funding. Use that money for medical, technical and environmental research. Or build some huge ass solar power plants in Nevada.
  • Raise the emission standards on cars to 100mpg (phased in over, say, a decade). We have the technology now and this measure would create a little more pressure on those idiots at Ford and GM to get moving on that. VW is about to release a car that gets 70mpg so it’s plausible right now.
  • Create a Constitutional amendment that secures the rights of one’s own body in all matters. I’d throw in something about not discriminating people based on sex, color, religion, sexual orientation, etc.
  • Create a national health care system that covers children under the age of 18 for all medical costs. The coverage would be lowered to “simple” medical coverage for adults (e.g. You could get your broken arm fixed and medicine for your cold, but that $1,000,000 cancer treatment will require outside insurance). Australia and New Zealand have a two tier system like this and, according to one former US citizen in NZ told me, it works fine.

If that makes me a hippie then so be it.

America's unlikely defender

No one has the right to take the life of another. No crime, no feeling of revenge, justifies that. Society has a right and a duty to isolate men and women who have caused harm, and may cause harm again, but to take their lives is unnecessary, unuseful and blasphemous. If you believe in God, life belongs to God. If you don’t believe in God, life belongs to oneself. It does not belong to the state.

The wonders of modern medicine

I’ve been living with a herniated disc for about four years and sciatica for the last 1.5 years. Anyone who’s ever lived with lower back pain or sciatica knows how debilitating it can be. It’s like a little dark cloud hovering over your head and raining on your parade. It makes sitting more difficult, walking more painful and strenuous activities impossible without medications (which are horrible for your liver and kidneys).

My problem was that my L5 disc was protruding out of my spinal column and compressing my S1 nerve. The result was constant dull pain and pressure in my lower back, shooting pains down my right leg and numbness and pain in my right foot. The lack of nerve activity also suppressed normal soreness and pain, which isn’t good either.

I finally was able to get all of this rectified yesterday during a surgical procedure known as Percutaneous Transforaminal Endoscopic Spine Surgery. In other words, the surgeon went in with a tiny scope, inserted a needle into my disc and used an instrument to vaporize part of the disc meat behind the herniation. Imagine tunneling under a hill, digging out a cavern under the hill and the hill collapsing into the cavern. In addition to this he put a bit of steroids in there for inflammation and filed out a bit around the bone where the S1 nerve comes out so it’d have a little more room to breath.

The results are staggering. I woke up and it was like someone switched the pain off. It’s magical to say the least. Within about two hours of waking up I was walking around my hospital room and another hour after that I was home on my couch watching TV. I even walked to get my prescriptions filled at the local pharmacy.

The recovery calls for a week of taking it easy and then another week of physical therapy after which I’ll continue strengthening my core (abdominals, lower back, thighs) at the gym. Within a month I’m hoping to be back on a bike.

I’ve got a new lease on life and I can’t possibly describe what that feels like. Thanks to everyone for their support throughout this entire thing!

Upcoming Speaking Engagements

I’ve got a few upcoming speaking engagements. If you’re going to be at either of the following events please let me know and we can meet up for drinks or whatever.

  • I’m going to be giving a workshop at FOWA in Miami titled Improving the Performance of Your Web App on Feb 28th.
  • I’ll be speaking at the MySQL Conference in April in Santa Clara, CA. My talk will be a short 45 minute tutorial on “Services Oriented Architecture with PHP and MySQL“. I’ll be covering how to make parallel and asynchronous data requests over HTTP. Code samples, best practices, etc.
  • I’ll be on a panel at SXSW titled “Considerations for Scalabale Web Ventures” with Cal Henderson of Flickr, Chris Lea of Media Temple and Matt Mullenweg of WordPress. It’ll be moderated by Digg’s own Kevin Rose. We’ll talk about caching, scaling out, best practices, etc.