Use Database Connection Strings with Laravel 8

I’ve been doing a lot of database stuff lately, and not much PHP, so when I returned to make my first Laravel project for a while, I had to check the docs to remind myself how some of this works. I noticed that the default approach to database credentials is still to use separate credentials for the host, port, and other variables. I’m using Aiven databases (because I work there and managed databases are great for demo apps as well as real ones!) which supply connection strings, but Laravel supports these too. Continue reading

The Laravel Synchronous Queue

Using queues for asynchronous processing is one of my favourite tricks for offloading hard work from web servers. When working with Laravel recently I was pleased to find that it supports beanstalkd out of the box. I’ve got opinions about frameworks with Opinions but I did find one thing I really liked in the way Laravel uses queues: the sync queue option that runs your queue synchronously on your development platform so you can develop and debug your work, then switch the queue platform you use later. Continue reading

Generating PHP library code from OpenAPI

I’m working with a bunch of OpenAPI specs now I am working for Nexmo and so far I’m loving working with API descriptions. OpenAPI is the next iteration of what used to be called “Swagger”, a machine-readable way to describe your API and from that description create docs, code libraries, tests, and all sorts. Today’s post is a quick writeup of how I generated some PHP code so that I can refer back to this next time I want to do it! Continue reading

Managing Environment Variables in PHP

Now I work with more programming languages, I start to miss features from other languages when I come “home” to PHP. One that I hadn’t seen in PHP before I saw it in other languages such as NodeJS (I think Ruby had the original implementation) was: a way to easily control setting your environment variables, particularly in development. In NodeJS the dotenv library is great for this; handily in PHP vlucas has already created phpdotenv so we are all set to apply these tricks to PHP applications! Continue reading

Make a POST Request from PHP With Guzzle

I work extensively with APIs and a variety of serverside scripting languages, and best practice does change over time. Many of the most popular posts on this blog are 10 years old, because apparently I was interesting in 2008. Two in particular from around that time relate to making POST requests from PHP … and I’d do it completely differently today. So, in an attempt to overcome some of the past crimes of the Internet in general and PHP in particular: here’s how to make a POST request in PHP, today, in a PHP 7+ world (it probably works in PHP 5 too). Continue reading

Relative Links with IBM Cloud API Gateway

I work quite a bit with serverless tech, particularly on IBM Cloud since I work there. At the moment I’m building a microservice using web actions. When a user creates data with a POST request, I want to redirect them to the URL of the new thing they created – but for that I need to know the URL that it was accessed with. This is a relatively new feature on API Gateway so here’s a quick howto for grabbing that URL in both JavaScript and PHP. Continue reading

Handle Webhooks with Serverless PHP

Did you know you can serverless with PHP? OK, so serverless is clearly not a verb, but since I love serverless tech and have also loved PHP for longer than I’m going to admit (the dates of the earliest posts on this blog might serve as a clue), using these technologies together is definitely my idea of a good time.

I included an example of a serverless endpoint to receive incoming webhooks in my talk at PHPUK last week and I got a few questions about it so I thought I’d share that example in written form. Continue reading

Testing API calls in PHP with Guzzle Mocks

I’m working on a CouchDB library for PHP, and so I needed to write some tests for it. CouchDB has an HTTP API so I’m basically making web requests and while I could certainly set up a test database and run full-on integration tests, there are a few limitations with that approach. Firstly: it means I’m testing the database as well, which isn’t what I want and brings extra dependencies that make the tests harder to run. Also: I want to be able to test error cases, rate limiting and so on, that would be difficult to recreate reliably. Continue reading

Connecting PHP to MySQL on Bluemix

Most of the PHP I write runs on Bluemix – it’s IBM self-service cloud, and since I work there, they pay for my accounts :) There are a bunch of databases you can use there, mostly open source offerings, and of course with PHP I like to use MySQL. Someone asked me for my connection code since it’s a bit tricky to grab the credentials that you need, so here it is. Continue reading