Skip to content

Instantly share code, notes, and snippets.

@AndreKelling
Created March 1, 2019 14:27
Show Gist options
  • Save AndreKelling/ba370266da3c18cd527401df5673fd68 to your computer and use it in GitHub Desktop.
Save AndreKelling/ba370266da3c18cd527401df5673fd68 to your computer and use it in GitHub Desktop.
fetch XML doc from URL and ecnode to json file in build dir
<?php
// fetch XML doc from URL and ecnode to json file in build dir
$jobsUrl = "https://xml-source";
$fileName = "jobs.json";
$fileDist = __DIR__."/../build/".$fileName;
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$response = get_content($jobsUrl);
$xml=simplexml_load_string($response) or die("Error: Cannot create object");
$json = json_encode($xml);
file_put_contents($fileDist, $json);
if(!file_put_contents($fileDist, $json)){
print "The ".$fileName." file could not be created.";
if(file_exists ($fileName))
unlink($fileName);
return;
}
$fileSize = floor(filesize($fileDist) / 1024 ). " KB";
echo "\e[0;32m ".$fileName." \e[0m (".$fileSize.") file in build dir saved! \r\n";
//$array = json_decode($json,TRUE);
//
//print_r($array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment