aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/php
diff options
context:
space:
mode:
Diffstat (limited to 'examples/php')
-rw-r--r--examples/php/route_guide/README.md28
-rw-r--r--examples/php/route_guide/route_guide.php142
-rw-r--r--examples/php/route_guide/route_guide.proto2
-rw-r--r--examples/php/route_guide/route_guide_client.php16
4 files changed, 93 insertions, 95 deletions
diff --git a/examples/php/route_guide/README.md b/examples/php/route_guide/README.md
index e5230ae4e4..00610ffb09 100644
--- a/examples/php/route_guide/README.md
+++ b/examples/php/route_guide/README.md
@@ -8,7 +8,7 @@ This tutorial provides a basic PHP programmer's introduction to working with gRP
It assumes a passing familiarity with [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial uses the proto2 version of the protocol buffers language.
-Also note that currently you can only create clients in PHP for gRPC services - you can find out how to create gRPC servers in our other tutorials, e.g. [Node.js](examples/node/route_guide).
+Also note that currently you can only create clients in PHP for gRPC services - you can find out how to create gRPC servers in our other tutorials, e.g. [Node.js](../node/route_guide).
This isn't a comprehensive guide to using gRPC in PHP: more reference documentation is coming soon.
@@ -29,7 +29,7 @@ With gRPC you can define your service once in a .proto file and implement client
<a name="setup"></a>
## Example code and setup
-The example code for our tutorial is in [examples/php/route_guide](examples/php/route_guide). To download the example, clone this repository by running the following command:
+The example code for our tutorial is in [examples/php/route_guide](.). To download the example, clone this repository by running the following command:
```shell
$ git clone https://github.com/grpc/grpc.git
```
@@ -68,7 +68,7 @@ The next sections guide you step-by-step through how this proto service is defin
<a name="proto"></a>
## Defining the service
-First let's look at how the service we're using is defined. A gRPC *service* and its method *request* and *response* types using [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file for our example in [`examples/protos/route_guide.proto`](examples/protos/route_guide.proto).
+First let's look at how the service we're using is defined. A gRPC *service* and its method *request* and *response* types using [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file for our example in [`route_guide.proto`](route_guide.proto).
To define a service, you specify a named `service` in your .proto file:
@@ -153,20 +153,20 @@ require dirname(__FILE__) . '/route_guide.php';
The file contains:
- All the protocol buffer code to populate, serialize, and retrieve our request and response message types.
-- A class called `examples\RouteGuideClient` that lets clients call the methods defined in the `RouteGuide` service.
+- A class called `routeguide\RouteGuideClient` that lets clients call the methods defined in the `RouteGuide` service.
<a name="client"></a>
## Creating the client
-In this section, we'll look at creating a PHP client for our `RouteGuide` service. You can see our complete example client code in [examples/php/route_guide/route_guide_client.php](examples/php/route_guide/route_guide_client.php).
+In this section, we'll look at creating a PHP client for our `RouteGuide` service. You can see our complete example client code in [route_guide_client.php](route_guide_client.php).
### Constructing a client object
To call service methods, we first need to create a client object, an instance of the generated `RouteGuideClient` class. The constructor of the class expects the server address and port we want to connect to:
```php
-$client = new examples\RouteGuideClient(new Grpc\BaseStub('localhost:50051', []));
+$client = new routeguide\RouteGuideClient(new Grpc\BaseStub('localhost:50051', []));
```
### Calling service methods
@@ -178,13 +178,13 @@ Now let's look at how we call our service methods.
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local asynchronous method.
```php
- $point = new examples\Point();
+ $point = new routeguide\Point();
$point->setLatitude(409146138);
$point->setLongitude(-746188906);
list($feature, $status) = $client->GetFeature($point)->wait();
```
-As you can see, we create and populate a request object, i.e. an `examples\Point` object. Then, we call the method on the stub, passing it the request object. If there is no error, then we can read the response information from the server from our response object, i.e. an `examples\Feature` object.
+As you can see, we create and populate a request object, i.e. an `routeguide\Point` object. Then, we call the method on the stub, passing it the request object. If there is no error, then we can read the response information from the server from our response object, i.e. an `routeguide\Feature` object.
```php
print sprintf("Found %s \n at %f, %f\n", $feature->getName(),
@@ -197,15 +197,15 @@ As you can see, we create and populate a request object, i.e. an `examples\Point
Now let's look at our streaming methods. Here's where we call the server-side streaming method `ListFeatures`, which returns a stream of geographical `Feature`s:
```php
- $lo_point = new examples\Point();
- $hi_point = new examples\Point();
+ $lo_point = new routeguide\Point();
+ $hi_point = new routeguide\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
- $rectangle = new examples\Rectangle();
+ $rectangle = new routeguide\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
@@ -219,12 +219,12 @@ Now let's look at our streaming methods. Here's where we call the server-side st
The `$call->responses()` method call returns an iterator. When the server sends a response, a `$feature` object will be returned in the `foreach` loop, until the server indiciates that there will be no more responses to be sent.
-The client-side streaming method `RecordRoute` is similar, except there we pass the method an iterator and get back a `examples\RouteSummary`.
+The client-side streaming method `RecordRoute` is similar, except there we pass the method an iterator and get back a `routeguide\RouteSummary`.
```php
$points_iter = function($db) {
for ($i = 0; $i < $num_points; $i++) {
- $point = new examples\Point();
+ $point = new routeguide\Point();
$point->setLatitude($lat);
$point->setLongitude($long);
yield $point;
@@ -245,7 +245,7 @@ To write messages from the client:
```php
foreach ($notes as $n) {
- $route_note = new examples\RouteNote();
+ $route_note = new routerguide\RouteNote();
$call->write($route_note);
}
$call->writesDone();
diff --git a/examples/php/route_guide/route_guide.php b/examples/php/route_guide/route_guide.php
index a836e03b55..76086f9772 100644
--- a/examples/php/route_guide/route_guide.php
+++ b/examples/php/route_guide/route_guide.php
@@ -1,9 +1,9 @@
<?php
// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0
// Source: route_guide.proto
-// Date: 2015-06-12 00:32:41
+// Date: 2015-08-31 21:11:45
-namespace examples {
+namespace routeguide {
class Point extends \DrSlump\Protobuf\Message {
@@ -19,7 +19,7 @@ namespace examples {
public static function descriptor()
{
- $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.Point');
+ $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'routeguide.Point');
// OPTIONAL INT32 latitude = 1
$f = new \DrSlump\Protobuf\Field();
@@ -58,7 +58,7 @@ namespace examples {
/**
* Clear <latitude> value
*
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function clearLatitude(){
return $this->_clear(1);
@@ -77,7 +77,7 @@ namespace examples {
* Set <latitude> value
*
* @param int $value
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function setLatitude( $value){
return $this->_set(1, $value);
@@ -95,7 +95,7 @@ namespace examples {
/**
* Clear <longitude> value
*
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function clearLongitude(){
return $this->_clear(2);
@@ -114,7 +114,7 @@ namespace examples {
* Set <longitude> value
*
* @param int $value
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function setLongitude( $value){
return $this->_set(2, $value);
@@ -122,14 +122,14 @@ namespace examples {
}
}
-namespace examples {
+namespace routeguide {
class Rectangle extends \DrSlump\Protobuf\Message {
- /** @var \examples\Point */
+ /** @var \routeguide\Point */
public $lo = null;
- /** @var \examples\Point */
+ /** @var \routeguide\Point */
public $hi = null;
@@ -138,7 +138,7 @@ namespace examples {
public static function descriptor()
{
- $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.Rectangle');
+ $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'routeguide.Rectangle');
// OPTIONAL MESSAGE lo = 1
$f = new \DrSlump\Protobuf\Field();
@@ -146,7 +146,7 @@ namespace examples {
$f->name = "lo";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
- $f->reference = '\examples\Point';
+ $f->reference = '\routeguide\Point';
$descriptor->addField($f);
// OPTIONAL MESSAGE hi = 2
@@ -155,7 +155,7 @@ namespace examples {
$f->name = "hi";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
- $f->reference = '\examples\Point';
+ $f->reference = '\routeguide\Point';
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
@@ -177,7 +177,7 @@ namespace examples {
/**
* Clear <lo> value
*
- * @return \examples\Rectangle
+ * @return \routeguide\Rectangle
*/
public function clearLo(){
return $this->_clear(1);
@@ -186,7 +186,7 @@ namespace examples {
/**
* Get <lo> value
*
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function getLo(){
return $this->_get(1);
@@ -195,10 +195,10 @@ namespace examples {
/**
* Set <lo> value
*
- * @param \examples\Point $value
- * @return \examples\Rectangle
+ * @param \routeguide\Point $value
+ * @return \routeguide\Rectangle
*/
- public function setLo(\examples\Point $value){
+ public function setLo(\routeguide\Point $value){
return $this->_set(1, $value);
}
@@ -214,7 +214,7 @@ namespace examples {
/**
* Clear <hi> value
*
- * @return \examples\Rectangle
+ * @return \routeguide\Rectangle
*/
public function clearHi(){
return $this->_clear(2);
@@ -223,7 +223,7 @@ namespace examples {
/**
* Get <hi> value
*
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function getHi(){
return $this->_get(2);
@@ -232,23 +232,23 @@ namespace examples {
/**
* Set <hi> value
*
- * @param \examples\Point $value
- * @return \examples\Rectangle
+ * @param \routeguide\Point $value
+ * @return \routeguide\Rectangle
*/
- public function setHi(\examples\Point $value){
+ public function setHi(\routeguide\Point $value){
return $this->_set(2, $value);
}
}
}
-namespace examples {
+namespace routeguide {
class Feature extends \DrSlump\Protobuf\Message {
/** @var string */
public $name = null;
- /** @var \examples\Point */
+ /** @var \routeguide\Point */
public $location = null;
@@ -257,7 +257,7 @@ namespace examples {
public static function descriptor()
{
- $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.Feature');
+ $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'routeguide.Feature');
// OPTIONAL STRING name = 1
$f = new \DrSlump\Protobuf\Field();
@@ -273,7 +273,7 @@ namespace examples {
$f->name = "location";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
- $f->reference = '\examples\Point';
+ $f->reference = '\routeguide\Point';
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
@@ -295,7 +295,7 @@ namespace examples {
/**
* Clear <name> value
*
- * @return \examples\Feature
+ * @return \routeguide\Feature
*/
public function clearName(){
return $this->_clear(1);
@@ -314,7 +314,7 @@ namespace examples {
* Set <name> value
*
* @param string $value
- * @return \examples\Feature
+ * @return \routeguide\Feature
*/
public function setName( $value){
return $this->_set(1, $value);
@@ -332,7 +332,7 @@ namespace examples {
/**
* Clear <location> value
*
- * @return \examples\Feature
+ * @return \routeguide\Feature
*/
public function clearLocation(){
return $this->_clear(2);
@@ -341,7 +341,7 @@ namespace examples {
/**
* Get <location> value
*
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function getLocation(){
return $this->_get(2);
@@ -350,20 +350,20 @@ namespace examples {
/**
* Set <location> value
*
- * @param \examples\Point $value
- * @return \examples\Feature
+ * @param \routeguide\Point $value
+ * @return \routeguide\Feature
*/
- public function setLocation(\examples\Point $value){
+ public function setLocation(\routeguide\Point $value){
return $this->_set(2, $value);
}
}
}
-namespace examples {
+namespace routeguide {
class RouteNote extends \DrSlump\Protobuf\Message {
- /** @var \examples\Point */
+ /** @var \routeguide\Point */
public $location = null;
/** @var string */
@@ -375,7 +375,7 @@ namespace examples {
public static function descriptor()
{
- $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.RouteNote');
+ $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'routeguide.RouteNote');
// OPTIONAL MESSAGE location = 1
$f = new \DrSlump\Protobuf\Field();
@@ -383,7 +383,7 @@ namespace examples {
$f->name = "location";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
- $f->reference = '\examples\Point';
+ $f->reference = '\routeguide\Point';
$descriptor->addField($f);
// OPTIONAL STRING message = 2
@@ -413,7 +413,7 @@ namespace examples {
/**
* Clear <location> value
*
- * @return \examples\RouteNote
+ * @return \routeguide\RouteNote
*/
public function clearLocation(){
return $this->_clear(1);
@@ -422,7 +422,7 @@ namespace examples {
/**
* Get <location> value
*
- * @return \examples\Point
+ * @return \routeguide\Point
*/
public function getLocation(){
return $this->_get(1);
@@ -431,10 +431,10 @@ namespace examples {
/**
* Set <location> value
*
- * @param \examples\Point $value
- * @return \examples\RouteNote
+ * @param \routeguide\Point $value
+ * @return \routeguide\RouteNote
*/
- public function setLocation(\examples\Point $value){
+ public function setLocation(\routeguide\Point $value){
return $this->_set(1, $value);
}
@@ -450,7 +450,7 @@ namespace examples {
/**
* Clear <message> value
*
- * @return \examples\RouteNote
+ * @return \routeguide\RouteNote
*/
public function clearMessage(){
return $this->_clear(2);
@@ -469,7 +469,7 @@ namespace examples {
* Set <message> value
*
* @param string $value
- * @return \examples\RouteNote
+ * @return \routeguide\RouteNote
*/
public function setMessage( $value){
return $this->_set(2, $value);
@@ -477,7 +477,7 @@ namespace examples {
}
}
-namespace examples {
+namespace routeguide {
class RouteSummary extends \DrSlump\Protobuf\Message {
@@ -499,7 +499,7 @@ namespace examples {
public static function descriptor()
{
- $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.RouteSummary');
+ $descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'routeguide.RouteSummary');
// OPTIONAL INT32 point_count = 1
$f = new \DrSlump\Protobuf\Field();
@@ -556,7 +556,7 @@ namespace examples {
/**
* Clear <point_count> value
*
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function clearPointCount(){
return $this->_clear(1);
@@ -575,7 +575,7 @@ namespace examples {
* Set <point_count> value
*
* @param int $value
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function setPointCount( $value){
return $this->_set(1, $value);
@@ -593,7 +593,7 @@ namespace examples {
/**
* Clear <feature_count> value
*
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function clearFeatureCount(){
return $this->_clear(2);
@@ -612,7 +612,7 @@ namespace examples {
* Set <feature_count> value
*
* @param int $value
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function setFeatureCount( $value){
return $this->_set(2, $value);
@@ -630,7 +630,7 @@ namespace examples {
/**
* Clear <distance> value
*
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function clearDistance(){
return $this->_clear(3);
@@ -649,7 +649,7 @@ namespace examples {
* Set <distance> value
*
* @param int $value
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function setDistance( $value){
return $this->_set(3, $value);
@@ -667,7 +667,7 @@ namespace examples {
/**
* Clear <elapsed_time> value
*
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function clearElapsedTime(){
return $this->_clear(4);
@@ -686,7 +686,7 @@ namespace examples {
* Set <elapsed_time> value
*
* @param int $value
- * @return \examples\RouteSummary
+ * @return \routeguide\RouteSummary
*/
public function setElapsedTime( $value){
return $this->_set(4, $value);
@@ -694,38 +694,36 @@ namespace examples {
}
}
-namespace examples {
+namespace routeguide {
- class RouteGuideClient{
+ class RouteGuideClient extends \Grpc\BaseStub {
- private $rpc_impl;
-
- public function __construct($rpc_impl) {
- $this->rpc_impl = $rpc_impl;
+ public function __construct($hostname, $opts) {
+ parent::__construct($hostname, $opts);
}
/**
- * @param examples\Point $input
+ * @param routeguide\Point $input
*/
- public function GetFeature(\examples\Point $argument, $metadata = array()) {
- return $this->rpc_impl->_simpleRequest('/examples.RouteGuide/GetFeature', $argument, '\examples\Feature::deserialize', $metadata);
+ public function GetFeature(\routeguide\Point $argument, $metadata = array(), $options = array()) {
+ return $this->_simpleRequest('/routeguide.RouteGuide/GetFeature', $argument, '\routeguide\Feature::deserialize', $metadata, $options);
}
/**
- * @param examples\Rectangle $input
+ * @param routeguide\Rectangle $input
*/
- public function ListFeatures($argument, $metadata = array()) {
- return $this->rpc_impl->_serverStreamRequest('/examples.RouteGuide/ListFeatures', $argument, '\examples\Feature::deserialize', $metadata);
+ public function ListFeatures($argument, $metadata = array(), $options = array()) {
+ return $this->_serverStreamRequest('/routeguide.RouteGuide/ListFeatures', $argument, '\routeguide\Feature::deserialize', $metadata, $options);
}
/**
- * @param examples\Point $input
+ * @param routeguide\Point $input
*/
- public function RecordRoute($arguments, $metadata = array()) {
- return $this->rpc_impl->_clientStreamRequest('/examples.RouteGuide/RecordRoute', $arguments, '\examples\RouteSummary::deserialize', $metadata);
+ public function RecordRoute($metadata = array()) {
+ return $this->_clientStreamRequest('/routeguide.RouteGuide/RecordRoute', '\routeguide\RouteSummary::deserialize', $metadata);
}
/**
- * @param examples\RouteNote $input
+ * @param routeguide\RouteNote $input
*/
public function RouteChat($metadata = array()) {
- return $this->rpc_impl->_bidiRequest('/examples.RouteGuide/RouteChat', '\examples\RouteNote::deserialize', $metadata);
+ return $this->_bidiRequest('/routeguide.RouteGuide/RouteChat', '\routeguide\RouteNote::deserialize', $metadata);
}
}
}
diff --git a/examples/php/route_guide/route_guide.proto b/examples/php/route_guide/route_guide.proto
index 0947184dbb..d50f8a5169 100644
--- a/examples/php/route_guide/route_guide.proto
+++ b/examples/php/route_guide/route_guide.proto
@@ -31,7 +31,7 @@ syntax = "proto2";
option java_package = "io.grpc.examples";
-package examples;
+package routeguide;
// Interface exported by the server.
service RouteGuide {
diff --git a/examples/php/route_guide/route_guide_client.php b/examples/php/route_guide/route_guide_client.php
index 6d9ae58b66..d21a080995 100644
--- a/examples/php/route_guide/route_guide_client.php
+++ b/examples/php/route_guide/route_guide_client.php
@@ -37,7 +37,7 @@ require dirname(__FILE__) . '/route_guide.php';
define('COORD_FACTOR', 1e7);
-$client = new examples\RouteGuideClient(
+$client = new routeguide\RouteGuideClient(
new Grpc\BaseStub('localhost:50051', []));
function printFeature($feature) {
@@ -60,7 +60,7 @@ function runGetFeature() {
print "Running GetFeature...\n";
global $client;
- $point = new examples\Point();
+ $point = new routeguide\Point();
$points = array(
array(409146138, -746188906),
array(0, 0),
@@ -84,15 +84,15 @@ function runListFeatures() {
print "Running ListFeatures...\n";
global $client;
- $lo_point = new examples\Point();
- $hi_point = new examples\Point();
+ $lo_point = new routeguide\Point();
+ $hi_point = new routeguide\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
- $rectangle = new examples\Rectangle();
+ $rectangle = new routeguide\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
@@ -118,7 +118,7 @@ function runRecordRoute() {
$num_points_in_db = count($db);
$num_points = 10;
for ($i = 0; $i < $num_points; $i++) {
- $point = new examples\Point();
+ $point = new routeguide\Point();
$index = rand(0, $num_points_in_db - 1);
$lat = $db[$index]['location']['latitude'];
$long = $db[$index]['location']['longitude'];
@@ -163,11 +163,11 @@ function runRouteChat() {
);
foreach ($notes as $n) {
- $point = new examples\Point();
+ $point = new routeguide\Point();
$point->setLatitude($lat = $n[0]);
$point->setLongitude($long = $n[1]);
- $route_note = new examples\RouteNote();
+ $route_note = new routeguide\RouteNote();
$route_note->setLocation($point);
$route_note->setMessage($message = $n[2]);