Stopping CodeIgniter from Escaping SQL

I’m adding some small features to the API for joind.in when I have a moment and this is my first experience of working with CodeIgniter. I’ve been getting increasingly impatient with its tendency to try to escape my SQL code for me – this is a really useful default feature but it seems to assume I don’t know what I’m doing and so it puts backticks all over perfectly acceptable SQL code, very annoying!

One night when I was getting exasperated with it tangling up my SQL expressions, I tweeted my frustration in the hope that I was just missing something simple. A prompt reply from @damiangostomski told me that this was indeed the case … I dug around for the API docs on codeigniter – it’s an established framework and has a good reputation. I knew it would have API docs even though I hadn’t used the framework before, and I found them:

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

That quote is from this API docs page – so a big thankyou to Damian for replying to me on twitter, and to the good people at codeigniter for adding a useful option to their framework and documenting it so nicely :)

Speaking at PHPNW February

If anyone is able to make it to the PHPNW User Group meet in Manchester next Tuesday 2nd February – I’m the speaker there! I’ll be giving a talk entitled “Best Practices for Web Service Design”, which covers lots of information about web services and how to write one that your users will love! Details of the event are over on upcoming, you can find out more about the talks, the venue and the group as a whole. If you’re able to make it then I’ll see you there – its a good crowd :)

Three Ways to Make a POST Request from PHP

EDIT: A more modern take on this topic is here http://www.lornajane.net/posts/2018/make-a-post-request-from-php-with-guzzle

I’ve been doing a lot of work with services and working with them in various ways from PHP. There are a few different ways to do this, PHP has a curl extension which is useful, and if you can add PECL extensions then pecl_http is a better bet but there are a couple of different ways of using it. This post shows all these side-by-side.

POSTing from PHP Curl

This is pretty straightforward once you get your head around the way the PHP curl extension works, combining various flags with setopt() calls. In this example I’ve got a variable $xml which holds the XML I have prepared to send – I’m going to post the contents of that to flickr’s test method.

$url = 'http://api.flickr.com/services/xmlrpc/';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

First we initialised the connection, then we set some options using setopt(). These tell PHP that we are making a post request, and that we are sending some data with it, supplying the data. The CURLOPT_RETURNTRANSFER flag tells curl to give us the output as the return value of curl_exec rather than outputting it. Then we make the call and close the connection – the result is in $response.

POSTing from Pecl_Http

Pecl_Http has two interfaces – one procedural and one object-oriented; we’ll start by looking at the former. This is even simpler than in curl, here’s the same script translated for pecl_http:

$url = 'http://api.flickr.com/services/xmlrpc/';

$response = http_post_data($url, $xml);

This extension has a method to expressly post a request, and it can optionally accept data to go with it, very simple and easy.

POSTing from Pecl_Http: the OO interface

Finally let’s see what the OO verison of the extension looks like. Exactly the same call as both the above examples, but using the alternative interface, means our code looks like this:

$url = 'http://api.flickr.com/services/xmlrpc/';

$request = new HTTPRequest($url, HTTP_METH_POST);
$request->setRawPostData($xml);
$request->send();
$response = $request->getResponseBody();

This example is quite a bit longer than the previous one, and you might think this indicates that this approach is more complicated. In some senses that is true and its probably overkill for our extremely trivial example. However it is worth mentioning that the pecl_http extension is extremely flexible and powerful, and can handle some cases that the curl extension can’t. So even if it looks more complicated here, it can still be an excellent choice to implement.

In Conclusion

That was a very fast round-up of three ways you could make an arbitrary web service call from PHP – hopefully these examples are clear and will help anyone just starting to implement something along these lines.

Dutch PHP Conference: Call for Papers Now Open

There is an announcement over on the DPC (Dutch PHP Conference) website – their Call for Papers is now open (so go submit!). What’s remarkable about this announcement is that I wrote it, and its signed with my name and the words “Your host this year” … yes, I’m hosting DPC.

I’m pretty excited about this, I love getting involved with events and I also love DPC as an event, so together these are pretty special. DPC is organised by my employers, Ibuildings – so I actually get paid to get involved with this conference, which is pretty cool :) The submissions have already started coming in to the call for papers and the quality and variety of the talks, from people I know well and others I’ve never heard of, is staggering. I’m hoping that this trend continues right through until the CfP closes on 31st January. The task of choosing the tasks will be very difficult but we have a panel of selectors ready to step up to the challenge – and I’m already excited about how good this year’s event is going to be!

Speaking at TEK·X

I’m always pleased to be accepted as a speaker but I’m especially delighted to hear that I’m speaking at TEK·X in Chicago this May. They had a crazy number of submissions for the number of slots available, and I really wanted to go since I spoke there last year and enjoyed the event hugely! This year I’m giving the following sessions:

PHP Best Practices (tutorial) – This is a half-day tutorial with my good friend Matthew Weier O’Phinney covering all sorts of good stuff you can do when you develop PHP. Its a general session and the aim is that everyone in the room takes away something new from our tips and tricks (and stories of what *NOT* to do!)

SVN in a Distributed World I’m giving this talk for the first time, looking at how traditional source control (subversion) compares with the newer distributed version control solutions (git, bzr). There’s been lots of buzz around git but in the PHP world we choose our tools on merit, not on cool factor, so this is a chance for me to share my experiences with both types of systems and talk a bit about which scenarios the various tools are a good fit for.

