grpc.io/public/docs/quickstart/php/index.html

550 lines
29 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/css/style.css">
<title>
PHP Quickstart &ndash; gRPC
</title>
<link rel="apple-touch-icon" href="/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" type="image/png" href="/favicons/android-chrome-192x192.png" sizes="192x192" >
<link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicons/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/favicons/manifest.json">
<link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#2DA6B0">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/favicons/mstile-150x150.png">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-60127042-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-60127042-1');
</script>
</head>
<body>
<div id="landing-content">
<div class="row">
<div class="topbannersub">
<nav class="navbar navbar-expand-md navbar-dark topnav">
<a class="navbar-brand" href="https://cjyabraham.github.io/">
<img src="https://cjyabraham.github.io/img/grpc-logo.png" width="114" height="50">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="topnav, collapse navbar-collapse" id="navbarSupportedContent" style="float:right !important">
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a class="nav-link" href="https://cjyabraham.github.io/about/">About</a>
</li>
<li class="nav-item dropdown active">
<a class="nav-link dropdown-toggle" href="https://cjyabraham.github.io/docs/" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Docs
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/docs">
Overview
</a>
<a class="dropdown-item" href="/docs/quickstart/">
Quick Start
</a>
<a class="dropdown-item" href="/docs/guides/">
Guides
</a>
<a class="dropdown-item" href="/docs/tutorials/">
Tutorials
</a>
<a class="dropdown-item" href="/docs/reference/">
Reference
</a>
<a class="dropdown-item" href="/docs/samples/">
Samples
</a>
<a class="dropdown-item" href="/docs/talks">
Presentations
</a>
</div>
</li>
<li class="nav-item ">
<a class="nav-link" href="/blog">
Blog
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="/community">Community</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://packages.grpc.io/">
Packages
</a>
</li>
<li class="nav-item ">
<a class="nav-link" href="https://cjyabraham.github.io/faq/">FAQ</a>
</li>
</ul>
</div>
</nav>
<div class="headertext">Documentation</div>
</div>
</div>
</div>
<div class="subnav d-none d-md-block">
<a href="https://cjyabraham.github.io/docs/" >Overview</a>
| <a href="https://cjyabraham.github.io/docs/quickstart/" class="active">Quick Start</a>
| <a href="https://cjyabraham.github.io/docs/guides/" >Guides</a>
| <a href="https://cjyabraham.github.io/docs/tutorials/" >Tutorials</a>
| <a href="https://cjyabraham.github.io/docs/reference/" >Reference</a>
| <a href="https://cjyabraham.github.io/docs/samples/" >Samples</a>
| <a href="https://cjyabraham.github.io/docs/talks/" >Presentations</a>
</div>
<div class="quickstartcols">
<div class="quickstartcol1">
<h8>Quick Start</h8>
<a href="https://cjyabraham.github.io/docs/quickstart/cpp/" >C++</a>
<a href="https://cjyabraham.github.io/docs/quickstart/csharp/" >C#</a>
<a href="https://cjyabraham.github.io/docs/quickstart/dart/" >Dart</a>
<a href="https://cjyabraham.github.io/docs/quickstart/go/" >Go</a>
<a href="https://cjyabraham.github.io/docs/quickstart/java/" >Java</a>
<a href="https://cjyabraham.github.io/docs/quickstart/android/" >Android Java</a>
<a href="https://cjyabraham.github.io/docs/quickstart/node/" >Node.js</a>
<a href="https://cjyabraham.github.io/docs/quickstart/objective-c/" >Objective-C</a>
<a href="https://cjyabraham.github.io/docs/quickstart/php/" class="active">PHP</a>
<a href="https://cjyabraham.github.io/docs/quickstart/python/" >Python</a>
<a href="https://cjyabraham.github.io/docs/quickstart/ruby/" >Ruby</a>
<a href="https://cjyabraham.github.io/docs/quickstart/web/" >Web</a>
</div>
<div class="quickstartcol2" style="margin-top:4%">
<h3 style="margin-top:0px;">PHP Quickstart</h3>
<p class="lead">This guide gets you started with gRPC in PHP with a simple
working example.</p>
<div id="toc"></div>
<h3 id="prerequisites">Prerequisites</h3>
<ul>
<li><code>php</code> 5.5 or above, 7.0 or above</li>
<li><code>pecl</code></li>
<li><code>composer</code></li>
<li><code>phpunit</code> (optional)</li>
</ul>
<p><strong>Install PHP and PECL on Ubuntu/Debian:</strong></p>
<p>For PHP5:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ sudo apt-get install php5 php5-dev php-pear phpunit</code></pre></div>
<p>For PHP7:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ sudo apt-get install php7.0 php7.0-dev php-pear phpunit</code></pre></div>
<p>or</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ sudo apt-get install php php-dev php-pear phpunit</code></pre></div>
<p><strong>Install PHP and PECL on CentOS/RHEL 7:</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
$ sudo yum install php56w php56w-devel php-pear phpunit gcc zlib-devel</code></pre></div>
<p><strong>Install PHP and PECL on Mac:</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ brew install homebrew/php/php56-grpc
$ curl -O http://pear.php.net/go-pear.phar
$ sudo php -d detect_unicode<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span> go-pear.phar</code></pre></div>
<p><strong>Install Composer (Linux or Mac):</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer</code></pre></div>
<p><strong>Install PHPUnit (Linux or Mac):</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ wget https://phar.phpunit.de/phpunit-old.phar
$ chmod +x phpunit-old.phar
$ sudo mv phpunit-old.phar /usr/bin/phpunit</code></pre></div>
<h3 id="install-the-grpc-php-extension">Install the gRPC PHP extension</h3>
<p>There are two ways to install gRPC PHP extension.
* <code>pecl</code>
* <code>build from source</code></p>
<h4 id="using-pecl">Using PECL</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">sudo pecl install grpc</code></pre></div>
<p>or specific version</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">sudo pecl install grpc-1.7.0</code></pre></div>
<p>Note: for users on CentOS/RHEL 6, unfortunately this step wont work.
Please follow the instructions below to compile the PECL extension from source.</p>
<h5 id="install-on-windows">Install on Windows</h5>
<p>You can download the pre-compiled gRPC extension from the PECL
<a href="https://pecl.php.net/package/grpc">website</a></p>
<h4 id="build-from-source-with-grpc-c-core-library">Build from Source with gRPC C core library</h4>
<p>Clone this repository</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ git clone -b <span style="color:#66d9ef">$(</span>curl -L https://grpc.io/release<span style="color:#66d9ef">)</span> https://github.com/grpc/grpc</code></pre></div>
<h5 id="build-and-install-the-grpc-c-core-library">Build and install the gRPC C core library</h5>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ cd grpc
$ git submodule update --init
$ make
$ sudo make install</code></pre></div>
<h5 id="build-and-install-grpc-php-extension">Build and install gRPC PHP extension</h5>
<p>Compile the gRPC PHP extension</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ cd grpc/src/php/ext/grpc
$ phpize
$ ./configure
$ make
$ sudo make install</code></pre></div>
<p>This will compile and install the gRPC PHP extension into the
standard PHP extension directory. You should be able to run
the <a href="#unit-tests">unit tests</a>, with the PHP extension installed.</p>
<h4 id="update-php-ini">Update php.ini</h4>
<p>After installing the gRPC extension, make sure you add this line
to your <code>php.ini</code> file, (e.g. <code>/etc/php5/cli/php.ini</code>,
<code>/etc/php5/apache2/php.ini</code>, or <code>/usr/local/etc/php/5.6/php.ini</code>),
depending on where your PHP installation is.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">extension<span style="color:#f92672">=</span>grpc.so</code></pre></div>
<p><strong>Add the gRPC PHP library as a Composer dependency</strong></p>
<p>You need to add this to your project&rsquo;s <code>composer.json</code> file.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-json" data-lang="json"> <span style="color:#e6db74">&#34;require&#34;</span><span style="color:#960050;background-color:#1e0010">:</span> {
<span style="color:#f92672">&#34;grpc/grpc&#34;</span>: <span style="color:#e6db74">&#34;v1.7.0&#34;</span>
}</code></pre></div>
<p>To run tests with generated stub code from <code>.proto</code> files, you will also
need the <code>composer</code> and <code>protoc</code> binaries. You can find out how to get these below.</p>
<h3 id="install-other-prerequisites-for-both-mac-os-x-and-linux">Install other prerequisites for both Mac OS X and Linux</h3>
<ul>
<li><code>protoc: protobuf compiler</code></li>
<li><code>protobuf.so: protobuf runtime library</code></li>
<li><code>grpc_php_plugin: Generates PHP gRPC service interface out of Protobuf IDL</code></li>
</ul>
<h4 id="install-protobuf-compiler">Install Protobuf compiler</h4>
<p>If you don&rsquo;t have it already, you need to install the protobuf compiler
<code>protoc</code>, version 3.4.0+ (the newer the better) for the current gRPC version.
If you installed already, make sure the protobuf version is compatible with the
grpc version you installed. If you build grpc.so from source, you can check
the version of grpc inside package.xml file.</p>
<p>The compatibility between the grpc and protobuf version is listed as table below:</p>
<table>
<thead>
<tr>
<th>grpc</th>
<th>protobuf</th>
</tr>
</thead>
<tbody>
<tr>
<td>v1.0.0</td>
<td>3.0.0(GA)</td>
</tr>
<tr>
<td>v1.0.1</td>
<td>3.0.2</td>
</tr>
<tr>
<td>v1.1.0</td>
<td>3.1.0</td>
</tr>
<tr>
<td>v1.2.0</td>
<td>3.2.0</td>
</tr>
<tr>
<td>v1.2.0</td>
<td>3.2.0</td>
</tr>
<tr>
<td>v1.3.4</td>
<td>3.3.0</td>
</tr>
<tr>
<td>v1.3.5</td>
<td>3.2.0</td>
</tr>
<tr>
<td>v1.4.0</td>
<td>3.3.0</td>
</tr>
<tr>
<td>v1.6.0</td>
<td>3.4.0</td>
</tr>
</tbody>
</table>
<p>If <code>protoc</code> hasn&rsquo;t been installed, you can download the <code>protoc</code> binaries from
<a href="https://github.com/google/protobuf/releases">the protocol buffers GitHub repository</a>.
Then unzip this file and Update the environment variable <code>PATH</code> to include the path to
the protoc binary file./protobuf/releases).
Then unzip this file and Update the environment variable <code>PATH</code> to include the path to
the protoc binary file.</p>
<p>If you really must compile <code>protoc</code> from source, you can run the following
commands, but this is risky because there is no easy way to uninstall /
upgrade to a newer release.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ cd grpc/third_party/protobuf
$ ./autogen.sh <span style="color:#f92672">&amp;&amp;</span> ./configure <span style="color:#f92672">&amp;&amp;</span> make
$ sudo make install</code></pre></div>
<h4 id="protobuf-runtime-library">Protobuf Runtime library</h4>
<p>There are two protobuf runtime libraries to choose from. They are identical
in terms of APIs offered. The C implementation provides better performance,
while the native implementation is easier to install. Make sure the installed
protobuf version works with grpc version.</p>
<h5 id="1-c-implementation-for-better-performance">1. C implementation (for better performance)</h5>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ sudo pecl install protobuf</code></pre></div>
<p>or specific version</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ sudo pecl install protobuf-3.4.0</code></pre></div>
<p>After protobuf extension is installed, Update php.ini by adding this line
to your <code>php.ini</code> file, (e.g. <code>/etc/php5/cli/php.ini</code>,
<code>/etc/php5/apache2/php.ini</code>, or <code>/usr/local/etc/php/5.6/php.ini</code>),
depending on where your PHP installation is.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">extension<span style="color:#f92672">=</span>protobuf.so</code></pre></div>
<h5 id="2-php-implementation-for-easier-installation">2. PHP implementation (for easier installation)</h5>
<p>Add this to your <code>composer.json</code> file:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-json" data-lang="json"> <span style="color:#e6db74">&#34;require&#34;</span><span style="color:#960050;background-color:#1e0010">:</span> {
<span style="color:#f92672">&#34;google/protobuf&#34;</span>: <span style="color:#e6db74">&#34;^v3.3.0&#34;</span>
}</code></pre></div>
<h4 id="php-protoc-plugin">PHP Protoc Plugin</h4>
<p>You need the gRPC PHP protoc plugin to generate the client stub classes.
It can generate server and client code from .proto service definitions.</p>
<p>It should already been compiled when you run <code>make</code> from the root directory
of this repo. The plugin can be found in the <code>bins/opt</code> directory. We are
planning to provide a better way to download and install the plugin
in the future.</p>
<p>You can also just build the gRPC PHP protoc plugin by running:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ git clone -b <span style="color:#66d9ef">$(</span>curl -L https://grpc.io/release<span style="color:#66d9ef">)</span> https://github.com/grpc/grpc
$ cd grpc
$ git submodule update --init
$ make grpc_php_plugin</code></pre></div>
<p>Plugin may use the new feature of the new protobuf version, thus please also
make sure that the protobuf version installed is compatible with the grpc version
you build this plugin.</p>
<h3 id="download-the-example">Download the example</h3>
<p>You&rsquo;ll need a local copy of the example code to work through this quickstart.
Download the example code from our GitHub repository (the following command
clones the entire repository, but you just need the examples for this quickstart
and other tutorials):</p>
<p>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. <a href="/docs/tutorials/basic/node/">Node.js</a>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ <span style="color:#75715e"># Clone the repository to get the example code:</span>
$ git clone -b v1.20.0 https://github.com/grpc/grpc
$ <span style="color:#75715e"># Build grpc_php_plugin to generate proto files if not build before</span>
$ cd grpc <span style="color:#f92672">&amp;&amp;</span> git submodule update --init <span style="color:#f92672">&amp;&amp;</span> make grpc_php_plugin
$ <span style="color:#75715e"># Navigate to the &#34;hello, world&#34; PHP example:</span>
$ cd examples/php
$ ./greeter_proto_gen.sh
$ composer install</code></pre></div>
<h3 id="run-a-grpc-application">Run a gRPC application</h3>
<p>From the <code>examples/node</code> directory:</p>
<ol>
<li>Run the server</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"> $ npm install
$ cd dynamic_codegen
$ node greeter_server.js</code></pre></div>
<p>In another terminal, from the <code>examples/php</code> directory:</p>
<ol>
<li>Run the client</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"> $ ./run_greeter_client.sh</code></pre></div>
<p>Congratulations! You&rsquo;ve just run a client-server application with gRPC.</p>
<h3 id="update-a-grpc-service">Update a gRPC service</h3>
<p>Now let&rsquo;s look at how to update the application with an extra method on the
server for the client to call. Our gRPC service is defined using protocol
buffers; you can find out lots more about how to define a service in a <code>.proto</code>
file in <a href="/docs/tutorials/basic/php/">gRPC Basics: PHP</a>. For now all you need to know is that both the
server and the client &ldquo;stub&rdquo; have a <code>SayHello</code> RPC method that takes a
<code>HelloRequest</code> parameter from the client and returns a <code>HelloResponse</code> from
the server, and that this method is defined like this:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php">// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user&#39;s name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}</code></pre></div>
<p>Let&rsquo;s update this so that the <code>Greeter</code> service has two methods. Edit
<code>examples/protos/helloworld.proto</code> and update it with a new <code>SayHelloAgain</code>
method, with the same request and response types:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php">// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user&#39;s name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}</code></pre></div>
<p>(Don&rsquo;t forget to save the file!)</p>
<h3 id="generate-grpc-code">Generate gRPC code</h3>
<p>Next we need to update the gRPC code used by our application to use the new
service definition. From the <code>grpc</code> root directory:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ protoc --proto_path<span style="color:#f92672">=</span>examples/protos <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> --php_out<span style="color:#f92672">=</span>examples/php <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> --grpc_out<span style="color:#f92672">=</span>examples/php <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> --plugin<span style="color:#f92672">=</span>protoc-gen-grpc<span style="color:#f92672">=</span>bins/opt/grpc_php_plugin <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> ./examples/protos/helloworld.proto</code></pre></div>
<p>or running the helper script under the <code>grpc/example/php</code> directory if you build
grpc-php-plugin by source:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">$ ./greeter_proto_gen.sh</code></pre></div>
<p>This regenerates the protobuf files, which contain our generated client classes,
as well as classes for populating, serializing, and retrieving our request and
response types.</p>
<h3 id="update-and-run-the-application">Update and run the application</h3>
<p>We now have new generated client code, but we still need to implement and call
the new method in the human-written parts of our example application.</p>
<h4 id="update-the-server">Update the server</h4>
<p>In the same directory, open <code>greeter_server.js</code>. Implement the new method like
this:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">sayHello</span>(<span style="color:#a6e22e">call</span>, <span style="color:#a6e22e">callback</span>) {
<span style="color:#a6e22e">callback</span>(<span style="color:#66d9ef">null</span>, {<span style="color:#a6e22e">message</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;Hello &#39;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">call</span>.<span style="color:#a6e22e">request</span>.<span style="color:#a6e22e">name</span>});
}
<span style="color:#66d9ef">function</span> <span style="color:#a6e22e">sayHelloAgain</span>(<span style="color:#a6e22e">call</span>, <span style="color:#a6e22e">callback</span>) {
<span style="color:#a6e22e">callback</span>(<span style="color:#66d9ef">null</span>, {<span style="color:#a6e22e">message</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;Hello again, &#39;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">call</span>.<span style="color:#a6e22e">request</span>.<span style="color:#a6e22e">name</span>});
}
<span style="color:#66d9ef">function</span> <span style="color:#a6e22e">main</span>() {
<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">server</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">grpc</span>.<span style="color:#a6e22e">Server</span>();
<span style="color:#a6e22e">server</span>.<span style="color:#a6e22e">addProtoService</span>(<span style="color:#a6e22e">hello_proto</span>.<span style="color:#a6e22e">Greeter</span>.<span style="color:#a6e22e">service</span>,
{<span style="color:#a6e22e">sayHello</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">sayHello</span>, <span style="color:#a6e22e">sayHelloAgain</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">sayHelloAgain</span>});
<span style="color:#a6e22e">server</span>.<span style="color:#a6e22e">bind</span>(<span style="color:#e6db74">&#39;0.0.0.0:50051&#39;</span>, <span style="color:#a6e22e">grpc</span>.<span style="color:#a6e22e">ServerCredentials</span>.<span style="color:#a6e22e">createInsecure</span>());
<span style="color:#a6e22e">server</span>.<span style="color:#a6e22e">start</span>();
}
...
</code></pre></div>
<h4 id="update-the-client">Update the client</h4>
<p>In the same directory, open <code>greeter_client.php</code>. Call the new method like this:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-php" data-lang="php"> $request = new Helloworld\HelloRequest();
$request-&gt;setName($name);
list($reply, $status) = $client-&gt;SayHello($request)-&gt;wait();
$message = $reply-&gt;getMessage();
list($reply, $status) = $client-&gt;SayHelloAgain($request)-&gt;wait();
$message = $reply-&gt;getMessage();</code></pre></div>
<h4 id="run">Run!</h4>
<p>Just like we did before, from the <code>examples/node/dynamic_codegen</code> directory:</p>
<ol>
<li>Run the server</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"> $ node greeter_server.js</code></pre></div>
<p>In another terminal, from the <code>examples/php</code> directory:</p>
<ol>
<li>Run the client</li>
</ol>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"> $ ./run_greeter_client.sh</code></pre></div>
<h3 id="what-s-next">What&rsquo;s next</h3>
<ul>
<li>Read a full explanation of how gRPC works in <a href="/docs/guides/">What is gRPC?</a>
and <a href="/docs/guides/concepts/">gRPC Concepts</a></li>
<li>Work through a more detailed tutorial in <a href="/docs/tutorials/basic/php/">gRPC Basics: PHP</a></li>
<li>Explore the gRPC PHP core API in its <a href="/grpc/php/namespace-Grpc.html">reference
documentation</a></li>
</ul>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>