---
title: "Writing a Greeter Client"
url: https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client
language: php
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Writing a Greeter Client

Programming language: PHP. Other languages: [C++](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/matlab.md), [Python](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/swift.md).

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](https://github.com/zeroc-ice/ice-demos/tree/3.8/php/Ice/greeter).

## 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:

```shell
slice2php Greeter.ice
```

This produces a single PHP source file named `Greeter.php`. 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
<?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`:

```php
$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.

### 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.

```php
$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 over `tcp` on `hello.zeroc.com` on port `4061`.

> **Note**
>
> 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:

```php
$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

At the end of the script, Ice destroys the communicator, which gracefully closes the connection to the server.

## Running the Client

We can run this client script with PHP as follows:

```shell
php Client.php
```

> **Note**
>
> 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).
