Using Charles To Debug PHP SOAP

I used this trick to solve a particularly sticky problem last week, so I thought I’d share. I had a 3rd party SOAP service (except it was actually a .NET WCF service, which is almost SOAP but not quite) which didn’t behave in accordance with the way its documentation said it would. PHP’s SoapClient has the ability to enable tracing and give information about the request/response headers/body, which is useful, but I needed to store the outputs and also rewrite a particular request header during the course of debugging this. Enter: Charles Proxy. Continue reading

PSR-What?

There’s been some cool things happening in the PHP world over the last few years, but with the least helpful names ever … yes, those PSR-somethings which all do totally different things (apart from two of them which are the same). They’re actually all superb things, and done for a good reason, so I thought I’d try to translate them into normal speak.

PSR

Let’s begin at the beginning. Once upon a time, at a conference, the lead developers from a selection of frameworks sat down in the same room (they are better at it nowadays, at the time I might not have believed it had I not been there) and agreed some standards for all their projects to use. The aim is to make PHP frameworks and libraries easier to combine for users, and so it was that php-fig: the PHP Framework Interop Group was born. This group of awesome individuals oversee the PHP Standards Recommendations (PSRs). Continue reading

Twitter Search API Using PHP and Guzzle

In case you missed it, Twitter updated their APIs recently, so that you have to authenticate to use even their search APIs to return publicly-available results. This is an increasing trend for API providers, to provide either very limited or nonexistent access for unauthenticated users, I think so they can rate limit consumers that swamp them. To cut a long story short, that meant I needed to update my dashboards that keep an eye on twitter searches to do more than just call file_get_contents in the general direction of the right URL. Continue reading

Ttytter: Command Line Twitter Tweaks

I’ve been using ttytter for quite a while, it’s a superbly inoffensive twitter client – and it’s console-based. This makes it easy to use from the keyboard, fast, and also makes it look a bit less like twitter if that’s not something people should be seeing over your shoulder!

I have customised a few settings which I find superhelpful, so I thought I’d share my config file and say a bit about some of the entries in there. The config for ttytter is held in a file called .ttytterrc in my home directory. Mine looks like this: Continue reading

DC4D 6: Not-Programming for Programmers

In case you haven’t seen the news, the next episode of Day Camp 4 Developers is coming up on Friday 26th July. This edition is about getting beyond being awesome at the code, which we know that you are, and picking up some other skills to complete you as a profesional. Whether you work in a large international corporation, a smaller company, or alone, we’ve got content that will make a difference.

Day Camp 4 Developers is a virtual conference, and it’s $40 (about 25 quid for UK people). If you can’t make it on the day, just get the video ticket and download the recorded sessions later. What I’m trying to say in this paragraph is that there are quite literally no excuses for missing out on this :)
Continue reading

Are Subqueries RESTful?

Twitter is great for one-liners, but it’s very difficult to carry on any kind of advanced conversation there. Therefore when I saw this tweet yesterday, I knew I’d be picking a different medium to reply:

The blog seems like a good place, as I can put examples and all kinds other things here, and waffle at length (which is really why I like it!). Because when condensed to tweet form, the answer is really “it depends”.

The Problem(s)

REST is all about representations of resources. They might come in different formats, and they might appear at their own URI as well as in one or more collections, but essentially you just get a representation of a thing. This is great, apart from when it isn’t.

  • What if you want a smaller result set with only a limited number of fields?
  • What if you want related data? For every resource in a collection?

Continue reading

PHP Version Adoption

PHP runs over 75% of all websites whose technologies are known (source: w3techs), which makes for a really REALLY long tail of users who once installed wordpress, phpmyadmin, or some other open source project that helped their business needs at the time. What they don’t do is upgrade. PHP’s current usage statistics look like this (source and raw numbers are if you want them):

PHP Version Adoption

What’s alarming about this is that the left half of this graph represents unsupported versions of PHP. PHP 5.2 has been end of life since January 2011. This doesn’t mean that you can’t use it any more, but it does mean that in terms of security updates, you are out of luck. Some distributions will try to retro-fit some of the fixes but essentially your PHP applications seem a bit lacklustre because, well, you’re using technology from 2006. Continue reading

Simplest PHP Generator Example

I really like the generators feature that’s arriving in PHP 5.5, and since we’re now into release candidate releases, it’s actually not that far away. I’ve been speaking on this topic and I thought I’d share my trivially simple code example from my slides.

Writing a Generator

The generators use the yield keyword to feed values out as they are iterated over. In code, they really look a lot like a function (or method):


<?php
function getValues() {
// totally trivial example
yield "Apple";
yield "Ball";
yield "Cat";
}

Continue reading

Setting Multiple Headers in a PHP Stream Context

Last week I tried to create a PHP stream context which set multiple headers; an Authorization header and a Content-Type header. All the examples I could find showed headers built up as a string with newlines added manually, which seemed pretty clunky and not-streams-like to me.

In fact, you’ve been able to pass this as an array since PHP 5.2.10, so to set multiple headers in the stream context, I just used this:

<?php
$options = &#91;"http" => [
    "method" => "POST",
    "header" => ["Authorization: token " . $access_token,
        "Content-Type: application/json"],
    "content" => $data
    ]];
$context = stream_context_create($options);

The $access_token had been set elsewhere (in fact I usually put credentials in a separate file and exclude it from source control in an effort not to spread my access credentials further than I mean to!), and $data is already encoded as JSON. For completeness, you can make the POST request like this:



What Goes in Source Control?

Short answer: everything! However we need some good directory structures and source control configuration to make that a really practical answer, so this article is a quick outline of my usual advice for a good source control structure for a standard web project. The examples are for a PHP project but I’m sure you could apply this to your own language of choice, also. Continue reading