Skip to content

Instantly share code, notes, and snippets.

@MikSDigital
Forked from isogram/playstore_crawler.php
Created February 3, 2017 10:09
Show Gist options
  • Save MikSDigital/dda9e259a8dae0ef2b365bc90ce24343 to your computer and use it in GitHub Desktop.
Save MikSDigital/dda9e259a8dae0ef2b365bc90ce24343 to your computer and use it in GitHub Desktop.
Playstore Crawler PHP using Symfony/DomCrawler and GuzzleHttp
<?php
use Cache; // alias from laravel
use Symfony\Component\DomCrawler\Crawler;
use GuzzleHttp\Client;
public function parsePlayStore($url='')
{
$parsedUrl = parse_url($url, PHP_URL_QUERY);
$packageNameFromUrl = 'com.inponsel.android';
if ($parsedUrl) {
$tmpArrUrl = explode('=', $parsedUrl);
if (isset($tmpArrUrl[1])) {
$packageNameFromUrl = $tmpArrUrl[1];
}
}
if(Cache::has(md5($url))){
$htmlResponse = Cache::get(md5($url));
} else {
$client = new Client();
$response = $client->request('GET', $url);
$htmlResponse = $response->getBody()->__toString();
Cache::put(md5($url), $htmlResponse, 60);
}
$crawler = new Crawler($htmlResponse);
$ps['url'] = $url;
$ps['title'] = trim($crawler->filter('.document-title')->first()->text());
$ps['iconImage'] = str_replace('=w300', '', $crawler->filter('img.cover-image')->attr('src'));
$ps['packageName'] = $packageNameFromUrl;
$ps['rating'] = str_replace(',', '.', $crawler->filter('.score-container > .score')->first()->text());
$ps['downloadsCount'] = trim($crawler->filter('.details-section-contents > .meta-info > .content')->eq(2)->text());
$ps['size'] = trim(str_replace(',', '.', $crawler->filter('.details-section-contents > .meta-info > .content')->eq(1)->text()));
return $ps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment