Skip to content

Instantly share code, notes, and snippets.

@developernaren
Created July 7, 2020 19:10
Show Gist options
  • Save developernaren/91e3973d2cef65ac38505123b23a71c2 to your computer and use it in GitHub Desktop.
Save developernaren/91e3973d2cef65ac38505123b23a71c2 to your computer and use it in GitHub Desktop.
Fetch images from a url
<?php
//dependencies
//"clue/buzz-react": "^2.9",
//"symfony/dom-crawler": "^5.1",
//"clue/mq-react": "^1.2"
require_once './vendor/autoload.php';
$url = 'http://example.com/site-with-images';
$html = file_get_contents($url);
$crawler = new \Symfony\Component\DomCrawler\Crawler($html);
$images = $crawler->filterXPath('//img')->getIterator();
$loop = \React\EventLoop\Factory::create();
$client = new \Clue\React\Buzz\Browser($loop);
//
// wraps Browser in a Queue object that executes no more than 10 operations at once
$q = new \Clue\React\Mq\Queue(50, null, function ($url) use ($client) {
return $client->get($url);
});
foreach ($images as $index => $img) {
$imageUrl = $img->getAttribute('src');
$info = pathinfo($imageUrl);
$result = __DIR__ . '/info/' . $info['filename'] . '.' . $info['extension'];
echo $imageUrl . PHP_EOL;
$q($imageUrl)->then(function (Psr\Http\Message\ResponseInterface $response) use ($result){
$content = (string)$response->getBody();
file_put_contents($result, $content);
});
}
$loop->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment