Rob AllenUsing HTTP/2 with PHP 7 on Mac (31.8.2016, 10:36 UTC)

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
cURL support => enabled
cURL Information => 7.50.1

Using Curl on the command line

If you want to use your shiny new curl from the command line, then the easiest way to do this is:

brew link curl --force

You can now do:

$ curl -V

and you should get:

curl 7.50.1 (x86_64-apple-darwin15.6.0) libcurl/7.50.1 OpenSSL/1.0.2h zlib/1.2.5 nghttp2/1.14.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

(at the time of writing!)

Using Guzzle to test HTTP/2 is working

To prove it works in PHP, use Guzzle!:

$ composer require guzzlehttp/guzzle

testhttp2.php:

<?php
require_once 'vendor/autoload.php';

$client = new GuzzleHttp \Client();
$response = $client->get('https://www.google.it', [
    'version' => 2.0,
    'debug' => true,
]);

Run this PHP code at the command line:

$ php testhttp2.php

As we've turned on debugging, the output looks like this:

* Rebuilt URL to: https://www.google.it/
*   Trying 216.58.201.3...
* Connected to www.google.it (216.58.201.3) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /usr/local/etc/openssl/cert.pem
  CApath: none
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
*  start date: Aug 17 18:56:43 2016 GMT
*  expire date: Nov  9 18:29:00 2016 GMT
*  subjectAltName: host "www.google.it" matched cert's "*.google.it"
*  issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* TCP_NODELAY set
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fa804802000)
> GET / HTTP/1.1
Host: www.google.it
User-Agent: GuzzleHttp/6.2.1 curl/7.50.1 PHP/7.0.10

* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< date: Sun, 28 Aug 2016 10:36:13 GMT
< expires: -1
< cache-control: private, max-age=0
< content-type: text/html; charset=ISO-8859-1
< p3p: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
< server: gws
< x-xss-protection: 1; mode=block
< x-frame-options: SAMEORIGIN
< set-cookie: NID=85=UrPdiR04M5ZzIUtj4sZMPRZbY8vZEKVvazZy59PpoO5NG29lVX7JNuaeiajLhDKHiEBF2tWev3yj0xFF7fvYChmARVsFSnfiBX06ewWnMaPeXy0KkneQJ7dmHGS7JYaz; expires=Mon, 27-Feb-2017 10:36:13 GMT; path=/; domain=.google.it; HttpOnly
< alternate-protocol: 443:quic
< alt-svc: quic=":443"; ma=2592000; v="35,34,33,32,31,30"
< accept-ranges: none
< vary: Accept-Encoding
<
* Connection #0 to host www.google.it left intact

The key things to notice:

* ALPN, offering h2
* ALPN, offering http/1.1

We tell the server that we want HTTP/2 (h2), but can accept HTTP/1.1. If the server doesn't support HTTP/2 it will send back HTTP/1.1

Using HTTP2, server supports multi-use

This is a good sign!

The response's status line is:

HTTP/2 200

The version number is 2 and we got a 200, so all is OK!

Everything is working as intended.

Link
PHP ClassesGenerating SQL to Execute the Installation of a Database schema from JSON definition (31.8.2016, 10:01 UTC)
By Samuel Adeshina
As explained in the first part of this article, it was presented the Scripd library as a means to generate SQL to execute actions to install database schemata.

Read this article to learn how to define common database structures in JSON format to install the schema using DDL SQL generated by Scripd library.
Link
Voices of the ElePHPantInterview with Colin O’Dell (30.8.2016, 09:00 UTC)

Video

Audio only

@colinodell

Show Notes

The post Interview with Colin O’Dell appeared first on Voices of the ElePHPant.

Link
PHP ClassesNotable PHP package: PHP SSH Connection Session (30.8.2016, 08:41 UTC)
By Manuel Lemos
SSH is a protocol to communicate with a remote server via a secure connection that allows to execute commands on a remote machine.

However, the length of the commands that can be executed is limited.

This package provides a workaround for this limitation that consists in creating a shell script to execute long commands and execute it using the bash shell.

Read this article to learn more details about how this notable PHP package works.
Link
PHP Classes5 Phases to Create Software Products that Will Be Perfect for Your Customers (29.8.2016, 08:35 UTC)
By Manuel Lemos
Many software products failed because their developers did not start by listening to their customers problems.

Learning about the customers problems is the first of 5 phases of a method called Design Thinking. When well applied this method can lead to the creation of products that are perfect for their customers.

Watch this video to learn how you can apply this method to design your software products in a short period of time and get what customers really need even before you start writing a line of code.
Link
SitePoint PHPSourcehunt 2016.8 – Contribute to Regression, Regex, ORMs, and More (28.8.2016, 16:00 UTC)

Sourcehunt August 2016 Vacations are over, and it’s time to hunt some open source contributions again! austintoddj/Canvas [561 ★] Canvas is a new mini-CMS aimed at developers who need personal blogs. It aims to be both pretty, and pretty usable. I’ve tried it out in my hunt for a personal blogging engine, and while it […]

Continue reading %Sourcehunt 2016.8 – Contribute to Regression, Regex, ORMs, and More%

Link
Sarfraz AhmedFrom Bad to Good: DI, DIC and Services (28.8.2016, 09:37 UTC)

In this post, I intend to touch the concepts of:

Even though these terms seems scary at first especially to new-comers but are really easy to understand and help write quality code that is easy to understand and extend. We will take a ^legacy code^ and convert it into object-oriented one by using these principles.

Suppose we have simple application that sends newsletter email to all its subscribers, here is the legacy code:

// file: index.php or could also be considered as Controller file in MVC paradigm

require __DIR__ . 'vendor/autoload.php';

use Demo\Mailer\SmtpMailer;

