PHP Classes

File: examples/scraping_multi.php

Recommend this page to a friend!
  Classes of Lars Moelleken   PHP httpful Request   examples/scraping_multi.php   Download  
File: examples/scraping_multi.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP httpful Request
Send and process HTTP requests using handler class
Author: By
Last change:
Date: 3 years ago
Size: 952 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);

require
__DIR__ . '/../vendor/autoload.php';

/**
 * @param string[] $urls
 *
 * @return array
 */
function scraping_multi(array $urls): array
{
   
$client = new \Httpful\ClientPromise();

    foreach (
$urls as $url) {
       
$client->add_html($url);
    }

   
$promise = $client->getPromise();

   
$return = [];
   
$promise->then(static function (Httpful\Response $response, Httpful\Request $request) use (&$return) {
       
/** @var \voku\helper\HtmlDomParser $dom */
       
$dom = $response->getRawBody();

       
// get title
       
$return[] = $dom->find('title', 0)->innertext;
    });

   
$promise->wait();

    return
$return;
}

// -----------------------------------------------------------------------------

$data = scraping_multi(
    [
       
'https://moelleken.org',
       
'https://google.com',
    ]
);

foreach (
$data as $title) {
    echo
'<strong>' . $title . ' </strong><br>' . "\n";
}