PHP code example of frictionlessdata / datapackage

1. Go to this page and download the library: Download frictionlessdata/datapackage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

frictionlessdata / datapackage example snippets


use frictionlessdata\datapackage\Package;
$package = Package::load("tests/fixtures/multi_data_datapackage.json");

foreach ($package as $resource) {
    echo $resource->name();
    foreach ($resource as $row) {
        echo $row;
    }
}

foreach ($package as $resource) {
    var_dump($resource->read());
}

Package::validate("tests/fixtures/simple_invalid_datapackage.json");  // array of validation errors

$package = Package::load('http://datahub.io/opendatafortaxjustice/eucountrydatawb/r/datapackage_zip.zip');

$package = Package::load('http://datahub.io/opendatafortaxjustice/eucountrydatawb/r/datapackage_zip.zip');
foreach ($package as $resource) {
    $resource->read(["cast" => false]);
}

$package = Package::load("tests/fixtures/multi_data_datapackage.json");
$package->resources();  // array of resource name => Resource object (see below for Resource class reference)
$package->getResource("first-resource");  // Resource object matching the given name
$package->removeResource("first-resource");
// add a tabular resource
$package->addResource("tabular-resource-name", [
    "profile" => "tabular-data-resource",
    "schema" => [
        "fields" => [
            ["name" => "id", "type" => "integer"],
            ["name" => "name", "type" => "string"]
        ]
    ],
    "path" => [
        "tests/fixtures/simple_tabular_data.csv",
    ]
]);

$package = Package::create([
    "name" => "datapackage-name",
    "profile" => "tabular-data-package"
]);
// add a resource
$package->addResource("resource-name", [
    "profile" => "tabular-data-resource", 
    "schema" => [
        "fields" => [
            ["name" => "id", "type" => "integer"],
            ["name" => "name", "type" => "string"]
        ]
    ],
    "path" => "tests/fixtures/simple_tabular_data.csv"
]);
// save the package descriptor to a file
$package->saveDescriptor("datapackage.json");

$package->save("datapackage.zip");

$resource = $package->getResource("resource-name")

use frictionlessdata\datapackage\Resource;
$resource = Resource::create([
    "name" => "my-resource",
    "profile" => "tabular-data-resource",
    "path" => "tests/fixtures/simple_tabular_data.csv",
    "schema" => ["fields" => [["name" => "id", "type" => "integer"], ["name" => "name", "type" => "string"]]]
]);

foreach ($resource as $row) {};  // iterating
$resource->read();  // get all the data as an array