Skip to content

Instantly share code, notes, and snippets.

@cuheguevara
Created April 17, 2013 09:17
Show Gist options
  • Save cuheguevara/5402946 to your computer and use it in GitHub Desktop.
Save cuheguevara/5402946 to your computer and use it in GitHub Desktop.
PHP XML Parser
<?php
function curl_get_html($options, $file) {
$ch = curl_init();
foreach ($options as $key => $value) {
curl_setopt($ch, $key, $value);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
@unlink($file);
$fp = @fopen($file, 'w');
fwrite($fp, $server_output);
fclose($fp);
return $file;
}
$url = array(
"http://address1.com/sitemap.xml",
"http://address2.com/site.xml",
"http://address3.com/sitemap.xml"
);
$inputFile = 'input.html';
$outputFile = 'output.html';
if (!file_exists($inputFile)){
$fp = fopen($inputFile, 'w');
fclose($fp);
}else{
unlink($inputFile);
$fp = fopen($inputFile, 'w');
fclose($fp);
}
if (!file_exists($outputFile)){
$fp = fopen($outputFile, 'w');
fclose($fp);
}else{
unlink($outputFile);
$fp = fopen($inputFile, 'w');
fclose($fp);
}
foreach ($url as $page) {
$options = array(CURLOPT_URL => $page,CURLOPT_POST => 0);
if (!file_exists($inputFile)){
$fp = fopen($inputFile, 'w');
fclose($fp);
}
// CURL XML to local file
$output = curl_get_html($options, $inputFile);
// load & extract local file
$xml = simplexml_load_file($output);
$fp = @fopen($outputFile, 'a');
foreach ($xml as $value) {
//echo $value->loc . " \n";
// extract node into text a line
fwrite($fp, $value->loc . " \n");
}
fclose($fp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment