PHP Web Services: 2nd Edition


I’m delighted to announce that the second edition of PHP Web Services is published! This isn’t an entirely new book but in my own biased opinion it is a much better job of this topic than I did the first time around :) Following from the feedback we got on the first book, this edition contains quite a lot more working examples (with code on github) as well as updates for new tools and expansion on newer technologies and practices.

Writing a second edition was nothing like writing a first edition, it’s more like editing with snippets of writing and rewriting thrown in. My heartiest thanks to my tech reviewers who sorted out all kinds of nonsense contradictions and generally asked hard questions during the process – you are all humans of the highest calibre :)

Upgrade To Better Passwords in PHP

The password features in PHP aren’t exactly new, but I see lots of applications from “before” which aren’t being migrated to better practices. I have some strategies for doing these migrations so I thought I’d share my main approach, plus a similar-but-different one I saw in the wild (OK it was in CakePHP, so not too wild!). Continue reading

OAuth2 with PHP’s built in Streams Functions

Most of the time when I work with APIs from PHP, I use Guzzle – it’s awesome and modern and elegant. However some of my work is with legacy platforms and I recently had a situation where we needed to integrate with a API using OAuth2, and launch that integration before the planned platform upgrade from an older version of PHP was expected to complete.

(this drives me nuts, I love upgrading systems but the downside is you have to work with the old ones first and none of the tools you want have been invented yet!)

For OAuth2, all I had to be able to do was to send an Authorization header with my web request from PHP. My second-favourite way of making API calls from PHP is to use PHP’s stream handling, so I did that. It’s not code you see very often but it’s super-simple and it works on every PHP platform I’ve tried so far, so here’s an example:

// assemble the options
$opts = array(
  'http'=>array(
    'header'=> "Authorization: Bearer " . $access_token
  )
);
// create the context
$context = stream_context_create($opts);

// now make the request! Use the context and simply output the result
echo file_get_contents('http://api.example.com/endpoint1', false, $context);

If you’re trying to make an API call from PHP and installing better tools is hard for any reason, this example may help!

API Testing with Runscope

I work with a lot of APIs and I really appreciate good tools. Lately I’ve been really enjoying using Runscope for testing and monitoring my APIs so I thought I’d share how I work with this tool, which has a great one-person free tier *and* the ability to import/export tests so if you need to work with a team with just the free tier (the examples here are from an open source project which is an obvious use case where it’s hard to fund tools), it’s clunky but doable. The idea here is just to show you around how to create your own API tests with Runscope (and also to write down what I did so I can point both my future self and others at this!) Continue reading

Insert Data with Phinx

Database patching is a wicked hard problem, one that’s got a bit easier in my world lately as I’ve been using Phinx on a few projects. I like Phinx because it avoids the numbered-patches problem by using date stamps as part of its patch naming and it is pretty smart about creating the correct forward and backward changesets from a single change() description.

One thing I didn’t immediately find was how to insert data. Continue reading

Generating a File List for Phan

Phan is the PHP Analyzer for PHP 7 code. I’ve been using it, partly out of curiosity, and partly to look at what the implications of upgrading my various projects will be. The simplest usage instructions are:

phan -f filelist.txt

I generated my filelist.txt files with a little help from grep – by looking for all files with opening PHP tags in, and putting that list of filenames into a file. My command looks like this:

grep -R -l "<?php" * > filelist.txt

This simply greps recursively (the -R switch) in all files looking for <?php and when it finds it, outputs only the filename (the -l switch does that bit). Then I just put all the output into my filelist.txt file.

Phan is in its early stages but it’s ready for you to run on your own projects. Look out that you may need to put your bootstrap or other include files first in the filelist.txt file if phan isn’t finding things in the right order – luckily with it all in one file, it’s relatively easy to move things around if you need to.

Git Won’t Check Out A Path It Autocompleted

One of my git repositories has developed a tendency to refuse to checkout a feature branch locally that exists on the remote repo. My git bash completion works, but then strange things happen! It turned out to be that I had two remotes with the same refspec, so I thought I’d write down the behaviour I saw and hopefully help someone else to fix this problem faster if they see it. Continue reading

Why My Open Source Project Needs a Code of Conduct

I’m an open source project maintainer, working on the projects associated with joind.in, and recently we added a Code of Conduct to all our projects (we have quite a few as you can see from our github organisation page https://github.com/joindin).

I feel the same way about codes of conduct for open source projects as I do about codes of conduct for events. You can absolutely run a totally safe and effective event without one, but by having one you make very clear what your expectations are – and in turn this manages the expectations of the people attending that event. Continue reading

PHP: Calling Methods on Non-Objects

PHP has subtly changed the wording of this error between various versions of the language, which can trip up your log aggregators when you upgrade so I thought I’d give a quick rundown of the changes around the “call to member function on non-object” error in PHP, up to and including PHP 7 which has an entirely new error handling approach. Continue reading