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

Using php-fpm as a simple built-in async queue
Aug 21, 2017 @ 09:25:20

On the Tideways blog Benjamin Eberlei has written up a post showing how to use php-fpm as a "poor man's queue" system, making it easier to hand off requests to be worked on out of band without having to install other software.

There are many tasks that a web-request should not perform directly so the user doesn't have to wait many seconds for a response. [...] The usual advice you find on the internet is to setup a queue such as RabbitMQ, Redis, Kafka, Gearman or Beanstalkd. But this means another service that you need to install, setup, maintain and monitor. With some of the queue systems operating them includes a steep learning phase that requires time and money for additional hardware.

But maybe you just need a poor mans version of an asynchronous queue without all the overhead? Then why not just use PHP-FPM itself?

He admits that it's more of an "experimental approach" but feels like it could be a viable option for the php-fpm users out there. He then shows how to use the hollodotme/fast-cgi-client library to execute an asynchronous request for a "SendEmail" command. The request is then passed off to another PHP-FPM worker for processing without the user having to wait on a result. He ends the post with a few words of warning about using this approach and some other methods for getting around the offloading of longer processing.

tagged: phpfpm asyncronous library tutorial offload processing socket

Link: https://tideways.io/profiler/blog/using-php-fpm-as-a-simple-built-in-async-queue

Lorna Mitchell:
Handling Incoming Webhooks in PHP
Jul 27, 2017 @ 12:27:14

Lorna Mitchell has a quick post to her site sharing a method she uses for handling incoming web hooks requests in PHP and the process her code usually uses for parsing the incoming message.

An increasing number of applications now offer webhooks as an integration, often in addition to an API. The classic example, familiar to most developers, is the GitHub webhooks which can notify your other systems such as CI tooling that a new commit has been added to a branch.

[...] Whether it's your source control, updates from your IoT sensors, or an event coming from another component in your application, I have some Opinions (TM) about handling webhooks, so I thought I'd write them down and include some code as well, since I think this is an area that many applications will need to work with.

She talks about the receive/respond workflow she recommends: immediately storing and acknowledge the data and then responding out of band (asynchronously). She includes a bit of example code that reads in the raw input from the incoming message, saves it and then responds back with a 200 response code back to the waiting service. She then talks about the out-of-band processing the message could use, evaluating the contents and acting on them as a result.

tagged: webhooks incoming processing asynchronous response tutorial

Link: https://lornajane.net/posts/2017/handling-incoming-webhooks-in-php

Delicious Brains:
Introducing WP Image Processing Queue - On-the-Fly Image Processing Done Right
Mar 09, 2017 @ 09:28:59

The Delicious Brains site has a new tutorial posted introducing WP Image Processing Queue, a tool that allows for on-the-fly image processing in your WordPress application via background processing.

I think the best solution is to get background processing into WordPress core so that all themes/plugins can share a single queue and ensure we don’t impact server performance. And so started my crusade.

At PressNomics, I had a great chat with Mike Schroder. He presented a very good path to core: find a feature that WordPress core needs and that needs background processing. In other words, piggyback! This is exactly how the image optimization stuff made it into core last year: by piggybacking off of responsive images. For background processing, he proposed coming up with an alternative to on-the-fly image processing (OTFIP). Whoa, turns out OTFIP is a problem we regularly deal with for WP Offload S3 as well. This could be a “two birds – one stone” kind of thing. Stars were aligning.

He talks more about some of the current discussions and efforts around processing the images like this (with OTFIP, On The Fly Image Processing). He covers some of the libraries that are currently out there for this processing and how, ultimately, the image processing queue came out to replace them as a result of some work at WordCamp US Contributor Day. He gives an example of the code needed to resize the images and the resulting markup. The post ends with the work he's planning on getting this queuing into the WordPress core and encourages plugin authors to use the OTFIP functionality rather than an external library.

tagged: wordpress image processing queue introduction onthefly

Link: https://deliciousbrains.com/introducing-wp-image-processing-queue/

