特定阵列到CSV文件

时间:2016-07-21 15:53:57

标签: php arrays csv http-status-codes

我将数据从表格发送到CSV文件时遇到问题。

Array

[link1] => HTTP Code
[link2] => HTTP Code
[link3] => HTTP Code
[link4] => HTTP Code

我需要将数据发送到CSV文件,以便链接不会重复出现。 不幸的是,我不知道如何在链接后发送链接(我在foreach循环中工作)以提取每个链接并将其发送到CSV,同时检查已经没有显示。

这是我的代码:

require('simple/simple_html_dom.php');
$xml = simplexml_load_file('https://www.gutscheinpony.de/sitemap.xml');
$fp = fopen('Links2.csv', 'w');
set_time_limit(0);

$links=[];

foreach ($xml->url as $link_url) 
{

    $url = $link_url->loc;

    $data=file_get_html($url);
    $data = strip_tags($data,"<a>");
    $d = preg_split("/<\/a>/",$data);

    foreach ( $d as $k=>$u ){
        if( strpos($u, "<a href=") !== FALSE ){
            $u = preg_replace("/.*<a\s+href=\"/sm","",$u);
            $u = preg_replace("/\".*/","",$u);

            if ( strpos($u, "http") !== FALSE) { 
                    $ch = curl_init($u);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $output = curl_exec($ch);
                    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

                    if(strpos($u, "https://www.gutscheinpony.de/") !== FALSE )
                        $u = substr($u, 28);

                        if($u == "/")
                            $u = $url;
                        }

            $links[$u] = $http_code;  

                $wynik = array( array($u, $url , $http_code));



            foreach ($wynik as $fields) {
                fputcsv($fp, $fields);
            }
        } 
    }
}


    curl_close($ch);
    fclose($fp);

echo 'Send to CSV file successfully completed ... ';

我需要从.xml获取每个链接,下载同一页面上的链接并指定HTTP状态。这部分我已经完成了。我不仅可以采用适当的方式将数据发送到CSV文件。

我指望你的帮助。

1 个答案:

答案 0 :(得分:0)

下面的代码基本上是您的代码,只有一些修改。还有观察结果表明://似乎不可接受作为PHP数组键的一部分。

    <?php

        require __DIR__ . '/simple/simple_html_dom.php';
        $xml        = simplexml_load_file('https://www.gutscheinpony.de/sitemap.xml');
        $fp         = fopen(__DIR__ . '/Links2.csv', 'w');
        set_time_limit(0);
        $links      = [];
        $status     = false;

        foreach ($xml->url as $link_url){

            $url    = $link_url->loc;
            $data   = file_get_html($url);
            $data   = strip_tags($data,"<a>");
            $d      = preg_split("/<\/a>/",$data);

            foreach ( $d as $k=>$u ){
                $http_code = 404;
                if( strpos($u, "<a href=") !== FALSE ){
                    $u = preg_replace("/.*<a\s+href=\"/sm","",$u);
                    $u = preg_replace("/\".*/","",$u);

                    if ( strpos($u, "http") !== FALSE) {
                        // JUST GET THE CODE ON EACH ITERATION,
                        // OPENING THE STREAM & CLOSING IT AGAIN ON EACH ITERATION...
                        $http_code  = getHttpCodeStatus($u);

                        if(strpos($u, "https://www.gutscheinpony.de/") !== FALSE ){
                            $u = substr($u, 28);
                        }

                        if($u == "/") {
                            $u = $url;
                        }
                        // THIS COULD BE A BUG... USING :// AS PART OF AN ARRAY KEY SEEMS NOT TO WORK
                        $links[str_replace("://", "_", $u)] = $http_code;

                        // RUN THE var_dump(), TO VIEW THE PROCESS AS IT PROGRESSES IF YOU WISH TO
                        var_dump($links);
                        $status = fputcsv($fp, array($u, $url , $http_code));
                    }

                }
            }
        }


        fclose($fp);
        if($status) {
            echo count($links) . ' entries were successfully processed and written to disk as a CSV File... ';
        }else{
            echo  'It seems like some entries were not successfully written to disk  - at least the last entry... ';                
        }

        function getHttpCodeStatus($u){
            $ch         = curl_init($u);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output     = curl_exec($ch);
            $http_code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            return $http_code;
        }