2012-08-10 86 views
0

这相当于问题:Download CSV directly into Python CSV parser但在php中。基本上,我正在寻找一个能够正好在几行代码做的事情我做的Python库提供的接口:php - 直接下载csv文件到内存,最好不用cURL

h = httplib2.Http('.cache') 
url = 'http://financials.morningstar.com/ajax/ReportProcess4CSV.html?t=' + code + '&region=AUS&culture=en_us&reportType='+ report + '&period=12&dataType=A&order=asc&columnYear=5&rounding=1&view=raw&productCode=usa&denominatorView=raw&number=1' 
headers, data = h.request(url) 
return data 

我需要下载并解析csv文件,但我想避免使用卷曲和它的低级接口。有什么建议么?

+3

cURL怎么了? – 2012-08-10 07:12:49

+0

你如何预测下载它,带着希望和梦想? – 2012-08-10 07:14:32

+0

我认为你可以使用[fopen](http://www.php.net/manual/en/function.fopen.php)... – 2012-08-10 07:15:24

回答

3

不太3线:

function getURL($url){ 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $ret = curl_exec($ch); 
    curl_close($ch); 
    return $ret; 
} 

或者我想你可以使用的file_get_contents,如果你真的不喜欢卷曲......

file_get_contents($url); 

此外,看一看str_getcsv()为将上述函数中的字符串转换为CSV文件中数据的数组。

0

您可以使用以下方法:

$content = file_get_contents($url); 

fopen wrappers应启用

4

你可以,如果fopen封装已经启用了文件加载到一个变量file_get_contents。你可以用str_getcsv解析结果。

$data = str_getcsv(file_get_contents('http://example.com/data/my.csv')); 

如果您需要设置特定的标题或使用其他的分隔符,请参阅手册。