php example

pull/13268/head
ZhouyihaiDing 7 years ago
parent 05547052d0
commit 5253720853
  1. 3
      examples/php/README.md
  2. 10
      examples/php/greeter_client.php
  3. 17
      examples/php/greeter_proto_gen.sh
  4. 58
      examples/php/helloworld.pb.php
  5. 49
      examples/php/helloworld_grpc_pb.php
  6. 209
      examples/php/route_guide/route_guide.pb.php
  7. 43
      examples/php/route_guide/route_guide_client.php
  8. 101
      examples/php/route_guide/route_guide_grpc_pb.php
  9. 16
      examples/php/route_guide/route_guide_proto_gen.sh

@ -41,10 +41,11 @@ TRY IT!
$ node greeter_server.js
```
- Run the client
- Generate proto files and run the client
```
$ cd examples/php
$ ./greeter_proto_gen.sh
$ ./run_greeter_client.sh
```

@ -17,12 +17,14 @@
*
*/
// php:generate protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto
require dirname(__FILE__).'/vendor/autoload.php';
// The following includes are needed when using protobuf 3.1.0
// and will suppress warnings when using protobuf 3.2.0+
@include_once dirname(__FILE__).'/helloworld.pb.php';
@include_once dirname(__FILE__).'/helloworld_grpc_pb.php';
@include_once dirname(__FILE__).'/Helloworld/GreeterClient.php';
@include_once dirname(__FILE__).'/Helloworld/HelloReply.php';
@include_once dirname(__FILE__).'/Helloworld/HelloRequest.php';
@include_once dirname(__FILE__).'/GPBMetadata/Helloworld.php';
function greet($name)
{

@ -0,0 +1,17 @@
#!/bin/bash
# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto

@ -1,58 +0,0 @@
<?php
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: helloworld.proto
namespace Helloworld;
use Google\Protobuf\Internal\DescriptorPool;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;
class HelloRequest extends \Google\Protobuf\Internal\Message
{
private $name = '';
public function getName()
{
return $this->name;
}
public function setName($var)
{
GPBUtil::checkString($var, True);
$this->name = $var;
}
}
class HelloReply extends \Google\Protobuf\Internal\Message
{
private $message = '';
public function getMessage()
{
return $this->message;
}
public function setMessage($var)
{
GPBUtil::checkString($var, True);
$this->message = $var;
}
}
$pool = DescriptorPool::getGeneratedPool();
$pool->internalAddGeneratedFile(hex2bin(
"0ae6010a1068656c6c6f776f726c642e70726f746f120a68656c6c6f776f" .
"726c64221c0a0c48656c6c6f52657175657374120c0a046e616d65180120" .
"012809221d0a0a48656c6c6f5265706c79120f0a076d6573736167651801" .
"2001280932490a0747726565746572123e0a0853617948656c6c6f12182e" .
"68656c6c6f776f726c642e48656c6c6f526571756573741a162e68656c6c" .
"6f776f726c642e48656c6c6f5265706c79220042360a1b696f2e67727063" .
"2e6578616d706c65732e68656c6c6f776f726c64420f48656c6c6f576f72" .
"6c6450726f746f5001a20203484c57620670726f746f33"
));

@ -1,49 +0,0 @@
<?php
// GENERATED CODE -- DO NOT EDIT!
// Original file comments:
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Helloworld {
// The greeting service definition.
class GreeterClient extends \Grpc\BaseStub {
/**
* @param string $hostname hostname
* @param array $opts channel options
* @param Grpc\Channel $channel (optional) re-use channel object
*/
public function __construct($hostname, $opts, $channel = null) {
parent::__construct($hostname, $opts, $channel);
}
/**
* Sends a greeting
* @param \Helloworld\HelloRequest $argument input argument
* @param array $metadata metadata
* @param array $options call options
*/
public function SayHello(\Helloworld\HelloRequest $argument,
$metadata = [], $options = []) {
return $this->_simpleRequest('/helloworld.Greeter/SayHello',
$argument,
['\Helloworld\HelloReply', 'decode'],
$metadata, $options);
}
}
}

@ -1,209 +0,0 @@
<?php
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: route_guide.proto
namespace Routeguide;
use Google\Protobuf\Internal\DescriptorPool;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;
class Point extends \Google\Protobuf\Internal\Message
{
private $latitude = 0;
private $longitude = 0;
public function getLatitude()
{
return $this->latitude;
}
public function setLatitude($var)
{
GPBUtil::checkInt32($var);
$this->latitude = $var;
}
public function getLongitude()
{
return $this->longitude;
}
public function setLongitude($var)
{
GPBUtil::checkInt32($var);
$this->longitude = $var;
}
}
class Rectangle extends \Google\Protobuf\Internal\Message
{
private $lo = null;
private $hi = null;
public function getLo()
{
return $this->lo;
}
public function setLo(&$var)
{
GPBUtil::checkMessage($var, \Routeguide\Point::class);
$this->lo = $var;
}
public function getHi()
{
return $this->hi;
}
public function setHi(&$var)
{
GPBUtil::checkMessage($var, \Routeguide\Point::class);
$this->hi = $var;
}
}
class Feature extends \Google\Protobuf\Internal\Message
{
private $name = '';
private $location = null;
public function getName()
{
return $this->name;
}
public function setName($var)
{
GPBUtil::checkString($var, True);
$this->name = $var;
}
public function getLocation()
{
return $this->location;
}
public function setLocation(&$var)
{
GPBUtil::checkMessage($var, \Routeguide\Point::class);
$this->location = $var;
}
}
class RouteNote extends \Google\Protobuf\Internal\Message
{
private $location = null;
private $message = '';
public function getLocation()
{
return $this->location;
}
public function setLocation(&$var)
{
GPBUtil::checkMessage($var, \Routeguide\Point::class);
$this->location = $var;
}
public function getMessage()
{
return $this->message;
}
public function setMessage($var)
{
GPBUtil::checkString($var, True);
$this->message = $var;
}
}
class RouteSummary extends \Google\Protobuf\Internal\Message
{
private $point_count = 0;
private $feature_count = 0;
private $distance = 0;
private $elapsed_time = 0;
public function getPointCount()
{
return $this->point_count;
}
public function setPointCount($var)
{
GPBUtil::checkInt32($var);
$this->point_count = $var;
}
public function getFeatureCount()
{
return $this->feature_count;
}
public function setFeatureCount($var)
{
GPBUtil::checkInt32($var);
$this->feature_count = $var;
}
public function getDistance()
{
return $this->distance;
}
public function setDistance($var)
{
GPBUtil::checkInt32($var);
$this->distance = $var;
}
public function getElapsedTime()
{
return $this->elapsed_time;
}
public function setElapsedTime($var)
{
GPBUtil::checkInt32($var);
$this->elapsed_time = $var;
}
}
$pool = DescriptorPool::getGeneratedPool();
$pool->internalAddGeneratedFile(hex2bin(
"0ac5050a11726f7574655f67756964652e70726f746f120a726f75746567" .
"75696465222c0a05506f696e7412100a086c617469747564651801200128" .
"0512110a096c6f6e67697475646518022001280522490a0952656374616e" .
"676c65121d0a026c6f18012001280b32112e726f75746567756964652e50" .
"6f696e74121d0a02686918022001280b32112e726f75746567756964652e" .
"506f696e74223c0a0746656174757265120c0a046e616d65180120012809" .
"12230a086c6f636174696f6e18022001280b32112e726f75746567756964" .
"652e506f696e7422410a09526f7574654e6f746512230a086c6f63617469" .
"6f6e18012001280b32112e726f75746567756964652e506f696e74120f0a" .
"076d65737361676518022001280922620a0c526f75746553756d6d617279" .
"12130a0b706f696e745f636f756e7418012001280512150a0d6665617475" .
"72655f636f756e7418022001280512100a0864697374616e636518032001" .
"280512140a0c656c61707365645f74696d651804200128053285020a0a52" .
"6f757465477569646512360a0a4765744665617475726512112e726f7574" .
"6567756964652e506f696e741a132e726f75746567756964652e46656174" .
"7572652200123e0a0c4c697374466561747572657312152e726f75746567" .
"756964652e52656374616e676c651a132e726f75746567756964652e4665" .
"617475726522003001123e0a0b5265636f7264526f75746512112e726f75" .
"746567756964652e506f696e741a182e726f75746567756964652e526f75" .
"746553756d6d61727922002801123f0a09526f7574654368617412152e72" .
"6f75746567756964652e526f7574654e6f74651a152e726f757465677569" .
"64652e526f7574654e6f746522002801300142360a1b696f2e677270632e" .
"6578616d706c65732e726f7574656775696465420f526f75746547756964" .
"6550726f746f5001a20203525447620670726f746f33"
));

@ -17,12 +17,9 @@
*
*/
require dirname(__FILE__).'/../vendor/autoload.php';
// php:generate protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto
// The following includes are needed when using protobuf 3.1.0
// and will suppress warnings when using protobuf 3.2.0+
@include_once dirname(__FILE__).'/route_guide.pb.php';
@include_once dirname(__FILE__).'/route_guide_grpc_pb.php';
require dirname(__FILE__).'/../vendor/autoload.php';
define('COORD_FACTOR', 1e7);
@ -38,9 +35,12 @@ function printFeature($feature)
} else {
$name_str = "feature called $name";
}
echo sprintf("Found %s \n at %f, %f\n", $name_str,
echo sprintf(
"Found %s \n at %f, %f\n",
$name_str,
$feature->getLocation()->getLatitude() / COORD_FACTOR,
$feature->getLocation()->getLongitude() / COORD_FACTOR);
$feature->getLocation()->getLongitude() / COORD_FACTOR
);
}
/**
@ -122,19 +122,24 @@ function runRecordRoute()
$feature_name = $db[$index]['name'];
$point->setLatitude($lat);
$point->setLongitude($long);
echo sprintf("Visiting point %f, %f,\n with feature name: %s\n",
$lat / COORD_FACTOR, $long / COORD_FACTOR,
$feature_name ? $feature_name : '<empty>');
echo sprintf(
"Visiting point %f, %f,\n with feature name: %s\n",
$lat / COORD_FACTOR,
$long / COORD_FACTOR,
$feature_name ? $feature_name : '<empty>'
);
usleep(rand(300000, 800000));
$call->write($point);
}
list($route_summary, $status) = $call->wait();
echo sprintf("Finished trip with %d points\nPassed %d features\n".
echo sprintf(
"Finished trip with %d points\nPassed %d features\n".
"Travelled %d meters\nIt took %d seconds\n",
$route_summary->getPointCount(),
$route_summary->getFeatureCount(),
$route_summary->getDistance(),
$route_summary->getElapsedTime());
$route_summary->getElapsedTime()
);
}
/**
@ -166,8 +171,12 @@ function runRouteChat()
$route_note->setLocation($point);
$route_note->setMessage($message = $n[2]);
echo sprintf("Sending message: '%s' at (%d, %d)\n",
$message, $lat, $long);
echo sprintf(
"Sending message: '%s' at (%d, %d)\n",
$message,
$lat,
$long
);
// send a bunch of messages to the server
$call->write($route_note);
}
@ -175,10 +184,12 @@ function runRouteChat()
// read from the server until there's no more
while ($route_note_reply = $call->read()) {
echo sprintf("Previous left message at (%d, %d): '%s'\n",
echo sprintf(
"Previous left message at (%d, %d): '%s'\n",
$route_note_reply->getLocation()->getLatitude(),
$route_note_reply->getLocation()->getLongitude(),
$route_note_reply->getMessage());
$route_note_reply->getMessage()
);
}
}

@ -1,101 +0,0 @@
<?php
// GENERATED CODE -- DO NOT EDIT!
// Original file comments:
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Routeguide {
// Interface exported by the server.
class RouteGuideClient extends \Grpc\BaseStub {
/**
* @param string $hostname hostname
* @param array $opts channel options
* @param Grpc\Channel $channel (optional) re-use channel object
*/
public function __construct($hostname, $opts, $channel = null) {
parent::__construct($hostname, $opts, $channel);
}
/**
* A simple RPC.
*
* Obtains the feature at a given position.
*
* A feature with an empty name is returned if there's no feature at the given
* position.
* @param \Routeguide\Point $argument input argument
* @param array $metadata metadata
* @param array $options call options
*/
public function GetFeature(\Routeguide\Point $argument,
$metadata = [], $options = []) {
return $this->_simpleRequest('/routeguide.RouteGuide/GetFeature',
$argument,
['\Routeguide\Feature', 'decode'],
$metadata, $options);
}
/**
* A server-to-client streaming RPC.
*
* Obtains the Features available within the given Rectangle. Results are
* streamed rather than returned at once (e.g. in a response message with a
* repeated field), as the rectangle may cover a large area and contain a
* huge number of features.
* @param \Routeguide\Rectangle $argument input argument
* @param array $metadata metadata
* @param array $options call options
*/
public function ListFeatures(\Routeguide\Rectangle $argument,
$metadata = [], $options = []) {
return $this->_serverStreamRequest('/routeguide.RouteGuide/ListFeatures',
$argument,
['\Routeguide\Feature', 'decode'],
$metadata, $options);
}
/**
* A client-to-server streaming RPC.
*
* Accepts a stream of Points on a route being traversed, returning a
* RouteSummary when traversal is completed.
* @param array $metadata metadata
* @param array $options call options
*/
public function RecordRoute($metadata = [], $options = []) {
return $this->_clientStreamRequest('/routeguide.RouteGuide/RecordRoute',
['\Routeguide\RouteSummary','decode'],
$metadata, $options);
}
/**
* A Bidirectional streaming RPC.
*
* Accepts a stream of RouteNotes sent while a route is being traversed,
* while receiving other RouteNotes (e.g. from other users).
* @param array $metadata metadata
* @param array $options call options
*/
public function RouteChat($metadata = [], $options = []) {
return $this->_bidiRequest('/routeguide.RouteGuide/RouteChat',
['\Routeguide\RouteNote','decode'],
$metadata, $options);
}
}
}

@ -0,0 +1,16 @@
#!/bin/bash
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto
Loading…
Cancel
Save