douxin1956 2017-04-11 05:31
浏览 45
已采纳

如何从这种类型的链接下载文件?

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;
}
  • 写回答

1条回答 默认 最新

  • dongxuying7583 2017-04-11 07:10
    关注

    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 );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题