Google's Protocol Buffer's project provides both a PHP implementation and a PHP extension for processing data. The PHP implementation provides a workable solution anywhere that PHP can be used, but is not as efficient. The extension requires compiling the C source and installing the extension, but provides much better performance in data parsing. I noticed this difference in performance while working on parsing some Open Street Map dump files.
I do my primary development work on windows, and wanted to get the extension installed for the performance boost. There does not appear to be an official pre-compiled DLL available anywhere for this extension, which means I needed to compile it myself. After a few hours of figuring out how to compile PHP and it's extensions on windows, I was able to get the DLL built and loaded. I'm documenting the process here for anyone else that needs this (or any other PECL extension) that is not officially provided.
This guide is for PHP 8.x, other versions may require different steps or requirements.
The PHP documentation has a Wiki page documenting how to build PHP and extension on windows. The documentation is pretty straight forward, but I found some of the items seemed to be a little out of order to me, so here I will attempt to provide a more step-by-step process.
First, obtain the software and sources required to perform the compilation.
- The PHP Source code for the version you are targeting. For this write-up, that will be 8.2.11.
- Visual studio community 2019. New versions are not currently supported by the PHP build system.
- The PHP binary tools SDK. Version 2.2.0 as of this writing.
- The protobuf PECL extension. Version 3.24.3 as of this writing.
- Any other PECL extensions you'd like to build.
I opted to perform this task in a clean Virtual Machine environment rather than in my daily OS. If you'd like to do this as well, you'll also need
Create a new virtual machine and install windows on it. If using virtual box, I recommend installing the Guest additions and setting up a shared folder for the virtual machine to easily transfer files between the virtual machine and the host.
Installing Visual Studio
Launch the visual studio installer. Continue through the installer until you reach the prompt for which development tools you would like to install. Select the option for Desktop development with C++ and continue.
When the installation is complete, close visual studio and it's installer.
Set up the PHP build environment
- Extract the PHP binary tools SDK to C:\php-sdk.
- Open the x64 Native tools Command Prompt provided by visual studio.
- In the command prompt, type
cd c:\php-sdkto enter the SDK directory
phpsdk-vs16-x64to set up the SDK environment and enter the SDK shell.
bin\phpsdk_buildtree phpdevto create the necessary directory structure.
- Extract the PHP source code into the newly created folder
c:\php-sdk\phpdev\vs16\x64. The source should be in its own folder, for example
- In the command prompt, enter the PHP source directory.
mkdir peclto create the pecl directory where the extensions will be placed.
- Extract the protobuf pecl source into the newly created pecl directory. The source should be in its own folder, e.g.
Building the extension
- In your command prompt, ensure you are in the PHP source directory.
buildconfto generate the configuration files.
configure --helpto view all the configuration options available to you. You should see the
--enable-protobufoption in the list.
configure --disable-all --disable-zts --enable-cli --with-all-shared --enable-protobufto configure the build PHP with the protobuf extension as a shared library. If you have other PECL extensions, add their --enable flag as well. If you need a thread safe build, omit the
If the configuration is successful, you should see a summary of the configuration, including a list of the extensions to be built. Ensure that protobuf is in that list and is marked as shared.
nmaketo build PHP and the extensions.
The nmake build process will take a few minutes to complete, and may show some warning messages in the process. When the build is complete, you will see a message
SAPI sapi\cli build complete.
The build output files wll be located in the
c:\php-sdk\phpdev\vs16\x64\php-8.2.11\x64\Release directory. Find the php_protobuf.dll file in this folder. Copy the php_protobuf.dll file to your PHP installation's ext directory and edit the php.ini file to load the extension.
While I recommend building your own DLL, for those who cannot, you may download the DLLs I built. These DLLs are for the VC16 x64 Non Thread Safe versions of PHP.