2016-01-24 60 views
0

我試圖從這個URL下載一個文件在PHP:http://www.roblox.com/Asset/BodyColors.ashx?userId=36377783如何使用php從.ashx頁面下載文件?

該頁面返回一個文件,您的瀏覽器自動下載。

我試圖使用捲曲:

<?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; 

?> 

但它重定向我到一個錯誤頁面。

如何下載.ashx url返回的文件?

(設置CURLOPT_USERAGENT不起作用。)

回答

1

有一個重定向 - 我使用file_get_contents()(但爲什麼不捲曲)和$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);