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

Muhammad Zamroni:
Streaming CSV Using PHP
Feb 16, 2018 @ 15:19:47

On his Medium.com site Muhammad Zamroni has a quick tutorial posted showing how to create a system that will [stream CSV data] in a Laravel application (https://medium.com/@matriphe/streaming-csv-using-php-46c717e33d87) back to the waiting client.

In one of our application, there’s a need to get list of data from a service. This data is used to generate report by background process, resulting a XLSX file that can be downloaded or attached to email. This service (an API) is written in Laravel. The background process is also written in Laravel. Both service are hosted in different server.

We pull data from MySQL and simply send the response in JSON to be easily parsed. The problem we encountered was the amount of data.

The main issue was the memory that it required to pull in all of the data and work with it. Based on some suggestions from another article they decided to switch from JSON to CSV for output and use the chunk handling to process pieces of data at a time. He includes the code for the controller that shows the use of chunk and a manual file pointer to push the data into the response 1000 records at a time.

tagged: stream csv content response laravel chunk tutorial

Link: https://medium.com/@matriphe/streaming-csv-using-php-46c717e33d87

Oscar Merida:
Creating ZIP files and streaming the response with Silex
Jan 19, 2018 @ 17:28:44

On his site Oscar Merida has shared a post showing how to create a ZIP file and stream the response in a Silex-based application using the DOMPDF library and PHP's ZipArchive functionality.

Creating a zip file is easy and Rob Allen has you covered on that front to get you started. I needed to do this to create a zip file of PDF files generated by Dompdf. It turns out, you can add files to a ZipArchive object without actually creating the file if you can get the contents as a string.

The post includes the code to create the DOMPDF instance, load the HTML and render the result. From there the ZipArchive takes over and creates the archive from the PDF result. Finally the code takes the ZIP archive as input and pushes it out with a custom header to tell the browser to download it and streams back the raw ZIP contents.

tagged: tutorial silex dompdf pdf zip archive silex stream download

Link: http://oscarm.org/2018/01/stream-a-zip-file-via-silex/

Sergey Zhuk:
ReactPHP PromiseStream: From Promise To Stream And Vice Versa
Dec 07, 2017 @ 15:19:51

Sergey Zhuk has posted another article to his site covering functionality provided in ReactPHP. In this latest tutorial he covers the PromiseStream handling of the library allowing for the translation from promise to stream (and back).

One of the patterns that are used to deal with streams is spooling: we need the entire resource data available before we start processing it. One approach is to collect each chunk of data received from the stream.

But, imagine that we have some client code that wants to process some data from a file. It doesn’t care about the streams, it only needs to receive the entire data from the file. With this approach, this code should be called inside the callback for the end event of the stream. So, the client code should now about streams, events, and callbacks. But sometimes it’s impossible.

Example code is included to illustrate the problem above and an answer is provided in the form of ReactPHP promises. This allows the data to move into the promise as the data is being read from the stream's source. The tutorial goes on to talk about the functionality behind this transition including the buffer method to create the promise with chunked data, the all method to build the promise from the full data in the stream and the first method that works with events on the stream. The article then covers the reverse, showing how to pull information from a promise and push it back out to a stream via the unwrapReadable and unwrapWritable methods.

tagged: reactphp promise stream migrate read write tutorial promisestream

Link: http://sergeyzhuk.me/2017/12/07/reactphp-promise-stream/

SitePoint PHP Blog:
How to Read Big Files with PHP (Without Killing Your Server)
Nov 21, 2017 @ 19:19:27

On the SitePoint PHP blog, there's a tutorial posted showing you how to deal with large files without "killing your server". In this case, it's not about the upload process but about the handling of large files on the server side.

It’s not often that we, as PHP developers, need to worry about memory management. The PHP engine does a stellar job of cleaning up after us, and the web server model of short-lived execution contexts means even the sloppiest code has no long-lasting effects.

There are rare times when we may need to step outside of this comfortable boundary — like when we’re trying to run Composer for a large project on the smallest VPS we can create, or when we need to read large files on an equally small server. It’s the latter problem we’ll look at in this tutorial.

They start off by describing how they plan to measure the success of the improved file handling, mostly around the memory usage required to work with the file. It then gets into some of the options available including:

  • reading files line by line
  • piping between files
  • using filters

The last option, the filters, seems to be the best one. He uses this one and customizes the handling with different configurations and custom protocols. All related code is included in the post and is avaialble on GitHub.

tagged: read big file memory consumption filter stream tutorial

Link: https://www.sitepoint.com/performant-reading-big-files-php/

Sergey Zhuk:
Building ReactPHP Memached Client: Making Requests And Handling Responses
Oct 26, 2017 @ 16: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 @ 18: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 @ 17: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 @ 17: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 @ 15: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 @ 14: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/


Trending Topics: