Pragmatism in the real world

Inserting binary data into SQL Server with ZF1 & PHP 7

If you want to insert binary data into SQL Server in Zend Framework 1 then you probably used the trick of setting an array as the parameter's value with the info required by the sqlsrv driver as noted in Some notes on SQL Server blobs with sqlsrv. Essentially you do this; $data['filename'] = 'test.gif'; $data["file_contents"] = array( $binaryData, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max') ); $db->insert($data); Where $db is an instance of Zend_Db_Adapter_Sqlsrv. If you use SQL Server… continue reading.

Autocomplete Composer script names on the command line

As I add more and more of my own script targets to my composer.json files, I find that it would be helpful to have tab autocomplete in bash. I asked on Twitter and didn't get an immediate solution and as I had already done something similar for Phing, I rolled up my sleeves and wrote my own. Start by creating a new bash completion file called composer in the bash_completion.d directory. This file needs executable… continue reading.

Using CircleCI for a PHP project

For a new client project, I've decided to use CircleCI to run my tests every time I push to GitHub. This turned out to be quite easy; this is how I did it. I started by creating a config file, .circleci/config.yml containing the following: version: 2 jobs: build: working_directory: /var/www/html docker: – image: php:7.1-apache environment: APP_ENV: test steps: – run: name: Install system packages command: apt-get update && apt-get -y install git – run: name:… continue reading.

Stand-alone usage of Zend-InputFilter

Any data that you receive needs to be checked and validated. There are number of ways to do this including PHP's filter_var, but I prefer Zend-InputFilter. This is how to use it as a stand-alone component. Installation Firstly, we install it using Composer: $ composer require zendframework/zend-inputfilter $ composer require zendframework/zend-servicemanager You don't have to have ServiceManager, but it makes working with InputFilter much easier, so it's worth installing. Create the InputFilter The easiest way… continue reading.

Rendering problem details in Slim

As I've already noted, in the project I'm currently building, I'm rendering errors in my Slim Framework API using RFC 7807: Problem Details for HTTP APIs via Larry Garfield's ApiProblem component and rka-content-type-renderer. One place where we need to integrate this approach into Slim is in the error handlers. Let's look at NotFound. To ensure that we return a response in the right format, we need to implement our own NotFound handler: src/App/Handler/NotFound.php: <?php namespace… continue reading.

Rendering ApiProblem with PSR-7

In the API I'm currently building, I'm rendering errors using RFC 7807: Problem Details for HTTP APIs. As this is a Slim Framework project, it uses PSR-7, so I updated rka-content-type-renderer to support problem. RFC 7807 defines a standard for sending details of an error in an HTTP response message. It supports both XML and JSON formats. From the RFC, an example response is: HTTP/1.1 403 Forbidden Content-Type: application/problem+json Content-Language: en { "type": "https://example.com/probs/out-of-credit", "title":… continue reading.

A note on framework performance

A question came up recently wondering why Slim Framework was around 5 times slower than plain PHP. All frameworks are by definition slower than no-code as there's more going on. i.e. an index.php of: <?php header('Content-Type: application/json'); echo json_encode(['result' => 1]); is going to be much faster than a Slim application with this code: use \Slim\App; include 'vendor/autoload.php'; $config = include 'Config/Container.php'; $app = new App($config); $app->get('/do-it', function($request, $response){ return $response->withJson(['result' => 1]); }); $app->run();… continue reading.

Homestead per-project crib sheet

I wanted a drop-dead simple way to try and replicate a problem someone was having on the Slim forums. I couldn't reproduce with php -S which is my go-to for this sort of thing, so I thought I'd try Homestead. I had recently listend to a Voices of the Elephpant episode with Taylor Otwell & Joe Ferguson where Joe mentioned that Homestead worked on a per-project basis too. I didn't know this, so tried it… continue reading.

Using CharlesProxy's root SSL with home-brew curl

Once I installed Homebrew's curl for HTTP/2 usage, I discovered that I couldn't automatically proxy SSL through Charles Proxy any more. $ export HTTPS_PROXY=https://localhost:8888 $ curl https://api.joind.in/v2.1/ curl: (60) SSL certificate problem: self signed certificate in certificate chain More details here: https://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn't adequate, you can specify an alternate file using the… continue reading.

Using HTTP/2 with PHP 7 on Mac

if you want to use HTTP/2 with PHP on OS X, you need a version of curl compiled with HTTP/2 support. You can then link your PHP's curl extension to this version. The easiest way to do this is to use Homebrew: $ brew install openssl $ brew install curl –with-nghttp2 $ brew install php70 –with-homebrew-curl At the time of writing, this will install PHP 7.0.10 with Curl 7.50.1: $ php -i | grep cURL… continue reading.