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

Sergey Zhuk:
Building ReactPHP Memached Client: Making Requests And Handling Responses
Oct 26, 2017 @ 11:37:03

Sergey Zhuk has kicked off a series of posts to his site showing how to create a ReactPHP memcache client that can work as a streaming client for your PHP application rather than single get/set requests.

Before writing any code we should think about our future client’s API: how we are going to use it [and] what methods it is going to have.

The client is going to be used in ReactPHP asynchronous ecosystem, so I’m going to provide a promise-based interface for it (when methods return promises). Also, we are building a streaming client. Under the hood, we will open a socket connection and use it as a stream. The client itself will be a wrapper on this binary stream communication. That means that it is our job to manually parse Memcached protocol to write and read data with sockets. So, having all of this in mind, let’s start.

He then starts in on the development of the base for the client including the factor class that will create the client (connector) with the provided Loop instance. He includes an example of this in use to create the client and point it to a local memcache server. Next he creates the client class that will use the stream to send requests and a parser to work with the responses and resolve actions that need to be taken based on their contents.

tagged: reactphp tutorial memcache client stream loop request response

Link: http://seregazhuk.github.io/2017/10/09/memcached-reactphp-p1/

SitePoint PHP Blog:
Building a Social Network with Laravel and Stream? Easy!
Apr 19, 2017 @ 13:53:03

Christopher Vundi has continued his series covering the integration of Laravel and the Stream service in this new tutorial. In the first post he showed how to add "follow" handling to the application, complete with a real-time stream event when it happens. In this new post he uses some of the same handling to enhance this to a larger "social network" type application.

In the previous post, we saw how to add the follow functionality to a Laravel app. We also looked at how to configure our app to use Stream. This part will focus on: configuring our models in order to make it possible to track activities, the different types of feeds that Stream provides, getting feeds from Stream [and] rendering the different types of feeds in a view.

He starts in with the "activity field" functionality, a base level object that stores each event that happens in the system and is then relayed to Stream. Then, using the included "feed manager" in the Stream package, he shows how to use built-in feeds and add in a custom feed for follow and unfollow events. The tutorial then walks through the output process of the events, handling of the updates from Stream and routing those back out to the waiting news feed on the frontend.

tagged: social network follow event stream streamio service tutorial series part2

Link: https://www.sitepoint.com/building-social-network-laravel-stream-easy/

Weebly Engineering Blog:
Streamed File Zipping and Downloading in PHP
Feb 06, 2017 @ 11:24:01

The Weebly Engineering blog has a new tutorial posted about streamed file zipping and downloading in a PHP based application. This allows the download to start even while the file is being compressed into a zip archive on the backend.

Did you notice that on Dropbox.com, you can select a folder and start downloading it while it’s being zipped? This on-the-fly zipping feature comes handy for both the user and the server?—?as a user you don’t have to wait until the files are zipped on the back end before the downloading starts, and it saves the server from creating a temporary zip file and deleting it afterwards.

[...] To implement this feature with PHP, we handle the zipping and streaming work on the back end using ZipStream. Assuming your files are on the server and can be loaded with file_get_contents given a path.

They include some of the details of the HTTP request format for the stream (as an attachment type) and how you can use the ZipStream package to quickly implement it into your code. They include a simple example of implementing it in a form, allowing the user to easily integrate it and work around some of the browser based security controls.

tagged: stream zip archive download tutorial zipstream tutorial

Link: https://medium.com/weebly-engineering/streamed-file-zipping-and-downloading-in-php-1fb59b0a5b79#.jop61kzc6

Tideways Blog:
Using HTTP client timeouts in PHP
Jan 06, 2017 @ 11:57:10

The Tideways blog has a post sharing things you can do in PHP to work with HTTP client timeouts in things that use the PHP sockets and streams.

Timeouts are a rarely discussed topic and neglected in many applications, even though they can have a huge effect on your site during times of high load and when dependent services are slow. For microservice architectures timeouts are important to avoid cascading failures when a service is down.

