How to download file Using PHP that has delayed force download?

落爺英雄遲暮 提交于 2020-08-06 07:09:21

问题


I am at a situation, where I need to download files from the URL, it is easy with the direct file URLs like https://somedomain.com/some-path/somefile.exe

file_put_contents( $save_file_loc, file_get_contents($url_to_download);

But what to do when you have delayed force download from the URL which actually prints HTML and how to differentiate those URL?

Example URL: https://filehippo.com/download_mozilla-firefox-64/post_download/

EDIT: On above url the file download starts using JS, as I tested with blocking JS and download did not start.

Thanks in advance for your help.


回答1:


  1. Read the html of the URL using file_get_contents
  2. Find the URL of the file within the HTML. You'll have to visit the page and view source to locate the URL. In your example of https://filehippo.com/download_mozilla-firefox-64/post_download/ it's found in between data-qa-download-url="https://dl5.filehippo.com/367/fb9/ef3863463463b174ae36c8bf09a90145/Firefox_Installer.exe?Expires=1594425587&Signature=18ab87cedcf3464363469231db54575665668c4f6&url=https://filehippo.com/download_mozilla-firefox-64/&Filename=Firefox_Installer.exe"
  3. As you may have noticed, the page may have pre-approved the request so it's not guaranteed to work if the host has checks using cookies or other methods.
  4. Create a regex based on the above to extract the URL using preg_match
  5. Then file_get_contents the URL of the file to download it.


来源:https://stackoverflow.com/questions/62835395/how-to-download-file-using-php-that-has-delayed-force-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!