Tag: php

Jenkins Swarm and Docker article

This article explains how to set up a Dockerfile that will allow you to set up a Jenkins environment with slaves that allow for unit tests on multiple environments, with different PHP versions. Using Jenkins’ Swarm plugin allows for automatic detection of new slaves. So even developers can temporarily ‘lend’ their laptops for build jobs. The advantage of using Docker is that this will not interfere with their current installation because it is running inside an image.

Every PHP developer recognizes this problem: How do we make sure that all software remains working after a PHP upgrade. With the PHP 7 release on the horizon we were looking for solutions to run our unit tests on both PHP 5.x and 7.

http://devblog.simgroep.nl/blog/2015/11/12/jenkins-swarm-docker/

Leave a Comment

A PHP frontend workflow

An interesting article which explains how to do perform a workflow in your PHP project for your frontend code, without using NodeJS. The article introduces BowerPHP, mini-asset and Robo as alternatives to the NodeJS modules.

If you’re intimidated, exhausted or irritated by Gulp, Grunt, NPM, Yeoman, and all the other NodeJS tools that aim to help but often do the opposite, a good asset management workflow is possible in PHP, and this tutorial aims to prove it. In this article, we’ll go through a NodeJS-free PHP front end asset management setup.

Look, Ma! No NodeJS! – a PHP front end workflow without Node

Leave a Comment

Set charset on PDO connection with mysql in PHP

When using PDO to connect to MySQL it is wise to explicitly set the character set to utf8 (of course, only when using utf8 is the charset). In the MySQL or MySQLi extension I would normally execute the query SET NAMES utf8 to set it.

In PDO the charset can be specified in the connection string:

$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);

The charset option is only used since PHP 5.3.6, so take this into account when running an older version of PHP. In that case you should run the following statement after constructing the PDO object:

$conn->exec('SET NAMES utf8');

But you should’nt be running such an old version of PHP anyway.

Leave a Comment

Google Data redirectUri required

In a piece of code where authentication/authorization for Youtube was done, I only setting the redirect_uri in the piece of code where the logging in was done, using createAuthUrl(). This value is the location to redirect to when the authorization is completed and the user is redirected back to the invoking website.

In subsequent requests, when attempting to upload a video, the Client was throwing errors. It was not immediately clear what was wrong, but it turns out that even for non-authentication requests the setRedirectUri() function must be specified for the API to work.

Leave a Comment

PHP Curl SSL on Windows

When developing on Windows I regularly found myself using the line below to circument SSL errors on Windows when using Curl in PHP:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

If I do not set these curl options, the request would fail with the message: Details: SSL3_GET_SERVER_CERTIFICATE:certificate verify failed. This is a
warning you normally only expect when connecting to a host with a self-signed certificate or something else. But on Windows this also happens for correct
certificates, because the certificate chain cannot be established. This can be solved in two steps:

  1. Download file with root certificates from http://curl.haxx.se/docs/caextract.html
  2. Add the lines below to your php.ini file (where the path is where you downloaded the file in step 1.

    [PHP_CURL]
    curl.cainfo=c:\apps\php\cacert.pem

This post is based on a solution in a stackoverflow post.

Leave a Comment

Setting a PHP configuration value via htaccess

An Apache .htaccess file can be used to set specific configuration values for PHP in a website. This is a useful feature when looking for a temporary PHP setting specific to that website; for example when doing an upgrade.

To set a value use: _php_value _. Setting a boolean value is done using php_flag Off|On. An unlimited execution time can be set for example by placing php_value_time_limit 0 in the .htaccess file.

Overriding a PHP setting via .htaccess is not always allowed in the site configuration. To allow this the Options option must be added to the AllowOverride directive in the Virtualhost configuration of the site.

Leave a Comment

PHP Whitespace check

Did you ever find yourself with unwanted whitespace at the start or end of your document? A tip to prevent this beforehand is to never close the PHP file with a _?> _tag. These tags are not required. It is a common practice on several projects.

If this is too late and you do have whitespace, execute the snippet below in the root folder to find the culprit.

<?php
foreach (glob('\*\*/\*.php') as $file){
	if (preg_match('/\?'.'>\s\s+\Z/m',file_get_contents($file)))
		echo $file . PHP_EOL;		if( preg_match('/^[\s\t]+<?php/', file_get_contents($file)) )
		echo $file . PHP_EOL;
}

This code will match both the whitespace starting before the open and after the close tag.

Leave a Comment