Created
August 10, 2015 19:27
-
-
Save allenp/3e1a2c4b046ee916585e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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