如何从PHP中的URL解析CSV数据

时间:2019-03-18 09:18:05

标签: php csv parsing

从URL解析csv数据时遇到问题。

网址为:http://www.tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=71672399601682259

我想获取数据以便能够使用它。

这是我的代码:

$url="http://www.tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=71672399601682259"
var_dump(file_get_contents($url));

并获得结果

�ɟ�\��S�n�0}��?�&;�s��IUa+-U�n��#s�@Y���ć�ˮ����E��G�ө(��Z]o��c�v�T7_���VJ���]nji�Ky�Α|���~\���ͷ��)x��J ��y��`��rl܂� a��d�e�:�dZ�����@��@㿰�Ҙc"���LT0�.�e`�|�ȱ�n��������)@�1�G�c�yF� �8nyУ�q̇aL��w^�D<���uh�V 偆�̡T�-d�Y-�wfBK)�֝��� F3�����έ@L6�(Y�LGa��P�5�A� ��1Xo�s (Ɍ����d<:�Q��y(�%bo������K��

当我第一次从URL下载名称为“ Etemad.Parsian.ETF.csv”的文件,然后使用此代码时:

$file_path="Etemad.Parsian.ETF.csv"
var_dump(file_get_contents($file_path));

获取结果

<TICKER>,<DTYYYYMMDD>,<FIRST>,<HIGH>,<LOW>,<CLOSE>,<VALUE>,<VOL>,<OPENINT>,<PER>,<OPEN>,<LAST>
Servat.Parsian.ETF,20190317,26000.00,26298.00,26000.00,26224.00,422989000,16130,13,D,26002.00,26298.00
Servat.Parsian.ETF,20190316,26035.00,26035.00,25975.00,26002.00,57205000,2200,2,D,25839.00,25975.00
Servat.Parsian.ETF,20190313,25850.00,25850.00,25820.00,25839.00,49093700,1900,4,D,25765.00,25850.00
Servat.Parsian.ETF,20190312,25765.00,25765.00,25765.00,25765.00,38647500,1500,1,D,25616.00,25765.00
Servat.Parsian.ETF,20190311,25617.00,25617.00,25611.00,25616.00,64039500,2500,2,D,25369.00,25611.00
Servat.Parsian.ETF,20190310,25400.00,25400.00,25369.00,25369.00,509669940,20090,6,D,25573.00,25369.00
Servat.Parsian.ETF,20190309,25570.00,25578.00,25570.00,25573.00,37847720,1480,4,D,25439.00,25571.00
Servat.Parsian.ETF,20190306,25213.00,25645.00,25213.00,25439.00,329948250,12970,5,D,25316.00,25630.00
Servat.Parsian.ETF,20190305,25235.00,25504.00,25235.00,25316.00,1267056000,50050,9,D,25324.00,25397.00
Servat.Parsian.ETF,20190304,25324.00,25324.00,25324.00,25324.00,27856400,1100,3,D,25400.00,25324.00

您能帮我直接从URL 获取而不是保存到文件吗?

另外,当我向网址服务器发送请求以通过此标头将CSV文件发送给我时:

Cache-Control →public, max-age=900
Content-Type →text/plain; charset=utf-8
Content-Encoding →gzip
Expires →Mon, 18 Mar 2019 08:48:05 GMT
Last-Modified →Mon, 18 Mar 2019 08:33:05 GMT
Vary →*
Server →Microsoft-IIS/10.0
content-disposition →attachment;filename=Servat.Parsian.ETF.csv
X-Powered-By →ASP.NET
Date →Mon, 18 Mar 2019 08:33:05 GMT
Content-Length →7519

我是从邮递员那里得到的。

1 个答案:

答案 0 :(得分:0)

使用MYDECODER()兄弟:

<?php
$url="http://www.tsetmc.com/tsev2/data/Export-txt.aspx?t=i&a=1&b=0&i=71672399601682259";
$site = file_get_contents($url);
var_dump( MYDECODER($site));
function MYDECODER($d){
$f=ord(substr($d,3,1));
$h=10;$e=0;
if($f&4){
    $e=unpack('v',substr($d,10,2));
    $e=$e[1];$h+=2+$e;
}
if($f&8){
    $h=strpos($d,chr(0),$h)+1;
}
if($f&16){
    $h=strpos($d,chr(0),$h)+1;
}
if($f&2){
    $h+=2;
}
$u = gzinflate(substr($d,$h));
if($u===FALSE){
    $u=$d;
}
return $u;
}
?>