如何使用php从.ashx页面下载文件? [英] How do I download a file from an .ashx page with php?

查看:147
本文介绍了如何使用php从.ashx页面下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从php中的此URL下载文件: http:/ /www.roblox.com/Asset/BodyColors.ashx?userId=36377783

I am trying to download a file from this url in php: http://www.roblox.com/Asset/BodyColors.ashx?userId=36377783

页面返回您的Web浏览器自动下载的文件。

The page returns a file your webbrowser automatically downloads.

我尝试使用cURL:

<?php
$uid = 36377783;

$xUrl = "http://www.roblox.com/Asset/BodyColors.ashx?userId=".$uid;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$xml = curl_exec($ch);
curl_close($ch);

echo $xml;

?>

但是它将我重定向到错误页面。

But it redirects me to an error page.

如何下​​载.ashx网址返回的文件?

How do I download the file the .ashx url returns?

(设置CURLOPT_USERAGENT无效。)

(Setting CURLOPT_USERAGENT doesn't work.)

推荐答案

有重定向-我使用 file_get_contents()(但为什么不卷曲)和 $ http_response_header

There is a redirection - i use file_get_contents() (but why not curl) and $http_response_header:

$uid = 36377783;

$xUrl = "http://www.roblox.com/Asset/BodyColors.ashx?userId=".$uid;

$opts = array(
  'http'=>array(
      'method'=>"GET",
      'follow_location' => true,
      'header'=>
        "Host: www.roblox.com\r\n" .
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0\r\n" .
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" .
        "Accept-Encoding: gzip, deflate\r\n" .
        "DNT: 1\r\n"

  )
);


$context = stream_context_create($opts);
$xml = file_get_contents($xUrl, false, $context);
#print_r($http_response_header);
$url_redirect = str_replace('Location: ',"",$http_response_header[5]);
#print $url_redirect;
$xml = file_get_contents($url_redirect);
#print_r($xml);
$roblox_responses = new SimpleXMLElement($xml);
print_r($roblox_responses);

这篇关于如何使用php从.ashx页面下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