SitePoint PHPUsing the Battery Status API to Optimize Your Development (1.12.2015, 23:00 UTC)

battery icon

Imagine if you could program your app to adapt to the end user's device -- such as turning off certain processes if the user's device has a low battery status. Well, you can.

The Device APIs Working Group is a series of specifications drafted to provide developers with access to additional information about a user's device, so that a more tailored experience can be created. This working group contains several evolving standards, including the Battery API which we will take a look at in this tutorial.

The Battery API is now supported on several desktop and mobile browsers, so now is a good time to look at the Battery API and see what it is, where it's supported, and why you should consider leveraging it in your upcoming projects.

The Battery API: A Brief Introduction

The Battery Status API (Battery API) is exactly what it sounds like: an interface that lets developers access information about the current user's battery.

It's a JavaScript-level API that enables developers to hook into various events and properties of the user's device. For example, if we have access to the API, we can get the current battery percentage and, based on that value, set good default settings. For example, if the user's device is in a low battery state, we can remove all background tasks and make them manual only.

Since early 2011, this API has been continually under development, moving back and forth from working draft to candidate recommendation status at the W3C. Over this period, several browsers have adopted support -- though some more than others.

How Can the API Help Us?

Since the specification is not yet finalized, we can't count on developing websites or web apps that fully depend on the Battery API. What we can do, however, is provide additional functionality, or a more customized experience, if the current browser supports the API -- i.e., using it in a progressive enhancement style.

What Information Can We Access?

Since we can access the user's battery state, we can determine their current battery level and whether the device is charging or discharging. In addition, we can sometimes get the device's approximate charge or discharge time (the time until the device is either charged or dead).

With this information, we can start to change the user experience to help the user.

What Could We Use This Information for Anyway?

Even though battery optimization generally isn't in the forefront of a developer's mind, it's a factor that should be considered, especially with everything moving towards mobiles.

Websites offer a range of useful but expensive operations that could be adjusted so that your users spend less of their battery on your website. (No one likes a website or web app that demolishes your battery.)

Let's look at some of the ways we could save some battery power:

  • We could customize the Geolocation elements so that they have a lower accuracy and don't request usage of the inbuilt GPS by default. We could have two options displayed to the user, one to request it normally and another with high accuracy.
  • We could set video streams or playback to lower quality levels by default, making data transfers are quicker (and thus requiring less work of the device). An option to download the high quality stream could be provided.
  • We could reduce complex CSS animations or interface elements and provide a more basic interface.
  • We could use the battery level to determine the default interval in which background tasks are run -- such as Ajax and Web Workers. We could use the state to determine how frequently our tasks should be called, in order to minimize network usage. Providing an option or setting so users could set what they want is also a good idea.

Overall, the idea is that we can use the Battery API to provide some good defaults that save battery power, while providing more options for interacting with the website.

Browser Support

Since the Battery API is not finalized, it doesn't have universal support across all browsers yet.

On a positive note, a good number of browsers now support it (as of November 2015), so now's the perfect time to get up and running with this API.

Mobile Devices

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

Stefan KoopmanschapThe PHP Developer During Advent (1.12.2015, 16:30 UTC)

It is December 1st, which is the traditional start of the advent period, the period leading up to Christmas. It has also in the past years sparked initiatives in the PHP, web development and Open Source communities. Some of those initiatives I want to mention here for everyone's enjoyment and education.

24 Pull Requests

To give back to the open source projects we all use on a regular basis, the suggestion of 24 Pull Requests is to send a pull request per day to any open source project. They give you suggestions on the projects and you can of course suggest other projects. It's an interesting challenge to send a PR every day up until Christmas, but if you're up for it, many projects will be grateful.

24 Days In December

During the days until Christmas, this website will post a daily thought on PHP and the community. Add it to your feed reader. Spoiler: I have contributed.

24 Ways

For those that do more than just PHP, 24 ways will give you an article per day on web design and development. Even if you only do PHP, it might be a good idea to broaden your horizon in December using this site.

Advent of Code

If you're up for a series of coding puzzles, you might enjoy Advent of Code. Advertised as interesting for any level developer, you can do a new puzzle every day.

24 Days of Blackfire

To help guide you through "the challenges of managing the performance of your applications", Blackfire is publishing 24 tutorials under the 24 days of Blackfire name.

