Skip to content

Instantly share code, notes, and snippets.

@allenp
Created August 10, 2015 19:27
Show Gist options
  • Save allenp/3e1a2c4b046ee916585e to your computer and use it in GitHub Desktop.
Save allenp/3e1a2c4b046ee916585e to your computer and use it in GitHub Desktop.
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class ImageScraper {
//GuzzleHttp\Client
private $browser;
//Array
private $config;
//Symfony\Component\DomCrawler\Crawler;
private $crawler;
public function __construct($config)
{
$this->config = $config;
$this->browser = new Client([
'base_url' => $config['web_host'],
'defaults' => [
'allow_redirects' => true
],
]);
$this->crawler = new Crawler();
}
protected function redirect_url($cat)
{
$url = sprintf($this->config['image_redirect'], $this->config['agent_id'], $cat);
return $url;
}
public function getTourImages($category_id)
{
try
{
$page = $this->browser->get($this->redirect_url($category_id));
$this->crawler->addContent($page);
$links = $this->crawler->filter('.hawaiifun-activity-images img')->each(function (Crawler $node, $i) {
return $node->attr('src');
});
return $links;
}
catch(\Exception $ex)
{
return [];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment