0

I have a problem with sending data from a table to a CSV file.

Array

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

I need to send the data to a CSV file so that the links do not recur. Unfortunately, I don't know how to send link after link (I work in a foreach loop) to extract each of these links and send it to CSV, and at the same time check that already did not show up.

This is my code:

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 ... ';

I need get every link from .xml, download links that are on the same page and specify the HTTP status. This part I have done. I can't only appropriate way to send data to a CSV file.

I'm counting on your help.

Barry
  • 3,303
  • 7
  • 23
  • 42
  • Try to remove one array() from `$wynik = array( array())` before using fputscsv. so just once `$wynik = array()` is given – JustOnUnderMillions Jul 21 '16 at 16:02
  • Can't you just put the fputcsv() foreach loop after the link processing loop, i.e outside it? $wynik will of course grow bigger, so it's provided you have the memory for it. Also make it an assocative array, with $url as the key. That way each$url value will only be written once. – Torbjörn Stabo Jul 21 '16 at 16:03

1 Answers1

0

The code below is essentially your code with a few modifications. There was also the observation that :// does not seem acceptable as part of PHP Array Keys.

    <?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;
        }
Poiz
  • 7,611
  • 2
  • 15
  • 17