UNIFIED DATA PROCESSING FRAMEWORK
composer require flow-php/etl ~0.27.0 Extracts
Read from various data sources.
Transforms
Shape and optimize for your needs.
Loads
Store and secure in one of many available data sinks.
Examples:
▶
▼
composer.json
{
"name": "flow-php/examples",
"description": "Flow PHP - Examples",
"license": "MIT",
"type": "library",
"require": {
"php": "^8.4",
"flow-php/etl": "1.x-dev",
"flow-php/etl-adapter-http": "1.x-dev",
"nyholm/psr7": "^1.8",
"symfony/http-client": "^6.4 || ^7.4"
},
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}code.php<?php
declare(strict_types=1);
use function Flow\ETL\DSL\{data_frame, ref, to_stream};
use Flow\ETL\Adapter\Http\PsrHttpClientStaticExtractor;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\{MockHttpClient, Psr18Client};
use Symfony\Component\HttpClient\Response\MockResponse;
require __DIR__ . '/vendor/autoload.php';
$requests = static function () : \Generator {
yield (new Psr17Factory())
->createRequest('GET', 'https://example.com');
};
$client = new PsrHttpClientStaticExtractor(
new Psr18Client(
new MockHttpClient(
[
new MockResponse(
file_get_contents(__DIR__ . '/input/example.com.html'),
[
'response_headers' => [
'Content-Type' => 'text/html',
],
],
),
],
),
),
$requests(),
);
data_frame()
->read($client)
->withEntry('title', ref('response_body')->htmlQuerySelector('body div h1')->domElementValue())
->withEntry('paragraphs', ref('response_body')->htmlQuerySelectorAll('body p')->expand())
->select('title', 'paragraphs')
->write(to_stream(__DIR__ . '/output.txt', truncate: false))
->run();
Output
+----------------+-------------------------------------------------------------------------------------------------------+
| title | paragraphs |
+----------------+-------------------------------------------------------------------------------------------------------+
| Example Domain | This domain is for use in documentation examples without needing permission. Avoid use in operations. |
| Example Domain | <a href="https://iana.org/domains/example">Learn more</a> |
+----------------+-------------------------------------------------------------------------------------------------------+
2 rows