Esempi di utilizzo dei dati tramite PHP

Per dettagli sulle API di CKAN si faccia riferimento alla relativa documentazione

Le API REST di CKAN rispondono ad interrogazioni HTTP sulla porta 80.

Per avere informazioni sui macrogruppi descrittivi le chiamate si costruiscono usando il suffisso "/api/3/action/"

le query possono essere :

1. group_list
2. package_list
3. tag_list

La query 1. restituisce la lista dei gruppi, eventuali altri parametri possono essere passati

La query 2. restituisce la lista dei dataset, in precedenti versioni di CKAN questi erano chiamati 'packages' e tracce di questa nomenclatura sono rimaste, eventuali altri parametri possono essere passati

La query 3. restituisce la lista complessiva dei tag che descrivono i vari dataset, tramite il singolo tag e' possibile effettuare ricerche sui singoli datasei. Eventuali altri parametri possono essere passati.

Nota bene: I risultati presenti in questa pagina sono frutto di un'attivita' dimostrativa quindi sono da considerare solo come un aiuto per lo studio delle interfacce.

Esempio 1

come ottenere i valori di group_list, package_list, tag_list

In questi esempi si usa prevalentemente la funzione file_get_contents per contattare il server REST, altre strade piu' sofisticate sono percorribile ( ad esempio tramite php-cURL )

In [1]:
$url_base = "http://data.tdm-project.it/api/3/action/";
Out[1]:
"http://data.tdm-project.it/api/3/action/"
In [2]:
$items_to_query = array("group_list", "package_list", "tag_list");


foreach ($items_to_query as $item) {
    print($url_base.$item);
    $data = file_get_contents($url_base.$item);
    
    # JSON to obj
    $obj = json_decode($data);
    
    # obj to formatted JSON, only to show it pretty
    $json_string = json_encode($obj, JSON_PRETTY_PRINT);
    
    print($json_string);
}
Out[2]:
http://data.tdm-project.it/api/3/action/group_list
Out[2]:
{
    "help": "http:\/\/data.tdm-project.it\/api\/3\/action\/help_show?name=group_list",
    "success": true,
    "result": [
        "energia",
        "meteo",
        "trasporti"
    ]
}
Out[2]:
http://data.tdm-project.it/api/3/action/package_list
Out[2]:
{
    "help": "http:\/\/data.tdm-project.it\/api\/3\/action\/help_show?name=package_list",
    "success": true,
    "result": [
        "devicestatus",
        "energymonitor",
        "fotovoltaico",
        "radar_2018-04-30",
        "radar_2018-05-20",
        "radar_2018-06-03",
        "radar_2018-10-12",
        "radar_2018-10-18",
        "radar_2018-11-05",
        "radar_meteo_unica",
        "sim-meteo-latest",
        "weatherobserved"
    ]
}
Out[2]:
http://data.tdm-project.it/api/3/action/tag_list
Out[2]:
{
    "help": "http:\/\/data.tdm-project.it\/api\/3\/action\/help_show?name=tag_list",
    "success": true,
    "result": [
        "Edge",
        "energy",
        "EnergyMonitor",
        "Environment",
        "fotovoltaico",
        "Gateway",
        "meteo",
        "photovoltaic",
        "radar",
        "rainfall",
        "simulation",
        "weather",
        "WeatherObserved"
    ]
}

Esempio 2

La fase di inizializzazione e' comune all'esempio 1

Partendo dal nome di un gruppo ottenuto dall'esempio precedente si richiede quali datasets coinvolga.

Per avere la lista dei datasets occorre impostare a True il parametro include_datasets e tramite il parametro id quale gruppo ispezionare.

In questo caso l'endpoint da usare e' group_show.

Per ulteriori dettagli consultare la relativa documentazione

In [3]:
$data = file_get_contents($url_base.'group_show?id=meteo&include_datasets=1');

# JSON to obj
$obj = json_decode($data);
    
# obj to formatted JSON, only to show it pretty
$json_string = json_encode($obj, JSON_PRETTY_PRINT);

print($json_string);
Out[3]:
{
    "help": "http:\/\/data.tdm-project.it\/api\/3\/action\/help_show?name=group_show",
    "success": true,
    "result": {
        "users": [
            {
                "email_hash": "edf7d8ada56dde083ff3d04d6b1e3861",
                "about": null,
                "capacity": "admin",
                "name": "admin",
                "created": "2018-05-25T16:13:14.988377",
                "sysadmin": true,
                "activity_streams_email_notifications": false,
                "state": "active",
                "number_of_edits": 2198,
                "display_name": "admin",
                "fullname": null,
                "id": "844db47c-c165-4301-88af-f1150a6cb4d4",
                "number_created_packages": 5
            },
            {
                "email_hash": "18a74de6ad4a096522b9ed3728bb7859",
                "about": null,
                "capacity": "admin",
                "name": "fXXXXXX",
                "created": "2018-06-04T15:46:18.241672",
                "sysadmin": false,
                "activity_streams_email_notifications": false,
                "state": "active",
                "number_of_edits": 2,
                "display_name": "XXXXXX XXXXXX",
                "fullname": "XXXXXX XXXXXX",
                "id": "8e1d71f9-f9d5-427b-b34c-76603f46d1a8",
                "number_created_packages": 0
            },
            {
                "email_hash": "0a42bb8cbaee0333bb99f1ff76cb94e0",
                "about": null,
                "capacity": "admin",
                "name": "mXXXXXX",
                "created": "2018-05-25T16:16:22.425894",
                "sysadmin": true,
                "activity_streams_email_notifications": false,
                "state": "active",
                "number_of_edits": 1383,
                "display_name": "XXXXXX XXXXXX",
                "fullname": "XXXXXX XXXXXX",
                "id": "44f157e0-ad1e-4b96-b28c-f50b21d6e02c",
                "number_created_packages": 16
            }
        ],
        "display_name": "meteo",
        "description": "",
        "image_display_url": "",
        "package_count": 18,
        "created": "2018-05-30T14:30:20.833716",
        "name": "meteo",
        "is_organization": false,
        "state": "active",
        "extras": [],
        "image_url": "",
        "groups": [],
        "type": "group",
        "title": "meteo",
        "revision_id": "4bd11508-f755-4930-9c46-8b64ef2b5661",
        "packages": [
            {
                "owner_org": "bd43bfd4-1171-4612-8cee-65dd01503848",
                "maintainer": null,
                "relationships_as_object": [],
                "private": false,
                "maintainer_email": null,
                "num_tags": 2,
                "id": "c8258283-724a-4052-8898-7f131704bb38",
                "metadata_created": "2018-12-03T18:09:00.941350",
                "metadata_modified": "2018-12-03T18:09:01.431270",
                "author": null,
                "author_email": null,
                "state": "active",
                "version": null,
                "license_id": "cc-by",
                "type": "dataset",
                "num_resources": 3,
                "title": "Radar rainfall 2018-11-05",
                "creator_user_id": "44f157e0-ad1e-4b96-b28c-f50b21d6e02c",
                "relationships_as_subject": [],
                "name": "radar_2018-11-05",
                "isopen": true,
                "url": null,
                "notes": "Radar rainfall dataset for weather event 2018-11-05",
                "license_title": "Creative Commons Attribution",
                "extras": [],
                "license_url": "http:\/\/www.opendefinition.org\/licenses\/cc-by",
                "organization": {
                    "description": "Dataset relativi al progetto TDM.",
                    "created": "2018-05-25T16:13:58.956150",
                    "title": "TDM",
                    "name": "tdm",
                    "is_organization": true,
                    "state": "active",
                    "image_url": "2018-07-12-085811.277378tdm-logo.png",
                    "revision_id": "a84e31f9-1220-4650-a9af-f7e7f3a323cd",
                    "type": "organization",
                    "id": "bd43bfd4-1171-4612-8cee-65dd01503848",
                    "approval_status": "approved"
                },
                "revision_id": "2d9f5d02-24c3-4efc-b9a4-c7e2fb169ae3"
            },
            {
                "owner_org": "bd43bfd4-1171-4612-8cee-65dd01503848",
                "maintainer": null,
                "relationships_as_object": [],
                "private": false,
                "maintainer_email": null,
                "num_tags": 2,
                "id": "70086a4b-96a2-4a2e-ac53-c4c4b0a49870",
                "metadata_created": "2018-12-03T18:09:00.048666",
                "metadata_modified": "2018-12-03T18:09:00.554597",
                "author": null,
                "author_email": null,
                "state": "active",
                "version": null,
                "license_id": "cc-by",
                "type": "dataset",
                "num_resources": 3,
                "title": "Radar rainfall 2018-10-18",
                "creator_user_id": "44f157e0-ad1e-4b96-b28c-f50b21d6e02c",
                "relationships_as_subject": [],
                "name": "radar_2018-10-18",
                "isopen": true,
                "url": null,
                "notes": "Radar rainfall dataset for weather event 2018-10-18",
                "license_title": "Creative Commons Attribution",
                "extras": [],
                "license_url": "http:\/\/www.opendefinition.org\/licenses\/cc-by",
                "organization": {
                    "description": "Dataset relativi al progetto TDM.",
                    "created": "2018-05-25T16:13:58.956150",
                    "title": "TDM",
                    "name": "tdm",
                    "is_organization": true,
                    "state": "active",
                    "image_url": "2018-07-12-085811.277378tdm-logo.png",
                    "revision_id": "a84e31f9-1220-4650-a9af-f7e7f3a323cd",
                    "type": "organization",
                    "id": "bd43bfd4-1171-4612-8cee-65dd01503848",
                    "approval_status": "approved"
                },
                "revision_id": "9a2dc0dd-8a19-4d50-8e0f-a95d86b667e8"
            },
            {
                "owner_org": "bd43bfd4-1171-4612-8cee-65dd01503848",
                "maintainer": "",
                "relationships_as_object": [],
                "private": false,
                "maintainer_email": "",
                "num_tags": 3,
                "id": "1c7ab540-e080-4957-b3f0-e550c4c3508a",
                "metadata_created": "2018-06-07T15:39:05.104659",
                "metadata_modified": "2018-06-12T15:46:33.598162",
                "author": "",
                "author_email": "",
                "state": "active",
                "version": "",
                "license_id": "cc-by",
                "type": "dataset",
                "num_resources": 48,
                "title": "radar-meteo-unica",
                "creator_user_id": "44f157e0-ad1e-4b96-b28c-f50b21d6e02c",
                "relationships_as_subject": [],
                "name": "radar_meteo_unica",
                "isopen": true,
                "url": "",
                "notes": "",
                "license_title": "Creative Commons Attribution",
                "extras": [],
                "license_url": "http:\/\/www.opendefinition.org\/licenses\/cc-by",
                "organization": {
                    "description": "Dataset relativi al progetto TDM.",
                    "created": "2018-05-25T16:13:58.956150",
                    "title": "TDM",
                    "name": "tdm",
                    "is_organization": true,
                    "state": "active",
                    "image_url": "2018-07-12-085811.277378tdm-logo.png",
                    "revision_id": "a84e31f9-1220-4650-a9af-f7e7f3a323cd",
                    "type": "organization",
                    "id": "bd43bfd4-1171-4612-8cee-65dd01503848",
                    "approval_status": "approved"
                },
                "revision_id": "cd826984-4c7e-494b-adbc-b8e0582ecce6"
            }
        ],
        "num_followers": 0,
        "id": "47965822-5ec6-4117-8fc4-77b3433e9742",
        "tags": [],
        "approval_status": "approved"
    }
}