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.