Created
October 7, 2015 10:52
-
-
Save isogram/8b2238905e2f5a90d4d6 to your computer and use it in GitHub Desktop.
Playstore Crawler PHP using Symfony/DomCrawler and GuzzleHttp
This file contains 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 | |
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