Using the Mailgun Store(): A Temporary Mailbox for Your App's Incoming Email
Jun 06, 2016 @ 12:22:39

The TutsPlus.com site has posted a tutorial today showing you how to use the "Store" functionality in Mailgun from your PHP application to temporarily handle your incoming emails.

In today's episode, Mailgun stepped in to sponsor a tutorial about how I integrated its message routing and Store() API to handle replies from users.

For example, when people receive meeting requests from others with Meeting Planner, they may just choose to reply and send a note like they would to a typical email thread. [...] Sounds complicated, but one of Meeting Planner's goals is to reduce the back and forth emails between people about planning and consolidate real-time changes into fewer notifications.

The start by introducing the Mailgun service and, more specifically, the Store() offering it provides. He uses a Yii2 framework based application to show the integration. Once the MX (mail) records are set up correctly it can then hook back in to your mail servers or web application. The code is included to make the migration to hold the notification info, make the POST request back to the application and use background process to handle the mail processing.

tagged: mailgun tutorial store incoming processing temporary callback yii2 example

Link: http://code.tutsplus.com/tutorials/using-the-mailgun-store-a-temporary-mailbox-for-your-apps-incoming-email--cms-26479

SitePoint PHP Blog:
Drupal 8 Queue API – Powerful Manual and Cron Queueing
Dec 14, 2015 @ 11:54:38

On the SitePoint PHP blog Daniel Sipos has written up a tutorial spotlighting a powerful feature of Drupal 8, the latest major release of this popular project: the Queue API. Queueing in Drupal allows you to offload tasks to be handled outside of the current web request.

The Queue API in Drupal allows us to handle a number of tasks at a later stage. What this means is that we can place items into a queue which will run some time in the future and process each individual item at that point and at least once. Usually, this happens on CRON runs, and Drupal 8 allows for a quick set up for cronjob based queues. It doesn’t necessarily have to be CRON, however.

They use two examples to help illustrate how to use the queueing system: one that uses the cron-based approach and another that's more manually triggered. They start out with the theory behind it all, talking about the different pieces (objects/classes) and how they fit together to make the queueing system. With that out of the way the article starts in on the code and the "Node Publish" queue, defining its basic structure and hooking it in to the framework. It shows you how to create the cron worker to process the queue and how to build the manual worker to do the same but only when specifically called.

tagged: drupal8 queue manual cron process defer processing tutorial

Link: http://www.sitepoint.com/drupal-8-queue-api-powerful-manual-and-cron-queueing/

Grok Interactive:
Importing Large CSV Files with PHP Part 1: Import Using One Query
Sep 23, 2015 @ 12:19:33

The Grok Interactive blog has posted a tutorial, the first part in a series, showing you how to work with large CSV files in PHP.

Importing CSV files into your application can become a problem when the file is really big, > 65,000 rows big. Each row of the file needs to be parsed, converted into an object, and then saved to a database. All of this needs to happen within a 30 second timeout window. It may sound like an impossible task, but there are actually a couple of solutions that can solve this problem. While working on a project at Grok, I was tasked with doing exactly that.

He talks about the method he tried initially for parsing the large files, splitting it up into different files and processing them as chunks. He points out that it relies on the file system, though, and this made it difficult to debug. He finally came up with a different, more simple solution: importing the files directly into MySQL via a LOAD DATA LOCAL INFILE command. He shows how to set this up in a controller and "importer" class that handles the upload and import via the importFileContents method (complete code included). He walks through what the code is doing and includes a few notes about the configuration of the database connection to specify additional options on the PDO connection to allow the local file load.

tagged: tutorial csv file import large processing chunk mysql load file query

Link: http://www.grok-interactive.com/blog/import-large-csv-into-mysql-with-php-part-1/

SitePoint PHP Blog:
An Introduction into Event Loops in PHP
Sep 10, 2015 @ 10:06:55

The SitePoint PHP blog has posted a tutorial from Christopher Pitt introducing you to using event loops in PHP, a feature that allows for asynchronous processing, executing code while waiting for other code to finish.