Paul M. JonesFirst Stable Aura 3.x Releases (1.12.2015, 15:57 UTC)

Today we released the first round of stable Aura 3.x packages:

Since the announcement of the plans for Aura 3.x, we have made one small concession: the minimum PHP version is 5.5, instead of 5.6 as originally announced. Even so, all the 3.x packages are tested and operational on PHP 5.6, PHP 7, and HHVM.

Via the Aura blog at

Derick RethansNew MongoDB Drivers for PHP and HHVM: History (1.12.2015, 09:07 UTC)

New MongoDB Drivers for PHP and HHVM: History

We recently released a new version of the MongoDB driver for PHP. This release is the result of nearly a year and a half work to re-engineer and rewrite the MongoDB driver. In this blog post, I will cover the back story of the how and why we undertook this effort.

The original driver was created by Kristina and released in early 2009. The extension had a very simple architecture and was written mostly in PHP. A handful of core functions were implemented in C, such as creating a resource for the connection, standard CRUD operations, query execution, and iterating over a result set (sans an Iterator interface). All of the convenient user facing APIs were implemented in PHP code. There were also a few classes to encapsulate certain MongoDB data types that PHP otherwise could not represent, such as its ISODate, ObjectID and Regex types.

A small example to create a connection, insert a document, and retrieve a result set looked like:

<?php include "Mongo.php";

$m = new Mongo();
$c = $m-?>selectCollection("phpt", "find");

