2015-07-11 131 views
0

我在使用fopen()接收API数据时,在解析php中的csv文件时出现问题。使用fopen()编码问题解析PHP

我的代码在我使用在浏览器中显示csv文件的URL时起作用,如1)下面所述。但是我得到一个从格式= csv结尾的URL输出的随机字符,如2)所示。

1)工作网址:返回期望的值 https://www.kimonolabs.com/api/csv/duo2mkw2?apikey=yjEl780lSQ8IcVHkItiHzzUZxd1wqSJv

2)不工作的网址:返回随机字符 https://www.parsehub.com/api/v2/projects/tM9MwgKrh0c4b81WDT_4FkaC/last_ready_run/data?api_key=tD3djFMGmyWmDUdcgmBVFCd3&format=csv

这里是我的代码: - 使用URL(2)上述

<?php 

$f_pointer=fopen("https://www.parsehub.com/api/v2/projects/tM9MwgKrh0c4b81WDT_4FkaC/ last_ready_run/data?api_key=tD3djFMGmyWmDUdcgmBVFCd3&format=csv","r"); 


while(! feof($f_pointer)){ 
    $ar=fgetcsv($f_pointer); 


echo $ar[1]; 


echo "<br>"; 
    } 
?> 


输出:对于提到URL (2)以上:

根@ MorryServer:/#PHP testing.php

?IU?Q?JL?。?/ Q?R ?? /??J-。?)?V????????????????????????????????? ?? @ D ?? K


正确的输出:如果我使用URL类型如(1)

根@ MorryServer指出:/#PHP testing.php

PHP通知:未定义偏移:1 /上线24 testing.php
头奖

+0

先用什么样子的URL返回数据像一个标题作为第一个价值。这是无效的CSV(或至少非常规),所以PHP的CSV功能将有一个问题。第二个网址似乎会返回有效的CSV值。而不是使用'fopen'(你不应该使用它;它是一个低级函数,只有在你必须使用它的时候才能使用),你应该使用像'str_getcsv'这样的东西,这个。 –

+0

嗨Sverri,第一个网址是好的,我可以删除标题,问题是2)奇怪的输出。我需要代码为parsehub url工作 –

+0

如果你运行这个命令,你会得到什么:'php -r“echo mb_internal_encoding();”'第二个URL看起来非常好。它返回一个mimetype为'text/csv',用'UTF-8'编码的文件。略微老版本的PHP默认使用'iso-8859-1'。较新的版本使用'utf-8'。什么是你的PHP版本? –

回答

1

这是一个encodi问题。

给定文件包含UTF-8字符。这些由fgetcsv函数读取,这是二进制安全的。行尾是Unix格式(“\ n”)。

终端上的输出被打乱。纵观发送的头,我们可以看到:

GET https://www.parsehub.com/api/v2/projects/tM9MwgKrh0c4b81WDT_4FkaC/last_ready_run/data?api_key=tD3djFMGmyWmDUdcgmBVFCd3&format=csv --> 200 OK 
Connection: close 
Date: Sat, 11 Jul 2015 13:15:24 GMT 
Server: nginx/1.6.2 
Content-Encoding: gzip 
Content-Length: 123 
Content-Type: text/csv; charset=UTF-8 
Last-Modified: Fri, 10 Jul 2015 11:43:49 GMT 
Client-Date: Sat, 11 Jul 2015 13:15:23 GMT 
Client-Peer: 107.170.197.156:443 
Client-Response-Num: 1 
Client-SSL-Cert-Issuer: /C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO RSA Domain Validation Secure Server CA 
Client-SSL-Cert-Subject: /OU=Domain Control Validated/OU=PositiveSSL/CN=www.parsehub.com 

心灵的Content-Encoding: gzipfgetcsv上的URL的工作并不明显gzip的处理encosing。 scrumbled字符串只是“文件”的gzip内容。

看看PHP的gzip库,首先在解析它之前解压。 证明:

srv:~ # lwp-download 'https://www.parsehub.com/api/v2/projects/tM9MwgKrh0c4b81WDT_4FkaC/last_ready_run/data?api_key=tD3djFMGmyWmDUdcgmBVFCd3&format=csv' data 
123 bytes received 
srv:~ # file data 
data: gzip compressed data, was "tcW80-EcI6Oj2TYPXI-47XwK.csv", from Unix, last modified: Fri Jul 10 11:43:48 2015, max compression 
srv:~ # gzip -d < data 
"title","jackpot" 
"Lotto Results for Wednesday 08 July 2015","€2,893,210" 

要得到正确的输出,微小的变化都需要:只需添加一个流包装:

<?php 

     $f_pointer=fopen("compress.zlib://https://www.parsehub.com/api/v2/projects/tM9MwgKrh0c4b81WDT_4FkaC/last_ready_run/data?api_key=tD3djFMGmyWmDUdcgmBVFCd3&format=csv","r"); 

     if ($f_pointer === false) 
       die ("invalid URL"); 

     $ar = array(); 
     while(! feof($f_pointer)){ 
       $ar[]=fgetcsv($f_pointer); 
     } 

     print_r($ar); 

?> 

输出:

Array 
(
    [0] => Array 
     (
      [0] => title 
      [1] => jackpot 
     ) 

    [1] => Array 
     (
      [0] => Lotto Results for Wednesday 08 July 2015 
      [1] => €2,893,210 
     ) 

) 
+0

非常感谢!这是一整天失去了这个问题:) - 一杯茶的时间:) –