The default socket timeout in PHP is 60 seconds. HTTP requests performed with for example file_get_contents, fopen, SOAPClient or DOMDocument::load are using this timeout INI setting to decide how long to wait for a response.

He talks some about how these timeouts can effect your script and some of the common reactions (in code) to them happening. He then shows how to configure these timeouts to match the needs of you application in a few ways:

  • globally in the ini configuration
  • on a per-call basis in a stream_context_create call
  • changing the load timeout for DOMDocument::load
  • updating the setting for calls with SOAPClient
  • changing the timeout on cURL extension calls

Each item on the list comes with the code/settings needed to make the change.

tagged: timeout socket stream domdocument soapclient curl tutorial

Link: https://tideways.io/profiler/blog/using-http-client-timeouts-in-php

Master Zend Framework:
The 3-Step Guide to Downloading Files in Zend Expressive
Aug 29, 2016 @ 10:37:44

On the Master Zend Framework site there's a tutorial posted showing you how to set up file download functionality in a Zend Expressive-based application.

A common requirement of web-based applications is to upload and download files. But, out of the box, there’s no simple way to download them in Zend Expressive. This tutorial shows you how - step-by-step.

Recently, I was asked on Twitter by @dgoosens, about how to download files using Zend Expressive. The timing was pretty good, as I’d done a simple implementation in a recent Zend Expressive project. So I knocked up a quick example and he, @acelayaa, and I talked it over, making various changes and suggestions along the way.

So, In today’s tutorial, I’m going to walk through a 3-step process for downloading files when using Zend Expressive.

He breaks the process down into three parts:

    1. The Download Functionality
    1. Using the Download Method
    1. Running the Application & Downloading the File

He includes code or commands for each step, showing you exactly how to set up this simple piece of functionality. Additionally it's implemented as a (mostly) self-contained method using the Stream handler to set the required headers and body.

tagged: zendframework zendexpressive file download tutorial stream

Link: http://www.masterzendframework.com/downloading-files-in-zend-expressive/?platform=hootsuite

Laravel News:
Laracon US Live Stream
Jul 06, 2016 @ 09:38:40

On the Laravel News site there's an announcement about a live stream available for Laracon US happening at the end of July (2016) - the 27th through the 29th.

For the first time ever, Laracon US is offering a live stream plus early access videos.

When you purchase access on the day of the conference you will get an email link to access the stream and be able to watch all the talks except for the one by Ryan Singer.

You can pick up your ticket for just $25 USD and get access to both the stream and early access videos. You can find out more about the conference itself and the talks that are scheduled on the main conference website.

tagged: laraconus16 laracon live video stream earlyaccess video ticket conference

Link: https://laravel-news.com/2016/07/laracon-us-live-stream/

Andrew Carter:
PSR-7 Objects Are Not Immutable
May 24, 2016 @ 10:28:05

Andrew Carter has written up a new post about PSR-7 objects (the PHP-FIG defined standard for handling requests and responses in PHP applications) and how the objects themselves are immutable.

What’s happening [in the provided example] is that the Zend Expressive framework is rendering the error page to the same object that you wrote your message to. Whilst the actual message object itself is immutable, the body stream that it references is not. Even when this object is cloned or “modified” (to become a new object) it will still use the same stream.

He explains a bit about what this means in a more practical sense and why the PSR-7 standard and why this happens (as defined after much discussion). Then he gets into a more recent debate happening in the PHP-FIG about PSR-7 middleware and the proposal for a standard structure in its creation. He points to some of the thoughts from Anthony Ferrara on the topic and an example from Woody Gilk showing an exception handler and how having the stream always appending content is a bad thing in that particular case.

tagged: psr7 object immutability output zendexpressive middleware stream

Link: http://andrewcarteruk.github.io/programming/2016/05/22/psr-7-is-not-immutable.html

Rob Allen:
Filtering the PSR-7 body in middleware
May 16, 2016 @ 09:25:30

Rob Allen has a post to his site showing how you can filter the body content of your response in a PSR-7 compatible system via some simple middleware.

Sometimes, there's a requirement to alter the data in the Response's body after it has been created by your controller action. For example, we may want to ensure that our brand name is consistently capitalised. One way to do this is to create middleware that [uses str_ireplace to replace a string]. That's great, but what happens if the new string is shorter than the old one? For instance, suppose I want to replace all uses of "nineteen feet" with "19FT".

In his example, replacing that part of the content would result in an odd string because of how they'd be replaced in the stream. He shows how to use the in-memory handling of PHP's fopen to create a new content instance to push back into the response and back out to the waiting application.

tagged: psr7 middleware filter content strireplace stream fopen tutorial

Link: https://akrabat.com/filtering-the-psr-7-body-in-middleware/

Scotch.io:
How To Process Tweets in Real-Time with Laravel
Apr 29, 2016 @ 09:10:16

The Scotch.io site has a tutorial posted showing you how to combine Laravel and the Twitter Streaming API to process tweets in real time inside your application.

This tutorial will show how to use the Twitter Streaming APIs to process tweets in real-time from a Laravel application. There are a variety of use cases for this: perhaps you want to auto-respond to mentions of your company, run a contest via Twitter, or create support tickets when users complain about your product. For this tutorial, we'll build a "featured tweets" widget to display approved tweets on our app's home page.

He starts with an overview of the tools and terms you'll need to know about for the tutorial and a few notes of things to watch out for. He then describes the overall structure of the application (an app just to show the tweets) and links to a repository for the impatient. He then breaks up the rest of the tutorial into several steps:

  • Creating a new Laravel application and installing the Phirehose library
  • Building a "process tweet" job and matching TwitterStream class to use Phirehose and dispatch the job
  • Make the artisan command to connect to the API and the application you created
  • Configure your queue driver and run the processing command
  • Set up a "Tweet" model to connect the application and database table

He finishes the post showing how to make the ProcessTweet Job useful, set up some basic authentication and pass the currently processed tweets into the default "Welcome" view.

tagged: realtime tweet twitter process stream api laravel job queue tutorial

Link: https://scotch.io/tutorials/how-to-process-tweets-in-real-time-with-laravel#step-11-make-the-processtweet-job-useful

Jeff Geerling:
Streaming PHP - disabling output buffering in PHP, Apache, Nginx, and Varnish
Apr 06, 2016 @ 13:45:27

In a recent post to his site Jeff Geerling shows you how to disable the output buffering that PHP includes and create "streaming PHP" code similar to Drupal's recently introduced BigPipe handling.

For the past few days, I've been diving deep into testing Drupal 8's experimental new BigPipe feature, which allows Drupal page requests for authenticated users to be streamed and loaded in stages—cached elements (usually the majority of a page) are loaded almost immediately, meaning the end user can interact with the main elements on the page very quickly, then other uncacheable elements are loaded in as Drupal is able to render them.

[...] BigPipe takes advantage of streaming PHP responses (using flush() to flush the output buffer at various times during a page load), but to ensure the stream is delivered all the way from PHP through to the client, you need to make sure your entire webserver and proxying stack streams the request directly, with no buffering.

He decided to try out different configurations to see if he could reproduce the same thing outside of Drupal and - good news, everyone - he found a reliable way. He starts with a basic procedural script that emulates BigPipe and calls a flush inside a loop to push the latest output to the waiting client. While this cooperates on the command line the browser doesn't cooperate the same way. A small tweak helps it work, so he shows how to reproduce this reliably across the full stack - Nginx, Apache and Varnish.

He ends with a quick warning for those using VMWare/VirtualBox about some oddness he experienced in buffering the responses and includes a way to test if it's your script or the VM causing the trouble.

tagged: stream output disable buffering apache nginx varnish tutorial

Link: http://www.jeffgeerling.com/blog/2016/streaming-php-disabling-output-buffering-php-apache-nginx-and-varnish