繁体   English   中英

php-将csv文件直接下载到内存,最好没有cURL

[英]php - download a csv file directly to the memory, preferably without cURL

这等效于问题:将CSV直接下载到Python CSV解析器中,但是在php中。 基本上,我正在寻找一个提供接口的库,该接口只需几行代码即可完成我用python处理的事情:

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文件,但我想避免使用cURL及其底层接口。 有什么建议么?

不完全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,如果您真的不喜欢cURL ...

file_get_contents($url);

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

如果启用了fopen包装器,则可以使用file_get_contents将文件加载到变量中。 您可以使用str_getcsv解析结果。

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

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

您可以使用以下内容:

$content = file_get_contents($url);

应该启用fopen包装器

暂无
暂无

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

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