Generating PHP Classes from Protocol Buffer

Just like we need to compile our Protocol Buffers using the Protocol Buffer Compiler (protoc) into Objective-C classes before we can use it in an iOS project, we also need to compile the Protocol Buffer into PHP classes before we can use it in a server side script.

To get the PHP source code, you can download it from http://code.google.com/p/pb4php/.
Disclaimer: Just like the Objective-C one, this is a third-party open-source library that’s not officially supported by Google. Also, I am a lot more experienced in iOS than in PHP, so what I am showing you is something that will do, but might not be the best approach.

You will need to have some kind of PHP server set up, so later you can send a Protocol Buffer from your iOS client to the server. In the zip file you just downloaded from that Google Code page, you will see three folders: example, message, and parser. You want to create another directory. In this case, we will call it story. In the story folder, you want to have three files: StoryRequest.proto, StoryResponse.proto, and protoc.php

This is the content of StoryRequest.proto; you’ve seen this before.

message StoryRequest {
	required string character1 = 1;
	required string character2 = 2;
	optional string secret_weapon = 3;
}

And this is StoryResponse.proto

message StoryResponse {
	enum ResponseType {
		SUCCESS = 0;
		FAILED = 1;
	}
	required ResponseType status = 1;
	optional string story = 2;
}

And this is the new protoc.php

<?php
// just create from the proto file a pb_prot[NAME].php file
require_once('../parser/pb_parser.php');
 
$test = new PBParser();
$test->parse('./StoryRequest.proto');
$test = new PBParser();
$test->parse('./StoryResponse.proto');
 
var_dump('File parsing done!');
?>

After this, upload the three folders (parser, message, and story) into the same directory on the server using some kind of FTP program. Now, type the full path to protoc.php script (should be in the form of http://someWebSite.com/…/story/protoc.php) into a browser. This triggers the protoc php script to run. It will look at the two .proto files, and compile them into PHP classes. If you refresh the directory in the FTP program, you should see two new files generated, pb_proto_StoryRequest.php and pb_proto_StoryResponse.php. The generated classes should have class definitions that extend PBMessage and have a bunch of getters and setters in them. (If you don’t see the files generated, you might need to refresh again. If you still don’t see them, check to see if you have all the paths set up correctly. If you still don’t see them, check to see if you get write access on your server.)

So now we finally have the PHP classes generated from the Protocol Buffer definitions (.proto files). Next we’ll write a php script to actually take requests from the iOS client.

This entry was posted in Protobuf. Bookmark the permalink.

One Response to Generating PHP Classes from Protocol Buffer

  1. Pingback: Writing a PHP Script that Takes One Protobuf and Returns Another Protobuf | Kelvin Kao Dev

Leave a Reply

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