PHP Hello World example first draft
This commit is contained in:
commit
80d76f1076
|
|
@ -0,0 +1,2 @@
|
|||
composer.lock
|
||||
vendor/
|
||||
|
|
@ -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
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
/*
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
require 'DrSlump/Protobuf.php';
|
||||
\DrSlump\Protobuf::autoload();
|
||||
require 'vendor/autoload.php';
|
||||
require 'helloworld.php';
|
||||
|
||||
function greet($name) {
|
||||
$client = new helloworld\GreeterClient(
|
||||
new Grpc\BaseStub('localhost:50051', []));
|
||||
$request = new helloworld\HelloRequest();
|
||||
$request->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");
|
||||
|
|
@ -0,0 +1,160 @@
|
|||
<?php
|
||||
// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0
|
||||
// Source: helloworld.proto
|
||||
// Date: 2015-05-29 21:39:19
|
||||
|
||||
namespace helloworld {
|
||||
|
||||
class HelloRequest extends \DrSlump\Protobuf\Message {
|
||||
|
||||
/** @var string */
|
||||
public $name = null;
|
||||
|
||||
|
||||
/** @var \Closure[] */
|
||||
protected static $__extensions = array();
|
||||
|
||||
public static function descriptor()
|
||||
{
|
||||
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'helloworld.HelloRequest');
|
||||
|
||||
// OPTIONAL STRING name = 1
|
||||
$f = new \DrSlump\Protobuf\Field();
|
||||
$f->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 <name> has a value
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasName(){
|
||||
return $this->_has(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear <name> value
|
||||
*
|
||||
* @return \helloworld\HelloRequest
|
||||
*/
|
||||
public function clearName(){
|
||||
return $this->_clear(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get <name> value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(){
|
||||
return $this->_get(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set <name> 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 <message> has a value
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasMessage(){
|
||||
return $this->_has(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear <message> value
|
||||
*
|
||||
* @return \helloworld\HelloReply
|
||||
*/
|
||||
public function clearMessage(){
|
||||
return $this->_clear(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get <message> value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage(){
|
||||
return $this->_get(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set <message> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
Loading…
Reference in New Issue