$dsn = 'sqlite:' . __DIR__ . 'database.sqlite';
$pdo = new PDO($dsn);

$mailer = new SmtpMailer('smtp.example.com', 'user', 'password', '465');

$sql = 'SELECT * from subscribers';

foreach ($pdo->query($sql) as $row) {
    $mailer->sendMessage(
        $row['email'],
        'This is the email body',
        $row['name'],
        'info@example.com'
    );
}

Right now above code smells and has these problems:

  • Logic is mixed up, SQL queries, sending emails database configuration is all in one file

To fix this, first instinct is to convert it into class:

class SubscriberMailer
{
    public function sendEmails()
    {
        $dsn = 'sqlite:' . __DIR__ . 'database.sqlite';
        $pdo = new PDO($dsn);

        $mailer = new SmtpMailer('smtp.example.com', 'user', 'password', '465');

        $sql = 'SELECT * from subscribers';

        foreach ($pdo->query($sql) as $row) {
            $mailer->sendMessage(
                $row['email'],
                'This is the email body',
                $row['name'],
                'info@example.com'
            );
        }
    }
}

This looks good to eye but it has more problems:

  • Logic is still mixed up, SQL queries, sending emails database configuration is all in one file
  • It has hard-coded dependencies (tight-coupling) on PDO and Mailer class, what if we wanted to change Mailer class ?
  • It is hard to unit-test because of tight coupling

Let's try to fix these problems. We can see that this class has dependency on PDO so instead of hard-coding it inside SubscriberMailer class, we can ^pass^ it via its constructor which now makes the class:

class SubscriberMailer
{
    protected $pdo = null;

    public function __construct($pdo)
    {
        $this->pdo = $pdo;
    }

    public function sendEmails()
    {
        $mailer = new SmtpMailer('smtp.example.com', 'user', 'password', '465');

        $sql = 'SELECT * from subscribers';

        foreach ($this->pdo->query($sql) as $row) {
            $mailer->sendMessage(
                $row['email'],
                'This is the email body',
                $row['name'],
                'info@example.com'
            );
        }
    }
}

This is a bit better, the class doesn't care where PDO objects comes from, it just needs one to operate and you can't instantiate the class without providing one so it makes its intention clear. So this is what is called Dependency Injection, no need to fear even though the term sounded scary initially (read my previous article on more ways of injecting dependencies). Here is how we use above class at this point of the time:

require __DIR__ . 'vendor/autoload.php';

use Demo\Mailer\SmtpMailer;
use Demo\Mailer\SubscriberMailer;

$dsn = 'sqlite:' . __DIR__ . 'database.sqlite';
$pdo = new PDO($dsn);

$subscriberMailer = new SubscriberMailer($pdo);
$subscriberMailer->sendEmails();

This is starting to look better compared to first snippet of code. However, we still have a problem in that we have hard-coded smtp config into our class, what if we wanted to change the smtp config without touching the class ? Or what if we had different setup of smtp config for development and production ? Remember a class should be a black-box which simply does its job without editing it again and again, you can only extend it not edit it. So this reasoning hints us that like pdo, we should also pass smtp config via class's constructor and move it from the class itself.

class SubscriberMailer
{
    protected $pdo = null;
    protected $smtpConfig = [];

    public function __construct($pdo, array $smtpConfig)
    {
        $this->pdo = $pdo;
        $this->smtpConfig = $smtpConfig;
    }

    public function sendEmails()
    {
        $mailer = new SmtpMailer(
            $this->smtpConfig['host'],
            $this->smtpConfig['user'],
            $this

Truncated by Planet PHP, read more at the original (another 11061 bytes)

Link
SitePoint PHPLocal Composer for Everyone! A Conference-Friendly Satis Setup (27.8.2016, 16:00 UTC)

While preparing my technical materials for WebSummerCamp, I realized my workshop would rely on a fairly stable internet connection, as we'd have a lot of ground to cover and a lot of packages to install. Rather than rely on the gods of live demos, or pre-installing everything and ruining the experience, I picked another route.

In this post, I'll show you how to set up a local Satis instance and have it host the packages over the network it's currently on, so that everyone who's also connected to it can put the address into composer.json as a custom repository source, and retrieve all packages from your machine locally - no internet connection required!

Composer logo with deal with it sunglasses

Prerequisites

Due to a habit of never installing programming-related software on my main OS (more about that here), I'm using Homestead Improved, as usual. If you have a working PHP installation on your host machine, or prefer Docker or something similar, please feel free to use that. Note, however, that for the sake of cross-platform friendliness and simplicity, this tutorial will be Homestead Improved-specific.

Before booting up the VM, make sure you share an arbitrary port. This can be done by editing the bottom section of Homestead.yaml. I picked 6789, so that my local "packagist" will be hosted on IP:6789 where IP will be the IP address of my host machine.

Satis

Inside the VM, in an arbitrary location of your choice (I picked /home/vagrant/Code/) install a new Satis project with:

composer create-project composer/satis --stability=dev --keep-vcs

This will create the subfolder satis.

Continue reading %Local Composer for Everyone! A Conference-Friendly Satis Setup%

Link
SitePoint PHPHow to Create a Pokemon Spawn Locations Recorder with CouchDB (26.8.2016, 16:00 UTC)

In a previous article, you’ve been introduced to CouchDB. This time, you’re going to create a full-fledged app where you can apply the things you learned. You’re also going to learn how to secure your database at the end of the tutorial. Overview of the Project You’re going to build a Pokemon spawn locations recorder. […]

Continue reading %How to Create a Pokemon Spawn Locations Recorder with CouchDB%

Link
Voices of the ElePHPantInterview with Brian Retterer (26.8.2016, 09:00 UTC) Link
LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP