Skip to the main content.
Downloads Try Thriftly
Downloads Try Thriftly
Group 762

Migrate and run DataFlex applications with Oracle, MS SQL Server, PostgreSQL, MySQL &  MariaDB.

flex2Crystal

Stuck in Crystal XI?  Upgrade and use the latest versions of Crystal Reports with DataFlex applications. 

BTR2SQL

Convert from Btrieve / P.SQL / Actian transactional engines to Oracle, MS SQL Server, and PostgreSQL

thriftly-1

Quickly build multi-protocol web services with the same API. Supports JSON-RPC, REST, SOAP,  Thrift, and gRPC.

 Group 671-1

 

Why Mertech?

3 min read

Connect your API, and your data, to PHP applications with Thriftly

Connect your API, and your data, to PHP applications with Thriftly

PHP has become one of the most popular web development languages used today. Sometimes mocked for its inconsistency and accidental rise to prominence, PHP’s ease of use still makes it perfectly suited for simple web development.

But what if you need to connect a more complex application or data source with your PHP app? For that, you’ll want to lean on APIs. In this post, you’ll learn how to connect a JSON-RPC API, created in Thriftly, to a PHP-based application, making it easy to access complex data in simple web apps.

Seeing What API Calls Are Available

Each Thriftly installation includes a sample application, called Property Tracker, and a sample Thriftly API used to manage inventory for a number of simulated business locations. For the purposes of this article, we’re going to pretend we want to build a public-facing PHP-based website over this data, so we can share and access it online.

Using the Thriftly API, we can expose the application’s underlying business logic to the web. When you access the Property Tracker API’s endpoint, you’re taken to a testing interface that shows the API calls available to plug in to your PHP application. These are the calls we’ll reference in the next step, when we begin constructing our PHP app.

Thriftly API Gateway JSON-RPC

Folding API Calls Into a PHP App

Let’s say we’ve decided to consume both the searchLocations and getLocation API calls, to allow us to find business locations that meet certain criteria. Our PHP application skeleton will present this data using two classes and the main index.php file. The classes we’ll use are:

  • JsonRPCClient - This client class converts the JSON-RPC structured data coming from our API to the PHP structure used by our app.
  • LocationController - This class uses the JsonRPCClient class to add a layer of logic to the API.

Since we’re using the JSON-RPC protocol, we must add the JsonRPCClient class to allow data to travel from our API server to our application (and vice-versa). The JsonRPCClient class is based on Curl (programming language) and is application agnostic. This means you can easily add your own controller class to the existing code:

Class LocationController {
/**
* Constructor
*
* @access public
* @param string $server Server object for communicating with the server
*/
public function __construct($server)
{
$this->server = $server;
}

/**
* Function will return an array of locations that meet the passed criteria.
* If no criteria is passed, all locations will be returned. Values are searched by using
* a like condition on its related column. Supports pagination.
*
* @access public
* @param array $criteria search parameters (same as API!)
* @return array or throws exception
*/

 function search($criteria = array()) {

$criteria = array("search" => $criteria);
$json_resp = $this->server->searchLocations($criteria);
return $json_resp;

}

...

The LocationController class uses the JsonRPCClient class as a dependency, allowing us to communicate directly with our API. Each instance will be saved in a ‘server’ property that we can use in other functions within the controller class. The JsonRPCClient class itself uses the power of the magic function ‘__call’ to automatically make API calls from within our controller class.

In our example, we’ve included a “search” function in the controller class that calls to the published function “searchLocation”. However, if you look through the JsonRPC source code, you’ll see that we never defined the “searchLocation” function. That’s because the JsonRPCClient class automatically maps that function to our API.

Pulling the Data Together

In our main index.php file, all we have to do is initialize both classes and start calling the public functions we added to our LocationController class. This allows us to present our data in the HTML section of our application.

// Define Global API Endpoint
define('APISERVER' , 'https://gateway-tx-1.thriftly.io/PropertyTracker');\
// Declare new client class
$ClientObj = new JsonRPC\JsonRPCClient(APISERVER, "LocationService");

//pass dependency
$LocationService = new ThriftlySample\LocationService($ClientObj);

//parameters for the API call
$criteria = array(
"name"=> "",
"city"=> "",
"state"=> "",
"zipCode"=> "",
"country"=> "",
"pageSize"=> 3,
"pageOffset"=> 0
);

// call search with above criteria
$searchData = $LocationService->search($criteria);
$detailedRecord = array();


if (isset($searchData[1])) {
// output details of the second found record
$firstRecordId = $searchData[1]["id"];
$detailedRecord = $LocationService->get($firstRecordId);

}

PHP Example Code Thriftly

Conclusion

In just a few steps, and with just minimal additional code, you can now access complex, underlying business logic from your simple, web-based PHP app. Thriftly APIs allow you to neatly connect with your existing data and push it to the web, letting you focus on creating a polished web application rather than retrofitting your existing software and databases. To get started connecting Thriftly APIs to PHP apps download your free trial and take a look at the sample application source code available in our repository.

New call-to-action

Legacy Application Modernization: Key Steps, Benefits & Best Practices

Legacy Application Modernization: Key Steps, Benefits & Best Practices

This blog post was co-authored with Riaz Merchant, President and CEO at Mertech. In the fast-paced software world, 'legacy' often signals a warning.

Read More
Hybrid Cloud Migration: Plan, Process and Advantages

Hybrid Cloud Migration: Plan, Process and Advantages

This post was co-authored with Riaz Merchant, President/CEO at Mertech Data Systems, Inc.

Read More
Financial Benefits of Cloud Migration & Hybrid Cloud Applications

Financial Benefits of Cloud Migration & Hybrid Cloud Applications

Shifting from your traditional legacy systems to the Cloud can be a game changer, as the benefits of cloud migration are numerous. Cloud computing...

Read More