Open Source Your Career Another new talk! This one is about how much personal gain there is being an open source contributor. I’ve grown hugely, both personally and professionally, from my experience with user groups, events, and software in the open source space – so I’ll be sharing some tips on how things can work out well all round.

If you’re going to the conference, then do make sure to stop me and say “hi” – there are so many people at these events that sometimes I miss out on meeting people I’d like to have spoken to. You can’t miss me, I’m the woman with the English accent and curly hair!! I had an absolutely great time last year and I’m already looking forward to this year’s conference!

Speaking at PHPUK

I’m pleased to announce that this year I’ll be speaking at PHPUK in London in February. I’ve attended this conference for the last three years, and attend its related user group, PHP London whenever I can find a reason to be in London on the right day. My talk this time is a brand new one, “Best Practices for Web Service Design”, which covers the main points (and pitfalls!) of architecting a web service to be as robust and useful as possible. This is something I’ve been doing quite a bit of in my day job lately and I’m hoping to pass on some of what I’ve learned.

This conference is well-established and I’ve had a blast most years I’ve attended! Although their schedule isn’t public yet (it will be soon), the other sessions I’ve heard about on the grapevine sound good. If you want to attend, the date is Friday 26th February and you can buy your tickets on their site. Let me know if I will see you there :)

PHP Advent 2009

I’m very proud to be able to say “I’m a PHP Advent author” – I’ve been invited to take part in this year’s event and my article One Step at a Time is now live!

My post this year is aimed as a reminder to us all that we can all aspire to better things, and lots of “better” eventually adds up to “pretty damn good”! If you read the post and have comments, add them here – and if you’ve chosen what one thing you’d like to change next, I’d be delighted to hear it. Whatever your next step, good luck :)

Speaking at PHP Benelux 2010

I’m delighted to announce that I’m speaking at the inaugural PHP Benelux Conference, to be held on Saturday 30th January in Antwerp, Belgium. The talk will be “Passing the Joel Test in the PHP World”; I gave this talk PHPNW09 in October and it was well-received there, so hopefully I can bring the same insight and inspiration to attendees at this new event as well!

On a personal level I’m very pleased to have a reason to visit the Low Countries – Ibuildings is a dutch company and I’m already making plans to link up with my colleagues there by extending the trip by a few days. I’ve also never been to Antwerp so I’m hoping I’ll see something of the city while I’m there, if time allows. The benelux user group contains many friends so I’m looking forward to what I know will be an excellent event and catching up with all the friends who will be there.

If you are attending, or thinking of it, let me know – and come and say “hi” to me on the day :)

Conference Biography Help

I’ve been updating my conference details recently, in order to submit my talks for php|tek in Chicago (the call for papers closes on Monday – get your submissions in!). One thing which I struggled with is my biography, I used to have a paragraph which sort of said “Lorna is a PHP Developer and involved with PHPWomen”, and I used that same entry for every conference for a year or more. However, just like speaker photos, biographies do date. I’ve taken on more responsibilities at work and I’ve been doing more things in the community as well so it was time for a refresh.

I’m quite happy with my new bio:

Lorna Jane Mitchell is a senior developer who speaks, writes and blogs on a variety of technical topics. At Ibuildings she runs the PHP Academy, meaning she’s involved in managing and coaching trainers, hosting seminars and conferences, building a training programme and representing Ibuildings within the PHP community. Lorna is the Editor-in-Chief at Ibuildings techPortal and blogs regularly at lornajane.net. In her spare time she is the European Representative of PHPWomen and is an organiser of the PHPNW user group and conference.

Getting This Far

To get to this point, I started with a list of things I should include. My job, my blog, my community activity, my technical interests. There’s definitely scope for including unexpected information here, I’m seriously thinking of adding my knitting hobby into this paragraph!

I then turned my points into sentences, and emailed the result to a few people to read. Even if you’re secretly hoping someone else will write your bio for you, its often easier for them to criticise something you have written than to start from scratch themselves. I always take this approach even when I know I’m probably making a hash of it, if I’m asking for someone’s input, I take the time to attempt it myself and send them the result. I’m enormously grateful to everyone who has reviewed my biographies and talk proposals, and I’m always happy to do the same for others when I can find the time.

Proofreaders can pick up spelling mistakes and help you put your best foot forward, it might be embarrassing to write about yourself but is it more or less embarrassing than having a lame biography printed in a conference programme?

Announcement: Editor-in-Chief of Ibuildings Techportal

A few weeks ago I got a call from my employers, Ibuildings, asking me how I felt about changing my role a bit and taking on some of the functions of our PCE (PHP Centre of Expertise). This area of the company does some super-cool stuff and so I said I’d be interested. Fast forward a bit and I’m on a call with Ivo Jansch (our CTO, who also oversees PCE) talking about what kind of things I could be involved in. I cannot describe the surprise I felt when he asked if I would take on the role of Editor-in-Chief at our developer portal site, techPortal … and of course I jumped at the chance.

I’ll be picking up a few other community-facing functions for Ibuildings but techPortal is the headline news, I’m super-excited to be entrusted with this project as our existing Editor-in-Chief, Cal Evans moves on from Ibuildings. Now the announcement has been made I guess its real … wish me luck :)