繁体   English   中英

使用fopen()编码问题解析PHP

[英]Parsing PHP with fopen() encoding issue

我在使用fopen()接收API数据时在php中解析csv文件时遇到问题。

当我使用以下1)中所述的URL在浏览器中显示csv文件时,我的代码有效。 但是我从以format = 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>";
   }
 ?>


输出:对于上面(2)中提到的URL:

root @ MorryServer:/#php testing.php

IU?Q?JL?。?/ Q?R ?? /)?J-.?))VH?/OM?K-NI?T0?P?*ͩT0204jzԴ?H ??? X ??? @ D ??


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

root @ MorryServer:/#php testing.php

PHP注意:未定义的偏移量:第24行的/testing.php中的1
头奖
€2,893,210

这是一个编码问题。

给定的文件包含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: gzip :在URL上使用fgetcsv显然不能处理gzip编码。 乱七八糟的字符串只是“文件”的压缩内容。

查看PHP的gzip lib,以便在解析之前先对其进行缩小。 证明:

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
        )

)

暂无
暂无

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

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