php - Specific Array to CSV file -


i have problem sending data table csv file.

array  [link1] => http code [link2] => http code [link3] => http code [link4] => http code 

i need send data csv file links not recur. unfortunately, don't know how send link after link (i work in foreach loop) extract each of these links , send csv, , @ same time check did not show up.

this 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 $link_url)  {      $url = $link_url->loc;      $data=file_get_html($url);     $data = strip_tags($data,"<a>");     $d = preg_split("/<\/a>/",$data);      foreach ( $d $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 $fields) {                 fputcsv($fp, $fields);             }         }      } }       curl_close($ch);     fclose($fp);  echo 'send csv file completed ... '; 

i need every link .xml, download links on same page , specify http status. part have done. can't appropriate way send data csv file.

i'm counting on help.

the code below code few modifications. there observation :// not seem acceptable 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 $link_url){              $url    = $link_url->loc;             $data   = file_get_html($url);             $data   = strip_tags($data,"<a>");             $d      = preg_split("/<\/a>/",$data);              foreach ( $d $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) {                         // code on each iteration,                         // opening stream & closing again on each iteration...                         $http_code  = gethttpcodestatus($u);                          if(strpos($u, "https://www.gutscheinpony.de/") !== false ){                             $u = substr($u, 28);                         }                          if($u == "/") {                             $u = $url;                         }                         // bug... using :// part of array key seems not work                         $links[str_replace("://", "_", $u)] = $http_code;                          // run var_dump(), view process progresses if wish                         var_dump($links);                         $status = fputcsv($fp, array($u, $url , $http_code));                     }                  }             }         }           fclose($fp);         if($status) {             echo count($links) . ' entries processed , written disk csv file... ';         }else{             echo  'it seems entries not written disk  - @ least 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;         } 

Comments