Skip to content

Web Service written in PHP returning JSON

This page provides an example of a web service written in PHP. The service supports the following requests:

  • Hotspot with response as JSON
  • Data with response as JSON

Script

When the script below is invoked, the handleResponse function is called (see last line). The function determines whether the request is for a hotspot or for data and calls a function to get that information as a PHP object. The handleResponse function then emits headers, encodes the PHP object in JSON format, and emits the JSON as its response to the request.

<?php
function emitHotspot($id)
{
    $response = new class{};
    if ($id)
        $response->html = "Request for hostpot Id '$id' received";
    else
        $response->error = "No hotspot Id parameter provided";
    return $response;
}

function emitData($id)
{
    if ($id)
    {
        $response = [];
        for ($i = 1; $i <= 3; $i++)
        {
            $data = new class {};
            $data->id = "$id-$i";
            $data->html = "<div>Data $i</div>";
            $response[] = $data;
        }
    }
    else
    {
        $data = new class {};
        $data->error = "No data Id parameter provided";
        $response = $data;
    }
    return $response;
}

function handleResponse()
{
    $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : "";
    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "";

    if ($type == "hotspot")
        $response = emitHotspot($id);
    else if ($type == "data")
        $response = emitData($id);
    else
    {
        $response = new class{};
        $response->error = $type ? "Unsupported type parameter '$type' provided" : "No type parameter provided";
    }

    header("Content-Type:application/json");
    header('Access-Control-Allow-Origin: *');
    echo json_encode($response);
}

handleResponse();
?>

Usage

Below are examples of requests that invoke the above web service called service.php.

function onEventRequestLiveData(event) {
   let url = "https://livedata.mapsalive.com/php/example/service.php";
   event.api.liveData.requestHotspot("json", 0, url, "type", "hotspot", "id", event.hotspot.id);
}
function onEventPageLoaded(event) {
   let url = "https://livedata.mapsalive.com/php/example/service.php";
   event.api.liveData.requestHotspot("json", 0, url, "type", "data", "id", "test");
}

Response

Below are responses from the web service. You can click the links to see the responses in a browser.

Hotspot HTML as JSON

https://livedata.mapsalive.com/php/example/service.php?type=hotspot&id=test

{
    "html":"Request for hostpot Id 'test' received"
}

Data as JSON

https://livedata.mapsalive.com/php/example/service.php?type=data&id=test

[
    {
        "id": "test-1",
        "html": "<div>Data 1</div>"
    },
    {
        "id": "test-2",
        "html": "<div>Data 2</div>"
    },
    {
        "id": "test-3",
        "html": "<div>Data 3</div>"
    }
]

Error as JSON

https://livedata.mapsalive.com/php/example/service.php

{
    "error":"No type parameter provided"
}