PHP developers are always waiting for something. Sometimes we’re waiting for requests to remote services. Sometimes we’re waiting for databases to return rows from a complex query. Wouldn’t it be great if we could do other things during all that waiting?

If you’ve written some JS, you’re probably familiar with callbacks and DOM events. And though we have callbacks in PHP, they don’t work in quite the same way. That’s thanks to a feature called the event loop.

He starts by explaining event loops with an example from a language that naturally supports it - Javascript. He includes another example using the setTimeout function in Javascript to show a simple loop but points out that PHP just doesn't support this same kind of handling (code included). So, how can you simulate the loop like in JS? He links to and includes a examples of two libraries that could be dropped in and used to do the hard work behind the scenes of the looping:

He suggests that PHP developer "get out of the single threaded mindset" and work in ways to handle asynchronous processing into their code to improve performance and flexibility.

tagged: event loop javascript library icicle reactphp asynchronous processing

Link: http://www.sitepoint.com/an-introduction-into-event-loops-in-php/

Laravel News:
Easy Image Processing in Laravel with Glide
Jan 30, 2015 @ 10:06:41

On the Laravel News site there's a new post sharing a video tutorial of how to use the Glide image handling library with your Laravel-based application.

Glide is a new package by Jonathan Reinink which is an on-demand image manipulation library. In this video he shows you how to set it up and the basic usage in Laravel 5.

In the tutorial Jonathan walks you through an introduction to the library and how it wraps around the Intervention image handling. He creates a basic application that, when an image endpoint is called, output the image with any given configuration options (like height and width). Glide is one of many packages making up The PHP League.

tagged: laravel image processing glide library thephpleague screencast video

Link: https://laravel-news.com/2015/01/using-glide-laravel/

SitePoint PHP Blog:
Building and Processing Forms in Symfony 2
Jun 06, 2014 @ 13:45:07

The SitePoint PHP blog has a new tutorial posted from author Daniel Sipos about form handling in Symfony2. More specifically, about creating them and handling the results from their submission. This is an introduction to the topic and gets into two examples, one focusing on a view implementation and the other using the form builder.

In this tutorial we will look at two examples of using forms in Symfony 2. In the the first, we will place form elements straight in the View file and then handle the form processing manually in the controller. In the second, we’ll use the Symfony form system to declare forms in an object oriented way and have Symfony process and persist the values. We will be working on a simple installation of the Symfony framework.As you may know, it comes with a default bundle called AcmeDemoBundle and we will use that to illustrate working with forms.

In the first example he looks at "non-entity forms" and shows how to create the form from normal HTML elements in the view. The form is just a simple input field and a submit button. He includes the code you'll need to process the form submission too. In the second example he includes an example of how to create the same setup but using the Form Builder instead. It's also links it to a data object, making it simpler to save the submission results.

tagged: symfony2 form processing view builder entity manager tutorial

Link: http://www.sitepoint.com/building-processing-forms-in-symfony-2

Mikko Koppanen:
Working on images asynchronously
Dec 16, 2013 @ 10:45:35

Mikko Koppanen has a new post to his site today about working with images asynchronously - the "offline" processing of things like user uploaded images using a queuing system.

To get my quota on buzzwords for the day we are going to look at using ZeroMQ and Imagick to create a simple asynchronous image processing system. Why asynchronous? First of all, separating the image handling from a interactive PHP scripts allows us to scale the image processing separately from the web heads. [...] Secondly, separating the image processing from a web script can provide more responsive experience to the user. This doesn’t necessarily mean faster, but let’s say in a multiple image upload scenario this method allows the user to do something else on the site while we process the images in the background.

He also includes a "barebones" example of how the system would work. The first Worker script makes the connection to the queue system and sends the data off for handling. The second script does most of the actual work, pulling in the image and using Imagick to create a thumbnail image. Finally he includes an example of the use of the workers in combination to send the image data for processing.

tagged: image asynchronous processing zeromq thumbnail imagick

Link: http://valokuva.org/working-on-images-asynchronously/