Skip to content

Instantly share code, notes, and snippets.

@theMadness
Created July 17, 2017 00:38
Show Gist options
  • Save theMadness/d2a5cdb84ae001afcd28f6bc5fcdc454 to your computer and use it in GitHub Desktop.
Save theMadness/d2a5cdb84ae001afcd28f6bc5fcdc454 to your computer and use it in GitHub Desktop.
<?php
namespace DS\App\Console\Commands;
use DS\App\Models\ConciergeRssFeed;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Psr7\Response;
use Illuminate\Console\Command;
class WebDataFetchCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:web-fetch-data';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Retrieve and store the content of remote web resources';
/**
* Execute the console command.
*/
public function handle()
{
$client = new Client([
'timeout' => 20,
]);
$promises = [];
foreach (ConciergeRssFeed::all() as $feed) {
$feed->content='durr';
$feed->save();
$promises[$feed->id] = $client->getAsync($feed->url)->then(function (Response $response) use ($feed) {
$feed->content = $response->getBody()->getContents();
$feed->save();
});
}
Promise\settle($promises)->wait();
$this->info('Data fetched');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment