diff --git a/php/.gitignore b/php/.gitignore new file mode 100644 index 00000000000..d8a7996ab38 --- /dev/null +++ b/php/.gitignore @@ -0,0 +1,2 @@ +composer.lock +vendor/ diff --git a/php/README.md b/php/README.md new file mode 100644 index 00000000000..4c2e6f11d06 --- /dev/null +++ b/php/README.md @@ -0,0 +1,84 @@ +gRPC in 3 minutes (PHP) +=========================== + +PREREQUISITES +------------- + +This requires PHP 5.5 or greater. + +INSTALL +------- + + - Clone this repository + + ```sh + $ git clone https://github.com/grpc/grpc-common.git + ``` + + - Install Protobuf-PHP + + ``` + $ git clone https://github.com/murgatroid99/Protobuf-PHP.git + $ cd Protobuf-PHP + $ rake pear:package version=1.0 + $ pear install Protobuf-1.0.tgz + ``` + + - Install composer + + ``` + $ cd grpc-common/php + $ curl -sS https://getcomposer.org/installer | php + ``` + + - (Coming soon) Download the gRPC PECL extension + + ``` + Coming soon + ``` + + - (Temporary workaround) Compile gRPC extension from source + + ``` + $ git clone https://github.com/grpc/grpc.git + $ cd grpc + $ git checkout --track origin/release-0_9 + $ git pull --recurse-submodules && git submodule update --init --recursive + $ cd third_party/protobuf + $ ./autogen.sh && ./configure --prefix=/usr && make && make install + $ cd ../.. + $ make && make install + $ cd src/php/ext/grpc + $ phpize && ./configure && make && make install + ``` + + +TRY IT! +------- + + - Run the server + + Please follow the instruction in [Node](https://github.com/grpc/grpc-common/tree/master/node) to run the server + ``` + $ cd grpc-common/node + $ nodejs greeter_server.js + ``` + + - Run the client + + ``` + $ cd grpc-common/php + $ php composer.phar install + $ php -d extension=grpc.so greeter_client.php + ``` + +NOTE +---- + +This directory has a copy of `helloworld.proto` because it currently depends on +some Protocol Buffer 2.0 syntax. There is no proto3 support for PHP yet. + +TUTORIAL +-------- + +Coming soon diff --git a/php/composer.json b/php/composer.json new file mode 100644 index 00000000000..2deca5f0a1b --- /dev/null +++ b/php/composer.json @@ -0,0 +1,9 @@ +{ + "name": "grpc/grpc-demo", + "description": "gRPC example for PHP", + "minimum-stability": "dev", + "require": { + "php": ">=5.5.0", + "grpc/grpc": "dev-master" + } +} diff --git a/php/greeter_client.php b/php/greeter_client.php new file mode 100644 index 00000000000..8eeceb38504 --- /dev/null +++ b/php/greeter_client.php @@ -0,0 +1,51 @@ +setName($name); + list($reply, $status) = $client->SayHello($request)->wait(); + $message = $reply->getMessage(); + return $message; +} + +$name = !empty($argv[1]) ? $argv[1] : 'world'; +print(greet($name)."\n"); diff --git a/php/helloworld.php b/php/helloworld.php new file mode 100644 index 00000000000..22da3d3970f --- /dev/null +++ b/php/helloworld.php @@ -0,0 +1,160 @@ +number = 1; + $f->name = "name"; + $f->type = \DrSlump\Protobuf::TYPE_STRING; + $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; + $descriptor->addField($f); + + foreach (self::$__extensions as $cb) { + $descriptor->addField($cb(), true); + } + + return $descriptor; + } + + /** + * Check if has a value + * + * @return boolean + */ + public function hasName(){ + return $this->_has(1); + } + + /** + * Clear value + * + * @return \helloworld\HelloRequest + */ + public function clearName(){ + return $this->_clear(1); + } + + /** + * Get value + * + * @return string + */ + public function getName(){ + return $this->_get(1); + } + + /** + * Set value + * + * @param string $value + * @return \helloworld\HelloRequest + */ + public function setName( $value){ + return $this->_set(1, $value); + } + } +} + +namespace helloworld { + + class HelloReply extends \DrSlump\Protobuf\Message { + + /** @var string */ + public $message = null; + + + /** @var \Closure[] */ + protected static $__extensions = array(); + + public static function descriptor() + { + $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'helloworld.HelloReply'); + + // OPTIONAL STRING message = 1 + $f = new \DrSlump\Protobuf\Field(); + $f->number = 1; + $f->name = "message"; + $f->type = \DrSlump\Protobuf::TYPE_STRING; + $f->rule = \DrSlump\Protobuf::RULE_OPTIONAL; + $descriptor->addField($f); + + foreach (self::$__extensions as $cb) { + $descriptor->addField($cb(), true); + } + + return $descriptor; + } + + /** + * Check if has a value + * + * @return boolean + */ + public function hasMessage(){ + return $this->_has(1); + } + + /** + * Clear value + * + * @return \helloworld\HelloReply + */ + public function clearMessage(){ + return $this->_clear(1); + } + + /** + * Get value + * + * @return string + */ + public function getMessage(){ + return $this->_get(1); + } + + /** + * Set value + * + * @param string $value + * @return \helloworld\HelloReply + */ + public function setMessage( $value){ + return $this->_set(1, $value); + } + } +} + +namespace helloworld { + + class GreeterClient{ + + private $rpc_impl; + + public function __construct($rpc_impl) { + $this->rpc_impl = $rpc_impl; + } + /** + * @param helloworld\HelloRequest $input + */ + public function SayHello(\helloworld\HelloRequest $argument, $metadata = array()) { + return $this->rpc_impl->_simpleRequest('/helloworld.Greeter/SayHello', $argument, '\helloworld\HelloReply::deserialize', $metadata); + } + } +} diff --git a/php/helloworld.proto b/php/helloworld.proto new file mode 100644 index 00000000000..ad8f7a15249 --- /dev/null +++ b/php/helloworld.proto @@ -0,0 +1,50 @@ +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto2"; + +option java_package = "ex.grpc"; + +package helloworld; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply) {} +} + +// The request message containing the user's name. +message HelloRequest { + optional string name = 1; +} + +// The response message containing the greetings +message HelloReply { + optional string message = 1; +}