简体   繁体   English

PHP 从使用 ASP 脚本的远程服务器下载文件

[英]PHP download file from remote server that's using ASP script

I'm trying to run my php script to download files off a remote server.我正在尝试运行我的 php 脚本以从远程服务器下载文件。 My script works fine when the remote url is pointing directly to the file, like " http://example.com/files/file.pdf ".当远程 url 直接指向文件时,我的脚本工作正常,例如“ http://example.com/files/file.pdf ”。

Problem is, most of the files are on a ASP server, and I don't have the url to the file path directly.问题是,大多数文件都在 ASP 服务器上,而我没有直接指向文件路径的 url。 Instead the ASP server is running a script to download the file.相反,ASP 服务器正在运行一个脚本来下载文件。 Those urls are like this: " http://example.com/files/GetFile.aspx?Id=2722&Autoresolve=True ".这些 url 是这样的:“ http://example.com/files/GetFile.aspx?Id=2722&Autoresolve=True ”。

So when I try reading the file content of the ASP server I get the literal markup of the page instead.因此,当我尝试读取 ASP 服务器的文件内容时,我得到的是页面的文字标记。

This is my code at the moment:这是我目前的代码:

$url = 'example.com/file.pdf';
$uploadfile = 'myserver.com/file.pdf';
$contents = file_get_contents($url);
$savefile = fopen($uploadfile, 'w');
fwrite($savefile, $contents);
fclose($savefile);

I can't seem to find any examples online for when the target file is being served by a script.当目标文件由脚本提供时,我似乎无法在网上找到任何示例。

Is this even possible and what do I need to look at?这甚至可能吗,我需要看什么?

most likely means that you need to have a cookie session prior to attempting the download.很可能意味着您需要在尝试下载之前进行 cookie 会话。 sometimes you must even initiate a download request and get an unique download url prior to actually doing the download.有时您甚至必须在实际进行下载之前发起下载请求并获得唯一的下载 URL。 and sometimes you even have to log in prior to doing the actual download.有时您甚至必须在进行实际下载之前登录。

you can use Fiddler Proxy to look at the download request made by your browser (most browsers (at least FireFox and Chrome) make it difficult to look at file download requests, hence the need for Fiddler proxy, even with the built-in dev tools), look for cookie session ids, or unique-looking tokens, sometimes they're in the cookies (like PHPSESSID), sometimes they're in the url (like /getFile.asp?id=123&token=qawsedrfgvbhnjm ), and sometimes they're in a HTTP header (like X-download-token: awsxdcfgbhn )您可以使用 Fiddler 代理查看浏览器发出的下载请求(大多数浏览器(至少是 FireFox 和 Chrome)很难查看文件下载请求,因此即使使用内置开发工具也需要 Fiddler 代理),查找 cookie 会话 ID,或外观独特的令牌,有时它们在 cookie 中(如 PHPSESSID),有时它们在 url 中(如/getFile.asp?id=123&token=qawsedrfgvbhnjm ),有时它们'在 HTTP 标头中(如X-download-token: awsxdcfgbhn

.. but since you didn't share the actual website you're trying to download from, nobody here can tell you exactly what your specific website needs. .. 但由于您没有分享您尝试下载的实际网站,这里没有人可以确切地告诉您您的特定网站需要什么。

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

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