1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Server
  5. API Manager
  6. Custom PHP

Custom PHP

The Custom PHP feature allows you to connect your API to a custom PHP script. In this way, you will be able to perform complex server-side operations and take control of WordPress as well.

When you choose to use this feature, the other operations like Read, Update, etc., are disabled.

Be careful when you use this feature. Because PHP allows you to access server-side features, a development error may compromise the operation of both WordPress and UniREST Solution.

PHP Editor

To start working with the Custom PHP feature, just check the Custom PHP option of the API you are creating. Because this API will work with your PHP code and not with a specific Database table, the selection of a table name is not required and the Custom PHP checkbox is immediately available in the graphical interface.

When you check the Custom PHP feature, the UniREST Server application will ask you for a confirmation, because the other options (Read, Write, Update, Delete, Custom SQL) will be hidden and not available. A PHP code editor will appear in the area below.

The UniREST Server application provides a basic PHP editor suitable to type simple scripts. The PHP code must be written inside the <?php and ?> tags, as usual.

Include WP Engine

If you want to work with WordPress features, check this option to include the WordPress Engine. In this way, you will have access to WordPress posts, APIs, frameworks, etc.


Working with Unity

In order to make your PHP script correctly work with Unity, you must respect some convention.

Basically, you are free to use PHP to develop what you need, but if you want to receive data from Unity and correctly reply to your Unity project, you have to use some built-in variables.

Receiving data from Unity

When your Unity project calls this kind of API, the UniREST Server application automatically reads the incoming data (if it’s present) and generates an associative array in the built-in $URInput variable.

For instance, if you’re sending from Unity a block of data based on this C# class…

[System.Serializable]
class MyData
{
      public string message;
      public bool isReady;
      public float value;
}

…the $URInput variable will be something like this:

<?php
      $message = $URInput["message"];
      $isReady = $URInput["isReady"];
      $value = $URInput["value"];
?>

Replying to Unity call

When Unity calls an API, it expects to receive a response. This response must be provided by PHP through the $UROuput built-in variable. It’s an associative array containing two keys: result and data. The UniREST Server application will automatically recognize this variable and send it to Unity in the proper way.

$UROutput parameters

resultstringThe result parameter must be a simple string containing:
OK (uppercase) which means that everything worked correctly;
something else (i.e. ERROR) which will be interpreted by UniREST as an error.
dataassociative
array
The data parameter must contain:
– an associative array, if your API is designed for replying with a single block of data;
OR
– an array of associative arrays, if your API is designed for replying with an array of blocks of data.

The associative array must replicate the same properties as declared in your data C# class. In this way, the UniREST Server application will be able to produce a JSON representation of your PHP data that can be converted into your C# class data structure.

Examples
In the following example, the C# class that represents the data structure has 3 properties: message, isReady, and value.

[System.Serializable]
class MyDataClass {
      public string message;
      public bool isReady;
      public fload value;
}

Because Unity expects to receive a JSON response, the JSON string must contains the same C# class properties. In the PHP code, this JSON response can be simply created with an associative array with keys named as the C# class properties.

<?php
      $reply = array();
      $reply["message"] = "HELLO!";
      $reply["isReady"] = true;
      $reply["value"] = 100;

      $UROutput["result"] = "OK";
      $UROutput["data"] = $reply;
?>

In this second example, the API is designed to reply with an array of data.

<?php
      $reply = array();

      // A block of data.
      $reply[]["message"] = "HELLO!";
      $reply[]["isReady"] = true;
      $reply[]["value"] = 100;

      // Another block of data.
      $reply[]["message"] = "GOODBYE!";
      $reply[]["isReady"] = false;
      $reply[]["value"] = 1;

      $UROutput["result"] = "OK";
      $UROutput["data"] = $reply;
?>

UniRESTClient Call() / CallOne() methods

In your Unity project (client-side) you must use Call() and CallOne() methods to communicate with this kind of API (see the “Call() / CallOne()” chapter).

As shown in the examples above, the UniREST Client accepts as response a single block of data or an array of blocks of data. For this reason, the UniRESTClient class has two methods: CallOne() for the first scenario and Call() for the second scenario.


Unity C# / API’s PHP relations

  1. C# and PHP must share the same data structure for the C# sent data and the PHP reply. This means they have to use the same “names”: in C# for the class properties, in PHP for the keys of the associative array reply.
    .
  2. If the Unity call contains data to send (it’s optional), those data are automatically “rendered” by PHP into the built-in $URInput associative array variable. This array has keys with the same name as the C# class properties.
    .
  3. The built-in $UROutput variable is an associative array with two fixed keys: result and data. This output is sent back to Unity which converts it into the C# class structure.

Security

Like all the APIs managed by the UniREST Solution, also this kind of API is under the UniREST security system. This means that:

  • only logged-in users (or a logged-in application) can call this API;
  • data transmission is encrypted and decrypted.

Working with WordPress

When the Include WP Engine option is checked, the WordPress Engine is automatically included in your API and you have access to all the WordPress features through PHP.

What you can do with WordPress and PHP is documented in the official WordPress Developer portal (https://developer.wordpress.org/) and the UniREST server application doesn’t limit you.

Example #1

In the following example, this API receives an ID from Unity, looks for a WordPress post with that ID, reads the post title and replies to Unity with that title.

<?php
      // Taking the wp Post with the received ID.
      $postID = $URInput["postID"];
      $wp_post = get_post($postID);
      $post_title = $wp_post->title;

      // Creating the reply for Unity.
      $reply = array();
      $reply["title"] = $post_title;

      // Reply.
      $UROutput["result"] = "OK";
      $UROutput["data"] = $reply;
?>

Example #2

In the following example, this API receives a user ID and a new email address. Then, it uses the WordPress Database framework to update the email of that user. The reply to Unity is just a “DONE!” message.

<?php
      global $wpdb;

      $user_email= $URInput["userEmail"];
      $user_id = $URInput["userID"];

      $wpdb->update($wpdb->users,
             array('user_login', $user_email), array('ID', $user_id),
             array('%s'), array('%d'));

      $reply = array();
      $reply["message"] = "DONE!";

      $UROutput["result"] = "OK";
      $UROutput["data"] = $reply;
?>