1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. Special Operations
  6. Call / CallOne (Custom PHP)

Call / CallOne (Custom PHP)

The Call() and CallOne() methods work in conjunction with APIs that are linked to custom PHP scripts. They are designed to call these special APIs, send custom data to them and receive a response.

Basically, Call() and CallOne() methods work similarly to the Read() and ReadOne() methods:

  • Call() method expects to receive an array of packs of data as a response;
  • CallOne() method expects to receive just one pack of data as a response.
The right format response must be provided by the PHP code as explained in the ''Custom PHP'' chapter.

Call()

The Call() method calls an API linked to a custom PHP script. This method releases an array of objects in the provided <DATA> format. The API’s PHP code must release an array.

_ = #UniRESTClient.Async#.$Call$<£DATA£>(%api, tableData, fieldsToCheck, callBack%);

DATA|custom Class
A class containing the data structure.

Note:
the class must have the [System.Serializable] attribute.
__
api|string
The API address, under the API class.
__
customData|object
Optional values to send to the server. It must be an instance of the custom class in use.
__
callBack|Action<DATA[]>
The function to call on operation completed.

DATA[]
An array of the custom class data-type or null if nothing is found.

[System.Serializable] 
class MyData {
    public string message;
}

// . . .

_ = UniRESTClient.Async.Call<MyData>(
        API.myphp_api, 
        new MyData(),
        (MyData[] results) =>
        {
            Debug.Log("Reply: " + results[0].message);
        }
);

// OR

_ = UniRESTClient.Async.Call<MyData>(
        API.myphp_api, 
        new MyData { message = "HI! I'M A UNITY GAME!" },
        (MyData[] results) =>
        {
            Debug.Log("Reply: " + results[0].message);
        }
);

CallOne()

The Call() method calls an API linked to a custom PHP script. This method releases a single object of <DATA> type. The API’s PHP code must release a single block of data.

_ = #UniRESTClient.Async#.$CallOne$<£DATA£>(%api, tableData, fieldsToCheck, callBack%);

DATA|custom Class
A class containing the data structure.

Note:
the class must have the [System.Serializable] attribute.
__
api|string
The API address, under the API class.
__
customData|object
Optional values to send to the server. It must be an instance of the custom class in use.
__
callBack|Action<DATA>
The function to call on operation completed.

DATA
A single object of the custom class data-type or null if nothing is found.

[System.Serializable] 
class MyData {
    public string message;
}

// . . .

_ = UniRESTClient.Async.CallOne<MyData>(
        API.myphp_api, 
        new MyData(),
        (MyData result) =>
        {
            Debug.Log("Reply: " + result.message);
        }
);

// OR

_ = UniRESTClient.Async.CallOne<MyData>(
        API.myphp_api, 
        new MyData { message = "HI! I'M A UNITY GAME!" },
        (MyData result) =>
        {
            Debug.Log("Reply: " + result.message);
        }
);

Example (C# and PHP)

The following example shows the use of C# and PHP for reading the title of all WordPress published posts. Because WordPress can contain many posts, the API’s PHP code must release an array, and the Call() method must be used in the Unity project.

PHP Script

The following PHP script calls the get_posts() WordPress method to get all the available Posts. For each found Post, it fills the $reply array with the Posts’ title. Finally, it returns the result to Unity through the built-in $UROutput system variable.

<?php

    $reply = array();

    $my_posts = get_posts();
    if( !empty( $my_posts ) ) {
        foreach ( $my_posts as $p ) $reply[]["title"] = $p->post_title;
    }

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

?>

C# Script

The following C# defines the MyPost class that reflects the PHP’s reply data structure. Then the Call() method asks the PHP script to release the WordPress Posts’ title. When the call is completed, the callBack function is executed with the Posts’ title list available into the titles variable.

[System.Serializable] 
class MyPost {
    public string title;
}

// . . .

_ = UniRESTClient.Call<MyPost>(
        API.myphp_api, 
        new MyPost(),
        (MyPost[] titles) =>
        {
            foreach (var t in titles) 
            {
                Debug.Log("Post title: " + t.title);
            }
         }
);