Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
Building a SparkPost Client: TDD with PhpUnit and Mockery
May 04, 2016 @ 12:26:32

On the SitePoint PHP blog they've continued their series covering the SparkPost mail delivery service and integrating it in to your application. In this latest part of the series author Christopher Pitt starts looking at the SparkPost API and uses it as a chance to practice some TDD (Test Driven Development) skills.

In a previous post, we looked at SparkPost (as an alternative to Mandrill), and explored a bit of the official PHP client. The official client handles a decent amount of work, but I got to thinking about what it would take to build a new client.

The more I thought about it, the more it made sense. I could learn about the SparkPost API, and practice Test Driven Development at the same time. So, in this post we’ll look to do just that!

He uses a few different libraries to explore the API and its endpoints: Guzzle for the HTTP requests and the Mockery+PHPUnit combination for the testing. He includes the setup and configuration for the testing environment and some sample tests for making sure things are connected. He then integrates Mockery into the testing, using it to mock the Guzzle requests and still have the tests pass even without the actual connection. He then works through several other tests and finishes the post with a mention of building coverage results for the "Client" class.

tagged: sparkpost client tutorial series tdd testdriven development mockery phpunit guzzle api

Link: http://www.sitepoint.com/building-a-sparkpost-client-tdd-with-phpunit-and-mockery/

Niklas Keller:
Let's Encrypt with PHP
Mar 16, 2016 @ 13:05:32

The introduction of the LetsEncrypt free SSL certificate service has made making your site SSL only a much easier process than ever before. They've made it even easier by allowing automation around certificate setup and renewal. In this post to his site Niklas Keller shows you how to use a PHP implementation of a tool to setup/refresh your certificates automatically.

Let’s Encrypt, the free and automated CA, started late last year into their public beta. They offer a pretty comprehensive client to automate the process of getting SSL clients and installing them. It will soon be moved to a new home at EFF.

If you’re already using PHP and don’t need / want the automatic installation of your free certificates or don’t want to install Python, here’s a client for you. kelunik/acme-client is able to issue and renew certificates using your already running webserver (e.g. Nginx or Apache). It’s using the http-01 challenge to prove your domain ownership / control to Let’s Encrypt.

