Using Google Protcol Buffers with PHP and protoc-gen-php

Protocol Buffers are a binary data transfer protocol from Google. You define the structure of the data using a C-like text file, then compile that protocol buffer file into a library appropriate for the language you are using. Google officially supports Java, Python and C++.

PHP Protocol Buffers

UPDATE!

I haven’t tested it yet, but DRSlump commented below to let me know about a new, alive and mostly complete implementation he is using and working on. I haven’t tried it yet since it uses PHP 5.3 and we haven’t quite moved over from 5.2 yet, but here it is for your enjoyment:

DRSlump’s Protobuf-PHP

I could find no complete implementation for PHP at this time. There are several 1/2 completed protocol buffer implementations out there for PHP, most of which date back to near the initial protocol buffer announcement from Google.

If you are going to use Protocol Buffers in a PHP project, you will probably want to evaluate the features and approach of each of these projects. For the project I am working on I decided to go with protoc-gen-php since it seemed to have the most complete (3/4ths complete?!) implementation and the most recent update.

Status of protoc-gen-php

There were two branches of protoc-gen-php when I started using it. Bramp was the original author and responded to emails, Iamamused has a few more features, but didn’t respond to any of my emails. I took the email response to mean, in part, that despite the lack of updates that Bramp was still interested in at least maintaining the project.After forking his project to add some features I needed, he did in fact indicate that he will indeed merge appropriate changes from my fork and from Iamamused back into the main branch.

Until that happens though you’ve got three choices:

My fork of Protoc-gen-php

I don’t really have interest in being a project maintainer long term. If you use my branch, I’m happy to help in whatever capacity I’m able until Bramp merges the features I’ve added back into his branch.

Here’s what I’ve added:

  • Signed Int support (sint32 / sint64)
  • Float and Double support
  • Support for the [packed=false] option so that repeating packables can be used (ie. not packed support, just the ability to specify non-packed)

The big caveat with float support is that PHP uses doubles to represent both floats and doubles, so when a float-double gets packed into actual an actual float in the protocol format it does lose precision. The best solution will be to either use a string or a double.

Need Other Features Added?

I have implemented the features I did because I needed them for the project I am working on. I’d be happy to add other features on a consulting basis, or to merge in your changes if you submit patches to me. Like I said though, hopefully it will be merged back into Bramp’s branch pretty soon, and he can handle the patches. It would be one less thing for me to worry about!

This entry was posted in Something Interesting and tagged , , . Bookmark the permalink.

7 Responses to Using Google Protcol Buffers with PHP and protoc-gen-php

  1. DrSlump says:

    I’m maintaining a fairly new but mostly complete PHP implementation at https://github.com/drslump/Protobuf-PHP.

    We’re using it at a pretty large project at work so it’s going to be maintained for the foreseeable future.

    • stuporglue says:

      Thanks for posting this!

      PHP implementations of Protocol Buffer are one of those things that Google poorly. It seems all the old outdated info rises to the top, and newer but more applicable projects like yours don’t get the Google love they deserve.

  2. Pingback: PHP Protocol Buffer to MySQL (and back!) bridge » Stuporglue.org

  3. AddTransit says:

    Hello,

    I just came across this article. As this article is now about 4 years old, can you tell me what is your current preferred method of working with PHP and Protocol Buffers?

    Thanks,

    Neil

    • stuporglue says:

      I’m afraid I’m no longer involved in the project that uses Protocol Buffers, but…

      If I were starting a new project that wasn’t already using Protocol Buffers, I’d probably just use JSON and live with the overhead. JSON is natively supported or has readily available libraries just about everywhere.

      If I needed to use protocol buffers and the project were new, I’d consider a language with better Protocol Buffer support like Python.

      If the project called for PHP and I had to use Protocol Buffers, I’d find the most active PHP protocol buffer project and use that.

  4. AddTransit says:

    Thanks for the quick reply.

    In spite of Protocol Buffers compression, like you I also prefer JSON due to it’s human readable form making it much more accessible for end users to understand and debug.

    However as we work with Google’sGTFS Realtime (for Transit), we must work with Protocol Buffers.

    I’ll have a look at these projects and see which one best suits.

    • stuporglue says:

      Hey! At least that looks like a fun project to work with. I got my masters in GIS and do a fair bit of web mapping, but I have never done anything with GTFS Realtime.

Leave a Reply

Your email address will not be published. Required fields are marked *