Ulf WendelPECL/mysqlnd_ms: how failed XA transactions get fixed (29.7.2014, 10:24 UTC)

XA transactions are an open standard for distributed transactions. A distributed or global transaction can spawn multiple databases. XA transactions come handy, for example, when data sets are sharded over multiple servers and a business transaction affects multiple shards. The free MySQL Fabric tools help to setup and manage a sharded MySQL cluster. The development version of PECL/mysqlnd_ms 1.6 helps with XA transactions. It abstracts SQL details and acts as a transaction manager. The PHP MySQL driver kicks in when things go wrong and XA transactions show their nasty side: blocked servers. Good news: this is a rare case. Bad news: a deep dive below.

Grant all Ulf’s a 10% discount (or none of them = use a transaction)
MySQL shard A: EMEA customers MySQL shard B: US customers MySQL shard C: APAC customers

/* Note: Fabric and XA don't go together - yet... it's coming! */
$link = 
new mysqli("mysql_sharding_cluster", "user", "password");

mysqlnd_ms_xa_begin($link, 1);

mysqlnd_ms_fabric_select_shard($link, "shop.customer", "EMEA");
$link->query("UPDATE discount = 10 WHERE first_name = 'Ulf');

mysqlnd_ms_fabric_select_shard($link, "shop.customer", "US");
$link->query("UPDATE discount = 10 WHERE first_name = 'Ulf');

mysqlnd_ms_fabric_select_shard($link, "shop.customer", "APAC");
$link->query("UPDATE discount = 10 WHERE first_name = 'Ulf');

mysqlnd_ms_xa_commit($link, 1);

PECL/mysqlnd_ms as a transaction manager

XA transactions use the two-phase commit protocol, which is a blocking protocol. Please, see also my previous blog post on the nature of the protocol and MySQL implementation limitation. If the client that drives the XA transaction, your PHP script, crashes at a certain point, some XA participants (MySQL servers) cannot make any progress. In the worst case, they end up waiting for a decision on the global transactions outcome endlessly. No, there is no timeout. As they wait, they block resources. That can be memory used for the transaction or some lock on some table.

Blocked during the second phase of the 2PC/XA protocol
PHP (coordinator) MySQL (participant) MySQL MySQL
–> Global commit  
  Comitted Uncomitted: waiting for global commit or rollback

Any serious user of XA transactions will therefore have to implement some mechanism that ensures progress in case of crashes. After a crash, it must be possible to learn which participant is blocked, connect to the participant and tell it to either commit or roll back the open transaction. This housekeeping job is rather annoying, yet important. PECL/mysqlnd_ms can do it for you, it can act as a transaction manager. (On an aside: the academic world does distinguish between a transaction manager and coordinator. I am using the terms interchangeably here.)

MySQL as a state store to track XA transactions

Upon request, PECL/mysqlnd_ms can record the state of each global transaction in a MySQL database. Should your PHP script (and with it PECL/mysqlnd_ms) crash or be interrupted in another way with an XA transaction being unfinished, then the next PHP script that runs can check the database and “garbage collect” the unfinished global transaction. The “next” PHP script could be run on the same server or another one, as long as all servers use the same MySQL database to track XA trans

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

thePHP.ccHHVM: The Alternative PHP Runtime (29.7.2014, 07:00 UTC)
Cal EvansInterview with Joel Clermont (29.7.2014, 05:00 UTC) Link
Bruno ŠkvorcUnderstanding OpCache (28.7.2014, 16:00 UTC)

The PHP in version 5.5 comes with a caching engine build-in - OpCache - which stores precompiled script bytecode in the memory. If you’re familiar with APC or Xcache, you will already know how such engines work. As each PHP script is being compiled at runtime, a part of the execution time gets used for transforming the human readable code into a code that can be understood by the machine. A bytecode cache engine like OpCache, APC or Xcache does it only once - during the first execution of a specific PHP file. Then the precompiled script is being stored in memory, which should lead to performance boost in your PHP applications.

Over the web you will easily find a lot of tutorials covering all the OpCache installation and configuration steps (it is enabled by default on 5.5, but can be installed as an extension on older versions). Read the article below to find the answers to some of the typical questions regarding different practical aspects of working with this particular cache engine.

1. Is OpCache worth installing at all? What speed boost can I expect?

Of course, it depends. If your server manages to handle the incoming traffic and keep low response times, you probably won’t feel the need to work on performance. But on a bigger website with a lot of traffic each little optimization step may count. Implementing OpCache may allow you to handle more requests per second and return the response quicker than without a bytecode caching engine. As OpCache is quite easy to install and configure, you won’t spend too much time on setting everything up.

Continue reading %Understanding OpCache%

Bruno ŠkvorcDiffbot: Crawling with Visual Machine Learning (27.7.2014, 18:00 UTC)

Have you ever wondered how social networks do URL previews so well when you share links? How do they know which images to grab, whom to cite as an author, or which tags to attach to the preview? Is it all crawling with complex regexes over source code? Actually, more often than not, it isn’t. Meta information defined in the source can be unreliable, and sites with less than stellar reputation often use them as keyword carriers, attempting to get search engines to rank them higher. Isn’t what we, the humans, see in front of us what matters anyway?

If you want to build a URL preview snippet or a news aggregator, there are many automatic crawlers available online, both proprietary and open source, but you seldom find something as niche as visual machine learning. This is exactly what Diffbot is - a “visual learning robot” which renders a URL you request in full and then visually extracts data, helping itself with some metadata from the page source as needed.

After covering some theory, in this post we’ll do a demo API call at one of SitePoint’s posts.

PHP Library

The PHP library for Diffbot is somewhat out of date, and as such we won’t be using it in this demo. We’ll be performing raw API calls, and in some future posts we’ll build our own library for API interaction.

If you’d like to take a look at the PHP library nonetheless, see here, and if you’re interested in libraries for other languages, Diffbot has a directory.

Continue reading %Diffbot: Crawling with Visual Machine Learning%

Bruno ŠkvorcMVC – a Problem or a Solution? (26.7.2014, 18:00 UTC)

Repositories, Adapters, MVC with all it cousins, SOLID, RTFM… As a (PHP) developer, those words are thrown at you from every corner of the web. And I hate it, I’ve had enough. Stop telling me what to do and show me those kittens instead.

Software solves problems

We don’t just write software. Code doesn’t fall out of the sky into our files. We analyze the requirements, split them into small problems we know how to solve, and then we solve those small problems. Every line of code you write, have written and will ever write solves a certain problem. Be it to save the world, show kittens on a screen or make it look good in IE8. It’s there for a reason, don’t you touch it!

Problems are solvable and the solutions for these problems become part of something bigger. A black box that satisfies all initial requirements. But how do we solve these problems? Is my solution the best solution? Will other developers (or me in 2 months) understand what I did here?

Continue reading %MVC – a Problem or a Solution?%

Anna FilinaBad conduct: the root of the problem (26.7.2014, 00:35 UTC)

More and more tech events are publishing codes of conduct. Although I think that it’s a good idea, I find it sad that we need to tell people things as basic as “treat others like you want to be treated”. I would like that in addition to treating the symptom, we understand and treat the root cause. Bad behavior incubates and spreads like a virus. We have a plague on our hands and we need to start controlling it!

Bad behavior existed since the dawn of man, but the Internet makes it easy to be offensive, mainly because the target is just a name or a profile picture. The interactions are less personal. Here are examples of bad behavior. I have personally been victim of ALL of these, as well as guilty of a few myself. Watch out for these both in others and in yourself.

Rape jokes

  • “Don’t drop the soap.”
  • “We totally raped your team.”
  • “He’d rape you if he knew that you listen to that singer.”

What’s really sad is that there’s no shortage of top ten lists.


  • Not leaving the person alone when asked to.
  • Directing derogatory comments at an individual. “You are an idiot.”
  • Using profane or indecent language, or make obscene suggestions. “He’ll stick his d*** up your a** if you say no.”

Yes, many insults are considered harassment by the law. As a side note, law exists to tell people how to behave in society, like telling people that they shouldn’t kill others, which is basically an expanded version of the ten commandments.

Intimidation and threat

  • “If you use that programming language, this is what will happen: (posts image of a person with a gun)”
  • “We’ll take your head off.”
  • Including partners, employers or clients in a disagreement to put the person’s livelihood at risk.
  • Suggest that harm may come to the person’s children.

Basically, if it can cause someone great discomfort, it’s intimidation. If it can cause fear, it’s a threat.


  • Spreading gossip and rumors with the intent of making others dislike or hate the person.
  • Posting material to intentionally defame or humiliate someone (e-mail with cc, blog posts, wikis, forum thread, etc.)
  • Vandalizing the person’s website, or websites that talk about that person.
  • Tweet trash about people without addressing them directly, but instead referring to them through mentions and links to their websites (exposing).

This is not limited to teenagers as you might think. Even CEOs sometimes engage in bullying. I’m not joking.

In the real world

Seeing others behave badly on the Internet makes such behavior seem acceptable, eventually making it seem acceptable offline. This got so out of hand that we need to explain to people that it’s not ok to use sexual imagery during a presentation at a tech conference. I even had a user group organizer draw his knife twice during a meeting because someone mispronounced his group’s name.

But what if the target did something wrong?

No. The reason is irrelevant. Nobody has the right to be the judge and executioner. If you see someone doing any of those things, no matter the reason, no matter what the other person did wrong, remind them that it is NOT acceptable behavior. They can’t solve a war with more rockets. That’s a great metaphor, use it. You can even post a link to this article.

Yes, you will be accused of tone policing and such, but you don’t need to reply. Anybody who thinks that the end justifies the means is just part of the problem. The silent witnesses of the thread already heard you. The seed has been planted. If some replies bother you, unsubscribe, ignore, block, stop notifications.

Will we succeed?

Everybody can make mistakes and correct them. If you want people to open their minds to criticism, call out their mistakes it in a non-threatening and non-humiliating way. People don’t respond well when their ego is attacked. Give them the benefit of the doubt to increase your “conversion rate”.

If enough people call out bad behavior, it will seem less okay. People will start to question themselves. Together, we will slowly tip the balance and eventually won’t need to explain the basics of respect on our websites.

Related read: On ethics and optimism.

Bruno ŠkvorcUsing Guzzle with Twitter via Oauth (25.7.2014, 16:00 UTC)

In a previous article, we found out about Guzzle and how it can aid us in the task of establishing communication with third party API’s over HTTP. We used it to get the output of a random number generator and basic interaction with Github’s API. It also offers a series of ‘subscribers’, log-subscriber being one of them and showed us how easy it was to integrate monolog into it.

While interacting with Github’s API we discovered that it supports basic authentication (sending plain username/password). But what if the API we want to use just offers OAUTH authentication?

Guzzle’s Oauth

Implementing OAUTH from scratch can be a hard and time consuming task, and would be like reinventing the wheel… You could grab a third party library to deal with OAUTH requests, but why do that when guzzle has its own OAUTH subscriber? Yay!

First of all, I have to make you aware that Guzzle has dropped support for PHP 5.3, so you’ll need PHP 5.4 to follow this exercise. The installation instructions from the previous article changed a little bit, yet composer makes our lives a lot easier.

Our goal is to interact with the Twitter API and get our own timeline for which ever purpose we need. We’ll begin with our composer.json file:

Continue reading %Using Guzzle with Twitter via Oauth%

PHP ClassesPHP Core Developers versus Facebook: Round 3 - The PHP Specification (25.7.2014, 08:43 UTC)
By Manuel Lemos
Recently Facebook developers played on another episode of a saga that seems to antagonize Facebook and PHP Core developers.

This time they announced an initiative to create a formal specification document for the PHP language.

Read this article to learn more about this and previous episodes of this saga, and what this may mean for the PHP future.
Bruno ŠkvorcGetting Started with ImpressPages (24.7.2014, 16:00 UTC)

Content management systems (CMSs) are among the most used web tools nowadays. Users range from designers and developers, to bloggers and project managers. However, even with a wide range of products on the market, users often neglect to try out new systems and experience new possibilities.

To broaden my horizons, I decided to explore the market and see what other CMSs have to offer. This time I am going to look into ImpressPages, a PHP based CMS built on an MVC engine.

What makes ImpressPages differ from other CMSs is code structure, customizability, the admin panel built on Bootstrap and an inline drag and drop editor. In this step-by-step tutorial, I am going to cover the very first steps of launching a website on ImpressPages.

Downloading and Installing ImpressPages

Download ImpressPages first. It’s important to check if your system meets all the requirements for running the CMS:

  • Apache server with “mod_rewrite” (adapt to Nginx as needed)
  • PHP 5.3.3 or newer;
  • PHP GD library;
  • PHP mbstring library;
  • PHP curl library;
  • PHP PDO library;
  • MySQL 5 and higher;
  • 30+ MB free disk space;
  • ini_set function enabled.

After you create a database in MySQL which ImpressPages will use, unzip the contents of its archive into your web server’s root, or wherever you mean to serve it from, and run it (either via localhost, or through the virtual host you’ve set up, depending on your configuration). The installation process has been recently updated and now takes only two steps instead of six.


After configuring your site information (website name, email and time zone), you will be automatically transferred to database installation. If your system doesn’t meet all the requirements, an error will be shown with information on what needs to be fixed.


Once the installation is complete, you will be transferred straight to your new website. You’ll be be able to change the administrator’s credentials there.

Continue reading %Getting Started with ImpressPages%

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