这相当于问题:将CSV直接下载到Python CSV解析器中,但使用php。基本上,我正在寻找一个库,它提供的接口能够在几行代码中完成我用python做的事情:
h = httplib2.Http('.cache')
url = 'http://financials.morningstar.com/ajax/ReportProcess4CSV.html?t=' + code + '®ion=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 包装器