public function download_file($link='https://apkpure.co/apk/com.ezfun.p1767.dns_gp1/00b97d3d32a4f8e6cbecff7da5a4af41/1.0/0dfa1365e8e1c70bf2dcf391350a7a77/', $filename='com.ezfun.p1767.dns_gp1_1.0'){
$extension = 'apk';
$mime = 'application/octet-stream';
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 );
}
我需要readfile下载一个apk文件,但是我无法直接访问这个网站,我需要一个代理。
任何建议?
答案 0 :(得分:0)
使用此代码使用curl代理下载文件:
function download_file($url, $filename) {
$proxy = 'Proxy IP ADDRess'; //128.114.63.15:3128
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($handle, CURLOPT_PROXY, $proxy);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($handle);
curl_close($handle);
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Cache-Control: private', false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="' . $filename . '.apk"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($response)); // provide file size
header('Connection: close');
echo $response;
}
答案 1 :(得分:0)
我发现readfile()不支持绑定代理,如果需要,必须找到一种在base中设置代理的方法。有人说使用apache,但我没有做过。
最后另一个函数file_get_contents()解决了我的问题。
这是我的代码:
public function download_file($link='https://apkpure.co/apk/com.ezfun.p1767.dns_gp1/00b97d3d32a4f8e6cbecff7da5a4af41/1.0/0dfa1365e8e1c70bf2dcf391350a7a77/', $filename='com.ezfun.p1767.dns_gp1_1.0'){
$aContext = array(
'http' => array(
'proxy' => 'tcp://192.168.10.3:18118',
'request_fulluri' => true,
),
);
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents("https://apkpure.co/apk/com.wepie.snakeoff/6fbde5c9d65e72d4d014f9bf654bfe47/2.0/984e0be4d6a84965544692bc30487837/", False, $cxContext);
$path = file_put_contents(public_path().'/application.snakeoff.apk', $sFile);
echo $path;
}
file_put_content将返回写入文件的字节数,如果失败则返回FALSE。