He shows how to use the client to set up a new LetsEncrypt account (if you don't already have one) and issue a certificate through a few command line calls. He then shows how to automate the process to check your certificate expiration and load the updated certificates. He also includes an example of the command to revoke a certificate, just in case.

tagged: letsencrypt ssl certificate client acme example refresh issue revoke

Link: http://blog.kelunik.com/2016/03/13/letsencrypt-php-client.html

Book Release: Integrating Web Services with OAuth and PHP
Feb 25, 2016 @ 14:08:17

php|architect has officially announced the release of their latest book: Integrating Web Services with OAuth and PHP from author and PHP community member Matt Frost.

Modern web applications are no longer standalone, monolithic codebases. Instead, they are expected to integrate with external, 3rd party applications to allow users to tap into new features, integrate with their social networks, and to easily migrate their data between systems. Many services afford these integrations by building web services that use the OAuth standard to authenticate users and allow “secure delegated access” on their behalf.

The book covers both of the major versions of OAuth currently in use (v1 and v2), how they differ and provides working PHP examples of both the client and server sides of the functionality. If you're interested you can "try before you buy" with an excerpt from the book to get a feel for the writing style and content. You can get more information and pick up a copy of your own directly from the php[architect] site.

tagged: server oauth webservice integrate release book client mattfrost

Link: https://www.phparch.com/books/integrating-web-services-with-oauth-and-php/

Tyler Longren:
Use Composer in Your WordPress Plugin or Theme
Nov 16, 2015 @ 10:22:47

In this post to his site Tyler Longren shows how to use Composer to install WordPress plugins as well as the usual libraries.

I love Composer. It just makes including libraries or scripts in your app incredibly easy. So easy that it’s stupid not to use it (in many, if not most cases).

[...] I'd never used Composer with a proprietary WordPress plugin before. The plugin is for a client so it’ll never be available to the public. Here’s the steps I took to make this WordPress plugin compatible with Composer so that I can easily bring in third-party libraries.

He breaks it down into a five step process, showing the installation of the Mailgun PHP client:

  • Install composer on your server
  • Add Mailgun as a dependency
  • Check your composer.json file
  • Tell composer to install Mailgun
  • Autoload Our Mailgun Classes in Our Plugin

With the help of Composer, the Mailgun client is ready to go and accessible in his WordPress instance, quickly and easily.

tagged: composer wordpress plugin mailgun client install autoload

Link: https://longren.io/use-composer-in-your-wordpress-plugin-or-theme/

SitePoint PHP Blog:
Suggesting Carbon with Composer – Date and Time the Right Way
Nov 16, 2015 @ 09:16:58

On the SitePoint PHP blog there's a tutorial that's been posted spotlightling a PHP library that can make working with dates and times simpler: Carbon . In this new tutorial they walk you through what the library has to offer and plenty of examples of it in use.

Carbon is a small library for date and time manipulation in PHP. It relies on and extends the core DateTime class, adding helpful methods for a significantly saner experience.

In this article, we’ll take a look at some basic usage examples, and then use it in a real project.

They start with the installation (via Composer) and a few examples of it in use, determining if a given date string is a weekend, in a leap year, etc. They also talk about localization support and working with time intervals. He then gets into the more real-world part of the example, updating the Diffbot client to optionally support Carbon for its date/time handling. He starts with some tests to define how he wants the handling to work and how to use it to parse the date returned from the DiffBot API.

tagged: carbon datetime library tutorial date time diffbot client api

Link: http://www.sitepoint.com/suggesting-carbon-with-composer-date-and-time-the-right-way/

SitePoint PHP Blog:
Breaking Free from Guzzle5 with PHP-HTTP and HTTPlug
Nov 09, 2015 @ 09:35:41

In a new tutorial from the SitePoint PHP blog editor Bruno Skvorc shows you how to "break free" from using the Guzzle HTTP client (which has become, by far, the most popular) and make it simpler to go with another option. He highlights the HTTPlug library that makes it easy to do just that.

In a previous series, we built a PHP client for Diffbot. The client works well and is in relatively widespread use – we even tested it on a live app to make sure it’s up to par – but it depends heavily on Guzzle 5.

There are two problems with this: Guzzle 6 is out, and supports PSR 7. [...] Someone implementing our client in their app might already have a preferred HTTP client in use, and would like to use theirs rather than Guzzle. [...] Coincidentally, there is a new project allowing us to do just that: HTTPlug.

He walks you through the installation of the library via Composer and covers what all kinds of functionality it contains. HTTPlug provides an "entry point" and unified interface for the HTTP client of your choosing, complete with interface packages to wrap the most common functionality. He shows how to refactor his Diffbot code to use the package, replacing the previous Guzzle dependency using the virtual package definition HTTPlug provides. He also updates some of the tests to use the HTTPlug package types rather than relying on Guzzle's object return types.

tagged: guzzle http client httplug library abstract tutorial

Link: http://www.sitepoint.com/breaking-free-from-guzzle5-with-php-http-and-httplug/

Cees-Jan Kiewiet:
ReactPHP: HTTP Client
Nov 05, 2015 @ 12:05:14

Cees-Jan Kiewiet has a post on his site focusing on the HTTP client side of the functionality offered by the ReactPHP. In this post he covers the basics of installation and usage with plenty of code examples (and screencasts of it in action).

Aside from a HTTP component ReactPHP also has a HTTP Client component that lets your send out HTTP requests. It is incredibly handy when you need to communicate with for example elasticsearch's REST API, AWS platform through their SDK or the RIPE Atlas API.

He walks you through the simple installation of the library (via Composer) and the code to send a simple request to an example.com domain, returning the HTML contents of the page. He then gets to some more complex examples: sending two requests at the same time, streaming the response body as it arrives and an example based on community feedback - streaming Twitter data. He ends the post with a community example showing the use of the Buzz HTTP client to make simple requests.

tagged: reactphp http client example stream twitter screencast

Link: http://blog.wyrihaximus.net/2015/11/reactphp-http-client/

SitePoint PHP Blog:
Powerful Custom Entities with the Diffbot PHP Client
Nov 02, 2015 @ 10:55:18

On the SitePoint PHP blog editor Bruno Skvorc continues his look at the Diffbot service and shows how to use their API to create entities representing objects from the API data.

A while back, we looked at Diffbot, the machine learning AI for processing web pages, as a means to extract SitePoint author portfolios. [...] Since then, the design of the pages we processed has changed, and thus the API no longer reliably works. In this tutorial, apart from rebuilding the API so that it works again, we’ll use the official Diffbot client to build custom entities that correspond to the data we seek (author portfolios).

He starts by setting up the environment for development (a Homestead Improved instance) and pulling in a few libraries to bootstrap the script. He shows the setup and configuration of the Diffbot client, creating a new API application via their UI and using the browser tools to help the service locate the information it needs. He then shows how to extend the client and define an "entity factory" to generate the objects requested. In this case he creates an AuthorFolio class to contain the author's number of posts.

tagged: diffbot client custom entities tutorial author portfolio api

Link: http://www.sitepoint.com/powerful-custom-entities-with-the-diffbot-php-client/

Davey Shafik:
Aug 24, 2015 @ 10:54:54

Davey Shafik has published a post about library he's created that's a sort of "recorder" for connections made with the Guzzle HTTP client - the Guzzle VCR.

A few days ago I pushed out a very small library to help with testing APIs using Guzzle: dshafik/guzzlehttp-vcr. [...] This is a simple middleware that records a request’s response the first time it’s made in a test, and then replays it in response to requests in subsequent runs.

The handler works by recording the responses from the API (ex: the JSON response data) and records them to files (again, JSON). A one-line call turns the "recording" on and points to a directory where the cached files should be stored. He shows how to use it in the constructor of your Guzzle client, setting it up as the "handler" for the requests. He also includes an example of a few unit tests that make use of the recording feature to check the response of a /test endpoint.

tagged: guzzle http client vcr recording response json cache handler

Link: http://daveyshafik.com/archives/69384-guzzlehttp-vcr.html

Gonzalo Ayuso:
PHP Dumper using Websockets
May 11, 2015 @ 08:49:21

Gonzalo Ayuso has a quick post to his site showing you how to make a "PHP dumper" for websocket connections based on a simple Silex application.

Another crazy idea. I want to dump my backend output in the browser’s console. There’re several PHP dumpers. For example Raul Fraile’s LadyBug. There’re also libraries to do exactly what I want to do, such as Chrome Logger. But I wanted to use Websockets and dump values in real time, without waiting to the end of backend script. Why? The answer is simple: Because I wanted to it.

He shows how to create a simple socket server (with Express in Javascript) and the basic Silex application with a "DumperServiceProvider" added in that will handle returning the debugging data back to the waiting client. He connects the Silex application with the websocket and shows the code to listen for new messages on the socket and display them back out to the browser. You can see an example of the end result in this video on YouTube.

tagged: websocket tutorial dumper debug output client expressjs nodejs

Link: http://gonzalo123.com/2015/05/11/php-dumper-using-websockets/