Pragmatism in the real world

Increase truncated body in a Guzzle exception

When Guzzle throws BadResponseException, the message includes information about the method, URL response code and then a truncated part of the body. For example: "Client error: `GET https://dev.clientproject.com:4444/oauth2/authorize?client_id=983e98d2fab8756a&scope=scope&response_type=code&redirect_uri=%2Fhome&code_challenge=some_code_challenge_here` resulted in a `400 Bad Request` response: {"error":"invalid_request","error_description":"The request is missing a required parameter, includes an invalid parame (truncated…) To retrieve the full text of the body, you grab it from the response property of the exception: $body = $e->getResponse()->getBody()->getContents(); Changing the truncation limit If you want… continue reading.

Fixing PhpStorm cannot find a local copy of Standard Input Code

Recently, I set up my PHP dev environment to allow me to step debug from unit tests that I run with make unit The relevant parts of Makefile look like this: # Set DEBUG=1 to enable Xdebug ifeq ($(origin DEBUG),undefined) XDEBUG := else XDEBUG := XDEBUG_SESSION=PHPSTORM endif unit: ## Run unit tests docker compose exec php bash -c "$(XDEBUG) vendor/bin/phpunit –testsuite=unit" I can now set a break point and run the unit tests with Xdebug… continue reading.

Installing the SQL Server PHP extension in Docker on Mac

I'm working on a project that uses MS SQL Server as its database. Recently, I noticed that the SQL Server Docker container now works with Apple Silicon Macs, so looked into setting up a PHP-FPM container with the sqlsrv extension installed. I'm noting the relevant parts of my Dockerfile for when I need them again, so this is entirely an aide-mémoire! FROM ubuntu:22.04 # Install PHP as per https://akrabat.com/7126 ## Register the Microsoft package repository… continue reading.

Prevent the Docker container from taking 10 seconds to stop

For one project that I'm working on the PHP-FPM-based Docker container is built from a Ubuntu container with PHP is installed into it. A little like this: FROM ubuntu:22.04 RUN apt-get update && apt-get upgrade -y && apt-get install -y gnupg curl # Register the Ondrej package repo for PHP RUN mkdir -p /etc/apt/keyrings \ curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg –dearmor | tee /etc/apt/keyrings/ppa_ondrej_php.gpg > /dev/null \ && echo "deb [signed-by=/etc/apt/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" >… continue reading.

Using Monolog's TestHandler

When I write integration tests with PHPUnit, I find it helpful use Monolog's TestHandler to check that the logs I expect are generated. It's reasonably common that classes that write to a log take a PSR\Log\LoggerInterface in their constructor and then use that for logging. This usually logs to the error handler for pushing to Sentry or whatnot. This makes testing easy! For testing we can use the TestHandler which stores the logs it receives… continue reading.

Check licenses of composer dependencies

With some commercial projects, it can be useful to know that all your dependencies have licences that your organisation deems acceptable. I had this requirement for a few clients now and came up with this script that we ran as part of our CI which would then fail if a dependency used a license that wasn't allowed. This proved to be reasonably easy as composer licenses will provide a list of all packages with their… continue reading.

Creating JWKS.json file in PHP

In order to verify a JWT created with an asymmetric key, the verifier needs to get the correct public key. One way to do is described in RFC7517 which describes the JSON Web Key format. Within the header of the JWT there is a kid property which is the key ID which is then used to find the correct key within a list provided at the /.well-known/jwks.json endpoint. The JWT header therefore looks something like… continue reading.

A quick guide to JWTs in PHP

The most common use of JWTs is as an authentication token, usually within an OAuth2 workflow. Creating these tokens is part and parcel of the authentication library that you use. I recently had a requirement to use a JWT independent of authentication and these are some notes on what I learned when researching with Lcobucci\JWT. Make up of a JWT To really understand JWTs, read RFC7519. For a more readable introduction, read the one on… continue reading.

PHPUK 2023

I have just returned from the 2023 edition of PHPUK and, as always, found it a valuable conference to catch up with the PHP community and find out what’s happening in the ecosystem. This year, I was accepted to speak on the differences between RPC, REST and GraphQL APIs and was surprised and gratified that the room was at full capacity. Thank you to everyone that attended; I hope that you learnt something useful. I… continue reading.

Installing Xdebug on PHP 8.1 installed with Homebrew

I have recently set up a new M2 MacBook Air and as usual, installed Homebrew and then installed PHP. Homebrew is always up to date, so it installed PHP 8.1 for me. Again, as usual, I installed Xdebug using pecl install xdebug. This whirrs and clicks for a while downloading and compiling the xdebug.so and then fails with: rob@ardent ~ $ pecl install xdebug downloading xdebug-3.1.5.tgz … Starting to download xdebug-3.1.5.tgz (232,070 bytes) … Build… continue reading.