Working with third-party REST API

ASP.NET, PHP, Tutorials

In the current world situation, all countries track new and existing cases of COVID-19. Some countries provide an API to access the latest data. Here is an API provided by Hong Kong’s department of health. Today we will learn how to display this data in our own application. Applies to version 10.4 of PHPRunner, ASPRunner.NET and ASPRunnerPro.

Creating the REST View

1. Lets see how we can display data for March 2020. We added a filter for ‘As of date’ field to show data that contains ’03/2020′ in this field (they use UK date format). Click ‘Get result’ and we will see both API query string and the data it returns.

2. Now, lets take a look at the URL:

We can paste it to the web browser and get the same results in JSON fomat. Now it is the time to create a REST connection. The part of the URL is the main connection URL.

3. When setting up the REST View we use the rest of our URL. Now we can run the request, add fields and build the project.

4. And this is how it looks in the web browser. I just shortened field labels to make it look better.

Creating the REST Chart

1. Now proceed to ‘Datasource tables’ screen, right click on our REST View and choose ‘Add chart’. By default it uses the same REST API call and has access to the same dataset.

2. Now we proceed to the chart setup. It is fairly straightforward, just select a few fields to show on the chart.

3. There is one more thing before we are ready to test it in the web browser. Our chart will make more sense if data is sorted by the date. Since this REST API doesn’t support sorting we will have to do this manually.

Proceed to the REST Chart ‘Source’ page, switch to ‘PHP mode’. Most of the code is already generated for us, we just need to insert the following snippet:

if( !$command->order ) {
$command->order[] = array("column" => "As of date", "dir"=>"ASC" );

Here is how the complete code going to look:

$method = "GET";
$url = "filter?";
$url = RunnerContext::PrepareRest( $url );

//	do the API request
$response = $dataSource->getConnection()->requestJson( $url, $method );
if( !$response ) {
	//	something went wrong
	$dataSource->setError( $dataSource->getConnection()->lastError() );
	return false;

//	convert API result into recordset
$rs = $dataSource->resultFromJson( $response, true );

// apply search and order parameters
if( !$command->order ) {
$command->order[] = array("column" => "As of date", "dir"=>"ASC" );
$rs = $dataSource->filterResult( $rs, $command->filter );
$dataSource->reorderResult( $command, $rs );

//	apply pagination
$rs->seekRecord( $command->startRecord );
return $rs;

4. And this how it looks in the web browser.


1 thought on “Working with third-party REST API

  1. Hey guys, excellent. But how do I get a SESSION variable as access token?

Leave a Reply

Your email address will not be published.