Skip to content

PHP-Client for the RebaseData API to read and convert databases

License

Notifications You must be signed in to change notification settings

rebasedata/php-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rebasedata-php-client

Introduction

This library allows to read and convert various database formats in PHP using the RebaseData API. When processing a database, the database is first sent to the secure RebaseData servers which then return the converted data. Below you will find several examples.

Dependencies

The library does not have any dependencies.

Installation

To install this library, you need Composer.

  1. To install rebasedata/php-client using Composer, run the following command:

    php composer.phar require rebasedata/php-client "1.*"
  2. Then include Composer's autoloader file that helps to autoload the libraries it downloads. To use it, just add the following line to your application:

    <?php
    
    require 'vendor/autoload.php';
    
    use RebaseData\Converter\Converter;
    
    $converter = new Converter();

Examples

At first, you need to define one or more input files for the database conversion.

use RebaseData\InputFile\InputFile;

$inputFile = new InputFile('/tmp/access.accdb');

It's also possible to define a name for the input file that is different than the path. The file's name and extension are important for the conversion.

use RebaseData\InputFile\InputFile;

$inputFile = new InputFile('/tmp/a1b2c3e4', 'access.accdb');

List all tables of an ACCDB file.

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/access.accdb');
$inputFiles = [$inputFile];

$converter = new Converter();
$database = $converter->convertToDatabase($inputFiles);
$tables = $database->getTables();

foreach ($tables as $table) {
    echo "Got table: ".$table->getName()."\n";
}

List all tables of a password-protected MSSQL BAK file.

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/backup.bak');
$inputFiles = [$inputFile];

$converter = new Converter();
$database = $converter->convertToDatabase($inputFiles, ['password' => 'value']);
$tables = $database->getTables();

foreach ($tables as $table) {
    echo "Got table: ".$table->getName()."\n";
}

Read the columns of a table called cars of an ACCDB file.

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/access.accdb');
$inputFiles = [$inputFile];

$converter = new Converter();
$database = $converter->convertToDatabase($inputFiles);
$table = $database->getTable('cars');

foreach ($table->getColumns() as $column) {
    echo "Got column: ".$column->getName()."\n";
}

Read the rows of a single table called cars of an ACCDB file. Since we're using the method getRowsIterator() which returns an iterator, the table can also be huge and our memory footprint is still low.

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/access.accdb');
$inputFiles = [$inputFile];

$converter = new Converter();
$database = $converter->convertToDatabase($inputFiles);
$table = $database->getTable('cars');

foreach ($table->getRowsIterator() as $row) {
    echo "Got row: ";
    foreach ($row as $column => $value) {
        echo "$column = $value ";
    }
    echo "\n";
}

If you want to work yourself on the CSV file of a certain table, you can get the CSV file like this:

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/access.accdb');
$inputFiles = [$inputFile];

$converter = new Converter();
$database = $converter->convertToDatabase($inputFiles);
$table = $database->getTable('cars');

$destinationCsvFilePath = '/tmp/cars.csv';

$table->copyTo($destinationCsvFilePath);

echo "You can find the CSV file in $destinationCsvFilePath\n";

Or don't you want to work on the data itself in PHP? Let's assume you want to convert certain input files to a new format and have them stored in a target directory, the following code snippet shows how to do it:

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/access.accdb');
$inputFiles = [$inputFile];

$targetDirectory = '/tmp/output/';
if (!file_exists($targetDirectory)) {
    mkdir($targetDirectory);
}

$converter = new Converter();
$converter->convertAndSaveToDirectory($inputFiles, 'mysql', $targetDirectory);

echo "You can find the MySQL script file (data.sql) in the following directory: $targetDirectory\n";

Similarly, you can convert input files to a new format and have them stored as ZIP file:

use RebaseData\InputFile\InputFile;
use RebaseData\Converter\Converter;

$inputFile = new InputFile('/tmp/access.accdb');
$inputFiles = [$inputFile];

$zipFile = '/tmp/output.zip';

$converter = new Converter();
$converter->convertAndSaveToZipFile($inputFiles, 'mysql', $zipFile);

echo "You can find the ZIP archive that contains the MySQL script file (data.sql) here: $zipFile\n";

By the default, the library will use the system's temporary folder as working directory. If you want to change that, you need to adjust the config:

use RebaseData\Config\Config;

$config = new Config();
$config->setWorkingDirectory('/tmp/rebasedata-working-dir');
 
$converter = new Converter($config);

In case you convert the same input files multiple times, you can enable the local cache so that the future conversion processes are much faster. You can also configure the caching directory. By default, the caching directory is inside of the working directory (see above).

use RebaseData\Config\Config;

$config = new Config();
$config->setCacheEnabled(true);
$config->setCacheDirectory('/tmp/cache/');

$converter = new Converter($config);

For conversions above a certain size, RebaseData requires a Customer Key that you can buy on the website. You can pass it like this:

use RebaseData\Config\Config;
use RebaseData\Converter\Converter;

$config = new Config();
$config->setApiKey('secret value');

$converter = new Converter($config);

Tests

To run tests, run the PHPUnit utility:

./bin/phpunit

License

This code is licensed under the MIT license.

Feedback

We love to get feedback from you! Did you discover a bug? Do you need an additional feature? Open an issue on Github and RebaseData will try to resolve your issue as soon as possible! Thanks in advance for your feedback!