Writing a Greeter Client in PHP
This page presents a step-by-step guide to writing the client-side of our PHP Greeter application.
This client creates a proxy to a remote object that implements the Greeter interface, and invokes the greet operation on this object.
You can find the complete source code for this example in the ice-demos repository.
Compile Slice File with Slice Compiler
The first step when writing a PHP application with Ice is to compile the Slice definitions for this application with the Slice to PHP compiler (slice2php).
Here, we compile the Greeter.ice Slice file we wrote earlier:
slice2php Greeter.ice
This produces a single PHP source file named Greeter.rb. This file provides the proxy class that we’ll use in our client code, so it’s essential to generate this file at the beginning of the development process.
Client Script
Our client is a small PHP script that loads the Ice library and the Greeter.php file generated by the Slice compiler:
<?php
require_once 'Ice.php';
require_once 'Greeter.php';
The remainder of this script can be broken down into four pieces:
1. Create a Communicator
First we create a Communicator using Ice\initialize:
$communicator = Ice\initialize($argv);
The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).
Our client, like most Ice applications, creates a single communicator.
It is important to make sure our communicator is properly destroyed (cleaned up) when it’s no longer needed. This ensures that network connections are gracefully closed, threads and joined, and other important clean-up occurs. In PHP, the communicator is destroyed automatically at the end of the script.
2. Create a Greeter Proxy
Next, we need a way to call on a remote Greeter object. In Ice, this is done with proxies. Proxies are local constructs that represent remote Ice objects and provide methods to call operations on those objects.
We create a Greeter proxy by calling createProxy on the GreeterPrxHelper class generated by the Slice compiler. This returns a new instance of a Greeter proxy.
$greeter = VisitorCenter\GreeterPrxHelper::createProxy(
$communicator, 'greeter:tcp -h hello.zeroc.com -p 4061');
createProxy accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached tcp on localhost on port 4061.
The name of the interface (Greeter) and the identity of the Ice object (greeter) are independent. The Ice objects hosted in the server could just as easily have identities like santa, bugsBunny, etc.
3. Make an Invocation
The third step is to call greet on the remote Ice object using our proxy and to print the greeting:
$greeting = $greeter->greet("alice");
echo "$greeting\n";
The greet function does all the heavy lifting for us: the proxy creates a request with the user name (“Alice”), the communicator establishes a connection to hello.zeroc.com:4061 and sends the request over this connection. Later the communicator receives the response, the proxy unmarshals the response’s payload and finally, returns a string (the greeting).
Here, we make a synchronous invocation, which means this call blocks until the response is received. Ice for PHP supports only synchronous invocations; other languages support asynchronous invocations as well.
4. Cleanup
The final step is the end of our block. At this point, our communicator goes out of scope causing it to be destroyed, and then our script completes.
Running the Client
We can run this client script with PHP as follows:
php Client.php
Ice for PHP supports only client development.
Here, we connect to the Greeter server running on hello.zeroc.com. This Ice server is implemented in a language with server-side support (C++, C#, Java, Python, or Swift).