Skip to content

Commit 5599774

Browse files
authored
unifying returned results: always include "success" and "message" #16
Merge pull request #16 from IzzySoft/finalize
2 parents 7e46918 + 42473a3 commit 5599774

File tree

1 file changed

+65
-45
lines changed

1 file changed

+65
-45
lines changed

google-play.php

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* @author Max & Izzy
66
* @copyright MIT https://github.com/BaseMax/GooglePlayWebServiceAPI/blob/master/LICENSE
77
* @log 2020-10-19 first release
8-
* @log 2020-12-07 recent version
9-
* @brief releases: 2020-10-19, 2020-10-25, 2020-10-29, 2020-10-30, 2020-12-05, 2020-12-06
8+
* @log 2020-12-10 recent version
9+
* @brief releases: 2020-10-19, 2020-10-25, 2020-10-29, 2020-10-30, 2020-12-05, 2020-12-06, 2020-12-07, 2020-12-10
1010
* @webpage repository https://github.com/BaseMax/GooglePlayWebServiceAPI
1111
**/
1212
class GooglePlay {
@@ -78,6 +78,7 @@ public function parseApplication($packageName, $lang='en_US', $loc='US') {
7878
return ['success'=>0,'message'=>$this->lastError];
7979
}
8080
$values = [];
81+
$message = '';
8182
$values["packageName"] = $packageName;
8283

8384
$values["name"] = strip_tags($this->getRegVal('/itemprop="name">(?<content>.*?)<\/h1>/'));
@@ -161,35 +162,7 @@ public function parseApplication($packageName, $lang='en_US', $loc='US') {
161162
print_r($values);
162163
}
163164
$values['success'] = 1;
164-
return $values;
165-
}
166-
167-
/** Parse page specified by URL for playstore links and extract package names
168-
* @method public parse
169-
* @param optional string link link to parse; if empty or not specified, defaults to 'https://play.google.com/apps'
170-
* @param optional bool is_url whether the link passed is an url to fetch-and-parse (true, default) or a string just to parse (false)
171-
* @return array array of package names
172-
*/
173-
public function parse($link=null, $is_url=true) {
174-
if ( $is_url ) {
175-
if ($link == "" || $link == null) {
176-
$link = "https://play.google.com/apps";
177-
}
178-
$input = file_get_contents($link);
179-
} else {
180-
$input = $link;
181-
}
182-
preg_match_all('/href="\/store\/apps\/details\?id=(?<ids>[^\"]+)"/i', $input, $ids);
183-
if ( isset($ids["ids"]) ) {
184-
$ids = $ids["ids"];
185-
$ids = array_values(array_unique($ids));
186-
$values = $ids;
187-
} else {
188-
$values = [];
189-
}
190-
if ($this->debug) {
191-
print_r($values);
192-
}
165+
$values['message'] = $message;
193166
return $values;
194167
}
195168

@@ -250,7 +223,42 @@ public function parsePerms($packageName, $lang='en') {
250223
foreach($arr[2] as $perm) $perms_unique[] = $perm[1];
251224
}
252225

253-
return ['success'=>1, 'grouped'=>$perms, 'perms'=>array_unique($perms_unique)];
226+
return ['success'=>1, 'message'=>'', 'grouped'=>$perms, 'perms'=>array_unique($perms_unique)];
227+
}
228+
229+
/** Parse page specified by URL for playstore links and extract package names
230+
* @method public parse
231+
* @param optional string link link to parse; if empty or not specified, defaults to 'https://play.google.com/apps'
232+
* @param optional bool is_url whether the link passed is an url to fetch-and-parse (true, default) or a string just to parse (false)
233+
* @return array array of package names
234+
* @brief this mainly is a helper for all methods parsing for app links, like parseTopApps, parseSimilar etc.
235+
*/
236+
public function parse($link=null, $is_url=true) {
237+
if ( $is_url ) {
238+
if ($link == "" || $link == null) {
239+
$link = "https://play.google.com/apps";
240+
}
241+
if ( ! $input = @file_get_contents($link) ) {
242+
$this->lastError = $http_response_header[0];
243+
return [];
244+
} else {
245+
$this->lastError = ''; // reset
246+
}
247+
} else {
248+
$input = $link;
249+
}
250+
preg_match_all('/href="\/store\/apps\/details\?id=(?<ids>[^\"]+)"/i', $input, $ids);
251+
if ( isset($ids["ids"]) ) {
252+
$ids = $ids["ids"];
253+
$ids = array_values(array_unique($ids));
254+
$values = $ids;
255+
} else {
256+
$values = [];
257+
}
258+
if ($this->debug) {
259+
print_r($values);
260+
}
261+
return $values;
254262
}
255263

256264
/** Obtain list of top apps
@@ -260,7 +268,9 @@ public function parsePerms($packageName, $lang='en') {
260268
*/
261269
public function parseTopApps() {
262270
$link = "https://play.google.com/store/apps/top";
263-
return $this->parse($link);
271+
$data = $this->parse($link);
272+
if ( empty($this->lastError) ) return ['success'=>1, 'message'=>'', 'data'=>$data];
273+
else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data];
264274
}
265275

266276
/** Obtain list of newest apps
@@ -270,7 +280,9 @@ public function parseTopApps() {
270280
*/
271281
public function parseNewApps() {
272282
$link = "https://play.google.com/store/apps/new";
273-
return $this->parse($link);
283+
$data = $this->parse($link);
284+
if ( empty($this->lastError) ) return ['success'=>1, 'message'=>'', 'data'=>$data];
285+
else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data];
274286
}
275287

276288
/** Parse Play Store page for a given category and return package names
@@ -281,17 +293,20 @@ public function parseNewApps() {
281293
*/
282294
public function parseCategory($category) {
283295
$link = "https://play.google.com/store/apps/category/" . $category;
284-
return $this->parse($link);
296+
$data = $this->parse($link);
297+
if ( empty($this->lastError) ) return ['success'=>1, 'message'=>'', 'data'=>$data];
298+
else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data];
285299
}
286300

287301
/** Obtain list of available categories
288302
* @method public parseCategories
289303
* @return array array[0..n] of category names to be used with this::parseCategory
290304
*/
291305
public function parseCategories() {
292-
$input = file_get_contents('https://play.google.com/store/apps/details?id=com.google.android.gm&hl=en&gl=US');
293-
preg_match_all('!href="/store/apps/category/([^"]+)"[^>]*>([^<]+)!i', $input, $cats);
294-
return array_unique($cats[1]);
306+
if ( ! $this->getApplicationPage('com.google.android.gm','en','US') )
307+
return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]];
308+
preg_match_all('!href="/store/apps/category/([^"]+)"[^>]*>([^<]+)!i', $this->input, $cats);
309+
return ['success'=>1, 'message'=>'', 'data'=>array_unique($cats[1])];
295310
}
296311

297312
/** Obtain list of similar apps
@@ -301,11 +316,11 @@ public function parseCategories() {
301316
*/
302317
public function parseSimilar($packageName) {
303318
if ( ! $this->getApplicationPage($packageName) )
304-
return ['success'=>0,'message'=>$this->lastError];
319+
return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]];
305320
$input = $this->getRegVal('!<h2 class="sv0AUd bs3Xnd">Similar</h2></a>(?<content>.+?)(<c-wiz jsrenderer="rx5H8d"|</aside>)!ims');
306321
if ( empty($input) )
307-
return ['success'=>0,'message'=>'no data found'];
308-
return $this->parse($input, false);
322+
return ['success'=>1, 'message'=>'no data found', 'data'=>[]];
323+
return ['success'=>1, 'message'=>'', 'data'=>$this->parse($input, false)];
309324
}
310325

311326
/** Obtain list of other apps by same author
@@ -315,11 +330,11 @@ public function parseSimilar($packageName) {
315330
*/
316331
public function parseOthers($packageName) {
317332
if ( ! $this->getApplicationPage($packageName) )
318-
return ['success'=>0,'message'=>$this->lastError];
333+
return ['success'=>0, 'message'=>$this->lastError, 'data'=>[]];
319334
$input = $this->getRegVal('!<h2 class="sv0AUd bs3Xnd">More by [^<]*</h2></a></div><div class="W9yFB">(?<content>.+?)</c-data></c-wiz></div></div></div><script!ims');
320335
if ( empty($input) )
321-
return ['success'=>0,'message'=>'no data found'];
322-
return $this->parse($input, false);
336+
return ['success'=>1, 'message'=>'no data found', 'data'=>[]];
337+
return ['success'=>1, 'message'=>'', 'data'=>$this->parse($input, false)];
323338
}
324339

325340
/** Search for apps by a given string
@@ -329,6 +344,11 @@ public function parseOthers($packageName) {
329344
*/
330345
public function parseSearch($query) {
331346
$link = "https://play.google.com/store/search?q=". urlencode($query) ."&c=apps";
332-
return $this->parse($link);
347+
$data = $this->parse($link);
348+
if ( empty($this->lastError) ) {
349+
if ( empty($data) ) return ['success'=>1, 'message'=>'no data found', 'data'=>$data];
350+
else return ['success'=>1, 'message'=>'', 'data'=>$data];
351+
}
352+
else return ['success'=>0, 'message'=>$this->lastError, 'data'=>$data];
333353
}
334354
}

0 commit comments

Comments
 (0)