2015-03-03 85 views
-1

验证我想通过脚本来检查,如果谷歌播放链接,程序是合法的:谷歌播放链接通过PHP

https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggame - 有效 https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggamessdasd - 无效

但每个脚本我买什么或者是免费的是给对我来说404或303的回应。可能有一些重定向。

如何验证这样的链接。如果Google Play商店中存在应用,我需要在我的广告系统中查看1000个链接。

我会写自己的循环,从数据库中读取等,但请有人熟悉PHP,帮助检查。我为此花了300美元,被2人骗了,那就是“检查”链接。始终404或303

+0

它看起来像你将不得不使用第三方API来做到这一点。一个快速的谷歌显示了Android市场的几个选项。 – Scopey 2015-03-03 04:29:15

回答

1

试试这个:

<?php 
/** 
* Check google play app 
* 
* @param string $url Url to check 
* 
* @return boolean True if it exists, false otherwise 
* @throws \Exception On Curl error, an exception is thrown 
*/ 
function checkGooglePlayApp($url) 
{ 
    $curlOptions = array(
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_CUSTOMREQUEST => 'GET', 
     CURLOPT_URL => $url 
    ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, $curlOptions); 
    $result = curl_exec($ch); 
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

    if ($curl_error = curl_error($ch)) 
    { 
     throw new \Exception($curl_error, Exception::CURL_ERROR); 
    } 

    curl_close($ch); 

    return $http_code == '200'; 
} 

$url = 'https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggameERRORERROR'; 
$result = checkGooglePlayApp($url); 
var_dump($result); // Should return false 

$url = 'https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggame'; 
$result = checkGooglePlayApp($url); 
var_dump($result); // Should return true 

它将返回:

bool(false) 
bool(true) 
1

这可以用get_headers功能轻松完成。例如:

不正确的URL

$file = 'https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggamessdasd'; 
$file_headers = get_headers($file); 
print_r($file_headers); 

返回结果:

Array 
(
[0] => HTTP/1.0 404 Not Found 
[1] => Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
[2] => Pragma: no-cache 
[3] => Expires: Fri, 01 Jan 1990 00:00:00 GMT 
[4] => Date: Tue, 03 Mar 2015 04:23:31 GMT 
[5] => Content-Type: text/html; charset=utf-8 
[6] => Set-Cookie: NID=67=QFThy03gh34QypYfoLFTz7bJDI-qzXvuzI05DtrF3aVs1L7NJO9byV6kemHRVVkViz-sodx3Z0GuCQTu9a_1JvToen6ZtjfhNy8MH6DDgH6zix2I4Gm9mauBPCxipnlG;Domain=.google.com;Path=/;Expires=Wed, 02-Sep-2015 04:23:31 GMT;HttpOnly 
[7] => P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." 
[8] => X-Content-Type-Options: nosniff 
[9] => X-Frame-Options: SAMEORIGIN 
[10] => X-XSS-Protection: 1; mode=block 
[11] => Server: GSE 
[12] => Alternate-Protocol: 443:quic,p=0.08 
[13] => Accept-Ranges: none 
[14] => Vary: Accept-Encoding 
) 

如果该文件不存在,将返回:

Array 
(
[0] => HTTP/1.0 200 OK 
[1] => Content-Type: text/html; charset=utf-8 
[2] => Set-Cookie: PLAY_PREFS=CgJVUxC6uYnvvSkourmJ770p:S:ANO1ljKvPst7-nSw; Path=/; Secure; HttpOnly 
[3] => Set-Cookie: NID=67=iFUl_Ls8EhAJE7STIJD7Wdq6NF-y4i6Xrlb78My75ZaruVWlAKObDRDNGDddGxD0hSsLRpvrQK7Tp5nuKCgGg2jF1GUf9_4H_zYsUDQ548Be2n8EDjp9clDfXKLYjmSg;Domain=.google.com;Path=/;Expires=Wed, 02-Sep-2015 04:26:14 GMT;HttpOnly 
[4] => Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
[5] => Pragma: no-cache 
[6] => Expires: Fri, 01 Jan 1990 00:00:00 GMT 
[7] => Date: Tue, 03 Mar 2015 04:26:14 GMT 
[8] => P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." 
[9] => X-Content-Type-Options: nosniff 
[10] => X-Frame-Options: SAMEORIGIN 
[11] => X-XSS-Protection: 1; mode=block 
[12] => Server: GSE 
[13] => Alternate-Protocol: 443:quic,p=0.08 
[14] => Accept-Ranges: none 
[15] => Vary: Accept-Encoding 
) 

所以,你可以创建这样一个脚本:

<?php 
$files = ['https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggame', 'https://play.google.com/store/apps/details?id=com.ketchapp.zigzaggamesadasd']; 

foreach($files as $file) 
{ 
    $headers = get_headers($file); 

    if($headers[0] == 'HTTP/1.0 404 Not Found') 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 
?>