我正在开发一个应用程序来下载一些应用程序。但我无法跨越Google Play商店认证。所以我只知道应用程序名称并从第三方网站下载。
我尝试使用get_file_content
和curl
,但收到错误回复:
Response内容必须是实现__toString()的字符串或对象,给定“boolean”。
我的代码:
private function download_file($name) {
$url='https://apkpure.com/snake-off-more-play-more-fun/com.wepie.snakeoff/download?from=details',
$app_name = $name.'.apk';
$path = self::DOWNLOAD_DIR;
set_time_limit ( 0 );
$url = trim ( $url );
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, $url );
curl_setopt ( $curl, CURLOPT_HEADER, 0 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
$file = curl_exec ( $curl );
curl_close ( $curl );
$filename = $path . $app_name;
$write = @fopen ( $filename, "w" );
if ($write == false) {
return false;
}
if (fwrite ( $write, $file ) == false) {
return false;
}
if (fclose ( $write ) == false) {
return false;
}
return $app_name;
}
答案 0 :(得分:0)
使用此代码获取html:
$ckfile = tempnam (sys_get_temp_dir(), rand().'cookiename');
$handle = curl_init($url);
curl_setopt ($handle, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($handle, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($handle, CURLOPT_ENCODING, 'identity');
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
// response total time
$time = curl_getinfo($handle, CURLINFO_TOTAL_TIME);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
$dom = new DOMDocument('1.0');
@$dom->loadHTML($response);
$xpath = new DOMXPath ($dom);
$array = array();
$links = $xpath->query ("//div[@class='fast-download-box']//a[@id='download_link']/@href");
$url = "";
foreach ($links as $link)
{
$url = $link->nodeValue;
if (filter_var($url, FILTER_VALIDATE_URL)) {
break 2;
}
}
download_file($url, 'application');
下载文件:
function download_file($link, $filename) {
$extension = apk;
$mime = 'application/octet-stream';
// Generate the server headers
if( strstr( $_SERVER['HTTP_USER_AGENT'], "MSIE" ) ) {
header( 'Content-Type: "'.$mime.'"' );
header( 'Content-Disposition: attachment; filename='.$filename.'.'.$extension );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( "Content-Transfer-Encoding: binary" );
header( 'Pragma: public' );
} else {
header( "Pragma: public" );
header( "Expires: 0" );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Cache-Control: private", false );
header( "Content-Type: ".$mime, true, 200 );
header( 'Content-Disposition: attachment; filename='.$filename.'.'.$extension);
header( "Content-Transfer-Encoding: binary" );
}
readfile( $link );
}