$c->insert( array(
        "foo" => "bar",
        "a" => "b",
        "b" => "c")

$cursor = $c->find(array("foo"=>"bar"), array("a"=>1,"b"=>1));

while ($cursor->hasNext()) {

Support for PHP 5.3 was soon added. In this release, much of the syntactic sugar in the form of PHP classes and code had been replaced by implementations in C. Where most (if not all) PHP extensions use phpt tests, the MongoDB driver instead implemented its test suite with PHPUnit tests. The main difference between test methods, is that phpt tests are all run in a separate PHP process in isolation, whereas PHPUnit tests re-use the same process by default. Which means that if one test case causes a crash in the driver, none of the others run either, and no result is visible.

In late 2009, the MongoDB driver for PHP reached a stable state with version 1.0.0. It added a feature to placate some PHP developers that refused to use single quotes around query operators. Query operators in MongoDB have the form of $ + operation, such as $gte for Greater Than or Equal or $set for setting fields during updates. As they are array keys, extra care needs to be taken. For example, the following does not do what you expect it to do:

$r = $c->find( array( "fieldname" => array( "$gte" => 42 ) ) );

The $gte in the double quotes of course causes a variable substitution, usually evaluating to an empty string. Instead of educating users to use single quotes (as in '$gte'), the driver acquired a way to replace the $ symbol to signal a query operator with a different symbol:

ini_set('mongo.cmd', '@');
$r = $c->find( array( "fieldname" => array( "@gte" => 42 ) ) );

The driver would replace these symbols before sending them on to the server. A little later, a warning was added in case an empty array key was found.

The 1.0.0 release also settled issues with serialising PHP variables to database types. MongoDB uses Binary JSON (BSON) as its internal format, and the PHP driver needs to serialize to and from this data type. BSON also supports compound data types, arrays (packed numerical keys), and documents (basically hash maps, or something akin to JSON objects). Because in PHP there is really only one array type, the decision was taken that both BSON documents and BSON arrays would be deserialized into PHP variables as arrays. The following example illustrates this:

<?php // Make a connection, select the 'test' collection in the 'demo' database,
// and clean out the collection.
$m = new Mongo;
$c = $m-?>selectCollection( 'demo', 'test' );

// Construct two documents
// 1. A stdClass object
$docObj = new stdClass;
$docObj->foo = 'bar';

// 2. An array
$docArr = [ 'foo' => 'baz' ];

// Insert documents into the collection:
$c->insert( $docObj );
$c->insert( $docArr );

// Query and show results
$r = $c->find();

foreach ( $r as $record )
        print_r( $record );

The result of this scr

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

Cal Evans (Voices of the ElePHPant) Its the Booze Talking – Contributing to Open Source (1.12.2015, 05:00 UTC)

@TessaMero – Tessa Mero

@elstamey – Emily Stamey

@AmbassadorAwsum – Amanda Folson

@ircmaxell – Anthony Ferrara

@Crell – Larry Garfield

@JoePFerguson – Joe Ferguson

Show Notes

The post Its the Booze Talking – Contributing to Open Source appeared first on Voices of the ElePHPant.

PHP ClassesPHP Automated SMS Gateway for Request and Response Service Part 2: Automatic Responses (1.12.2015, 03:00 UTC)
By Dave Smith
Once you start improving your SMS response service using the DOTGO system, the types of the messages that you want to handle get more complex, so you need a better way to organize and process those messages.

Read this article to learn how to how to use the PHP DOTGo Engine package to handle more complex responses using DOTGO Web service's terminating nodes.
Fabien PotencierAnnouncing 24 Days of Blackfire (30.11.2015, 23:00 UTC)

I still remember the excitement I had 15 years ago when I discovered my first programming advent calendar; it was one about Perl. It was awesome, and every year, I was waiting for another series of blog posts about great Perl modules. When I open-sourced symfony1, I knew that writing an advent calendar would help adoption; Askeet was indeed a great success and the first advent calendar I was heavily involved with. I wrote another one, Jobeet, for symfony 1.4 some years later.

And today, I'm very happy to announce my third advent calendar, this one is about Blackfire. This time, the goal is different though: in this series, I won't write an application, but instead, I'm going to look at some development best practices which includes topics like profiling, performance, testing, continuous integration, and my vision on performance optimization best practices.

I won't reveal more about the content of the 24 days as the point is for you to discover a new chapter day after day, but I can already tell you that I have some great presents for you... just one small clue: it's about Open-Sourcing something. I'm going to stop this blog post now before I tell you too much!

Enjoy the first installment for now as it has just been published.

SitePoint PHPFilling out PDF Forms with PDFtk and PHP (30.11.2015, 17:00 UTC)

PDF files are one of the most common ways of sharing documents online. Whether we need to pass our clients’ documents to third-party service providers like banks or insurance companies, or just to send a CV to an employer, using a PDF document is frequently the first option.

PDF files can transfer plain/formatted text, images, hyperlinks, and even fillable forms. In this tutorial, we’re going to see how we can fill out PDF forms using PHP and a great PDF manipulation tool called PDFtk Server.

To keep things simple enough, we’ll refer to PDFtk Server as PDFtk throughout the rest of the article.

Digital document illustration


We’ll use Homestead Improved for our development environment, as usual.

Once the VM is booted up, and we’ve managed to ssh into the system with vagrant ssh, we can start installing PDFtk using apt-get:

sudo apt-get install pdftk

To check if it works, we can run the following command:

pdftk --version

The output should be similar to:

Copyright (c) 2003-13 Steward and Lee, LLC - Please This is free software; see the source code for copying conditions. There is NO warranty, not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

How It Works

PDFtk provides a wide variety of features for manipulating PDF documents, from merging and splitting pages to filling out PDF forms, or even applying watermarks. This article focuses on using PDFtk to fill out a standard PDF form using PHP.

PDFtk uses FDF files for manipulating PDF forms, but what is an FDF file?

FDF or Form Data File is a plain-text file, which can store form data in a much simpler structure than PDF files.

Simply put, we need to generate an FDF file from user submitted data, and merge it with the original PDF file using PDFtk’s commands.

What Is Inside an FDF File

The structure of an FDF file is composed of three parts: the header, the content and the footer:

Continue reading %Filling out PDF Forms with PDFtk and PHP%

Paul M. Jones50% Off “Modernizing Legacy Applications in PHP” (30.11.2015, 14:37 UTC)

For Black Friday/Cyber Monday, and the rest of this week, my books Modernizing Legacy Applications in PHP and Solving the N+1 Problem in PHP are 50% off. If you’ve been waiting for a sale to get these books, now is your chance!

Thijs FerynFabrizio Branca – Talking about Magento & living in the Bay Area (30.11.2015, 08:59 UTC)

This is the last episode of the Zendcon 2015 series. And the term “last but not least” definitely applies here. I

The post Fabrizio Branca – Talking about Magento & living in the Bay Area appeared first on Thijs Feryn's blog.

LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP