简体   繁体   English

使用CURL进行下载而无需直接路径| www.url.com/things?download=文件

[英]Using CURL to download without a direct path | www.url.com/things?download=file

How does one download a file from a web page without a direct path to the file. 没有网页的直接路径,如何从网页下载文件。 For example a URL with GET information instead of the path. 例如,带有GET信息而不是路径的URL。 The code below seems to be downloading the actual page html instead of the file... 下面的代码似乎正在下载实际的页面html而不是文件...

Not sure what I'm doing wrong. 不知道我在做什么错。 I also would like to augment this to also perform on sites that require logins but I think I would just have to add 我也想扩大这一点,使其在需要登录的网站上也能执行,但我想我只需要添加

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password")

to the code? 的代码?

$output_filename = "advanced.exe";

$host = "http://download.cnet.com/Advanced-SystemCare-Free/3001-2086_4-10407614.html?hlndr=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://download.cnet.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);

$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);

The link you have there isn't the actual link to the file, only the page that initiates the download. 您所拥有的链接不是文件的实际链接,只有启动下载的页面。 By the looks of it, the page uses JavaScript to trigger the download, so you would want to dig through their code to find out exactly how they do it. 从外观上看,该页面使用JavaScript触发了下载,因此您希望深入研究他们的代码以确切地了解他们是如何进行的。 Then you can find the real URL to the file. 然后,您可以找到文件的真实URL。

A simple way, if you are doing this only for one file, would be to download the file in your browser, and then access the URL it used from the browser's download manager. 如果仅对一个文件执行此操作,一种简单的方法是在浏览器中下载该文件,然后从浏览器的下载管理器访问其使用的URL。 (In Firefox, for example, right click the file and choose "Copy Download Link") (例如,在Firefox中,右键单击该文件,然后选择“复制下载链接”)

I also would like to augment this to also perform on sites that require logins but I think I would just have to add ... 我也想扩大这一点,使其在需要登录的网站上也能执行,但我想我只需要添加...

That would work only for HTTP based authentication. 这仅适用于基于HTTP的身份验证。 If the site uses a traditional login form, this will not work. 如果该网站使用传统的登录表单,则将无法使用。 You'd have to submit several, sequential HTTP requests via CURL, using cookies to store the session state. 您必须使用Cookie来通过CURL提交多个顺序的HTTP请求,以存储会话状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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