My Book is Published: PHP Master

PHP Master Cover ShotI am truly delighted to announce that “my” book is published. Technically it’s really “our” book since I had the pleasure of co-authoring with friends and PHP rockstars Davey Shafik and Matt Turland. The book is “PHP Master” and it’s a broad-ranging text covering a series of topics that we felt were relevant to PHP developers who aren’t beginners, but who are looking to fill in some gaps and get a good grounding in creating whole applications in PHP.
Continue reading

Announcing Training Dates for November

I’m teaching some new public courses next month (in Leeds), so I thought I’d mention them on the blog in case anyone is interested. I’m super-excited because this is the first time I’ve delivered my own training content as a public course, so even if you aren’t in the position where you work with lots of people who need the same training, you can pop along! Continue reading

PHP OAuth Provider: Access Tokens

I’ve been working with OAuth, as a provider and consumer, and there isn’t a lot of documentation around it for PHP at the moment so I thought I’d share my experience in this series of articles. This relates to the stable OAuth 1.0a spec, however OAuth2 has already started to be adopted (and differs greatly). This article uses the pecl_oauth extension and builds on Rasmus’ OAuth Provider post. This entry follows on from the ones about the initial requirements, how to how to handle request tokens, and authenticating users.
Continue reading

PHP Static Analysis Tool Usage

Last week I tweeted a poll with the question “Which PHP static analysis tools do you regularly use?”. These are the results:
Static Analysis Tools Poll Results (phpcs 81%, phpmd 59%, phpcpd 59%, phploc 41%, phpdpd 48%)

My interest was mostly because I’m working on a book chapter which includes some static analysis content, and there are a couple of these tools that I include in my own builds, but I don’t do much with the output of them. However I didn’t want to drop anything from the chapter if it was actually a valuable tool and I was just missing the point – pretty much all the tools got a good number of votes though, so I’ll be covering all of the above. It does look as if phploc has less of a following, however it’s one of my favourite tools so it gets a mention anyway!

Thanks to everyone who took the time to vote; I thought I’d share the results in case anyone was interested.

Shortening URLs from PHP with Bit.ly

I’ve been looking around for a really simple API that would be a nice place to get started using web services from PHP – and I realised that bit.ly actually fits the bill really well. They have straightforward api docs on google code, and it’s also a pretty simple function!

Here’s a simple example, using PHP’s curl extension, of using the bit.ly API to get a short URL, using PHP (you need an API key, but if you’re a registered bit.ly user, you can log in and then find yours at http://bitly.com/a/your_api_key).

$ch = curl_init('http://api.bitly.com/v3/shorten?login=username&apiKey=R_secret&longUrl=http%3A%2F%2Flornajane.net');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
print_r(json_decode($result));

Continue reading

PHP Returning Numeric Values in JSON

When I wrote about launching a prototype of a new joind.in API, quite a few people started to try it out. My friend David Soria Parra emailed me to point out that many of the numbers in the API were being returned as strings. He said:

It’s just a standard problem of PHP REST services. When I try to access it with java I have to convert it over and over again to ints.

I did have a quick look at the PHP manual page for json_encode but I didn’t see anything mentioning this. A few weeks later (my inbox is a black hole and it takes a while to process these things) I fell over a throwaway comment to an undocumented constant JSON_NUMERIC_CHECK, and I added the constant name to my todo list. In the time it took for me to actually get around to googling for this, some wonderful person updated the PHP manual page (this is why I love PHP) to include it as a documented option, and someone else had added a user contributed note about using it.

It turns out, this constant does exactly what I need. Here’s a simple use case:

echo json_encode(array('event_id' => '603'));
echo json_encode(array('event_id' => '603'), JSON_NUMERIC_CHECK);

and the output:

{"event_id":"603"}
{"event_id":603}

There are probably some situations in which you don’t want all your looks-like-a-number data to be returned as a number, but for now it seems to be a good fit for api.joind.in.