Skip to content

Commit 5253720

Browse files
committedNov 6, 2017
php example
1 parent 0554705 commit 5253720

9 files changed

+68
-438
lines changed
 

‎examples/php/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ TRY IT!
4141
$ node greeter_server.js
4242
```
4343

44-
- Run the client
44+
- Generate proto files and run the client
4545

4646
```
4747
$ cd examples/php
48+
$ ./greeter_proto_gen.sh
4849
$ ./run_greeter_client.sh
4950
```
5051

‎examples/php/greeter_client.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
*
1818
*/
1919

20+
// php:generate protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto
21+
2022
require dirname(__FILE__).'/vendor/autoload.php';
2123

22-
// The following includes are needed when using protobuf 3.1.0
23-
// and will suppress warnings when using protobuf 3.2.0+
24-
@include_once dirname(__FILE__).'/helloworld.pb.php';
25-
@include_once dirname(__FILE__).'/helloworld_grpc_pb.php';
24+
@include_once dirname(__FILE__).'/Helloworld/GreeterClient.php';
25+
@include_once dirname(__FILE__).'/Helloworld/HelloReply.php';
26+
@include_once dirname(__FILE__).'/Helloworld/HelloRequest.php';
27+
@include_once dirname(__FILE__).'/GPBMetadata/Helloworld.php';
2628

2729
function greet($name)
2830
{

‎examples/php/greeter_proto_gen.sh

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

‎examples/php/helloworld.pb.php

-58
This file was deleted.

‎examples/php/helloworld_grpc_pb.php

-49
This file was deleted.

‎examples/php/route_guide/route_guide.pb.php

-209
This file was deleted.

‎examples/php/route_guide/route_guide_client.php

+27-16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
*
1818
*/
1919

20-
require dirname(__FILE__).'/../vendor/autoload.php';
20+
// php:generate protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto
2121

22-
// The following includes are needed when using protobuf 3.1.0
23-
// and will suppress warnings when using protobuf 3.2.0+
24-
@include_once dirname(__FILE__).'/route_guide.pb.php';
25-
@include_once dirname(__FILE__).'/route_guide_grpc_pb.php';
22+
require dirname(__FILE__).'/../vendor/autoload.php';
2623

2724
define('COORD_FACTOR', 1e7);
2825

@@ -38,9 +35,12 @@ function printFeature($feature)
3835
} else {
3936
$name_str = "feature called $name";
4037
}
41-
echo sprintf("Found %s \n at %f, %f\n", $name_str,
38+
echo sprintf(
39+
"Found %s \n at %f, %f\n",
40+
$name_str,
4241
$feature->getLocation()->getLatitude() / COORD_FACTOR,
43-
$feature->getLocation()->getLongitude() / COORD_FACTOR);
42+
$feature->getLocation()->getLongitude() / COORD_FACTOR
43+
);
4444
}
4545

4646
/**
@@ -122,19 +122,24 @@ function runRecordRoute()
122122
$feature_name = $db[$index]['name'];
123123
$point->setLatitude($lat);
124124
$point->setLongitude($long);
125-
echo sprintf("Visiting point %f, %f,\n with feature name: %s\n",
126-
$lat / COORD_FACTOR, $long / COORD_FACTOR,
127-
$feature_name ? $feature_name : '<empty>');
125+
echo sprintf(
126+
"Visiting point %f, %f,\n with feature name: %s\n",
127+
$lat / COORD_FACTOR,
128+
$long / COORD_FACTOR,
129+
$feature_name ? $feature_name : '<empty>'
130+
);
128131
usleep(rand(300000, 800000));
129132
$call->write($point);
130133
}
131134
list($route_summary, $status) = $call->wait();
132-
echo sprintf("Finished trip with %d points\nPassed %d features\n".
135+
echo sprintf(
136+
"Finished trip with %d points\nPassed %d features\n".
133137
"Travelled %d meters\nIt took %d seconds\n",
134138
$route_summary->getPointCount(),
135139
$route_summary->getFeatureCount(),
136140
$route_summary->getDistance(),
137-
$route_summary->getElapsedTime());
141+
$route_summary->getElapsedTime()
142+
);
138143
}
139144

140145
/**
@@ -166,19 +171,25 @@ function runRouteChat()
166171
$route_note->setLocation($point);
167172
$route_note->setMessage($message = $n[2]);
168173

169-
echo sprintf("Sending message: '%s' at (%d, %d)\n",
170-
$message, $lat, $long);
174+
echo sprintf(
175+
"Sending message: '%s' at (%d, %d)\n",
176+
$message,
177+
$lat,
178+
$long
179+
);
171180
// send a bunch of messages to the server
172181
$call->write($route_note);
173182
}
174183
$call->writesDone();
175184

176185
// read from the server until there's no more
177186
while ($route_note_reply = $call->read()) {
178-
echo sprintf("Previous left message at (%d, %d): '%s'\n",
187+
echo sprintf(
188+
"Previous left message at (%d, %d): '%s'\n",
179189
$route_note_reply->getLocation()->getLatitude(),
180190
$route_note_reply->getLocation()->getLongitude(),
181-
$route_note_reply->getMessage());
191+
$route_note_reply->getMessage()
192+
);
182193
}
183194
}
184195

‎examples/php/route_guide/route_guide_grpc_pb.php

-101
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
# Copyright 2015 gRPC authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto

0 commit comments

Comments
 (0)
Please sign in to comment.