简体   繁体   中英

How to download files from this type of links?

I am developing an application to download some apps. But I can't cross the Google Play store authentication. So I just know the apps name and download from third party website.

I try to use get_file_content and curl , but I get a error response:

The Response content must be a string or object implementing __toString(), "boolean" given.

My code:

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;
}

Use this code to get 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');

for download file:

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 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM