Read external XML, save it in a variable and save locally.
If I pull XML data from a web server and then try writing the data to a file locally on my webserver do I need to do any encoding?
I do add a string to the xml data before I save it in a backup file.
Do I need to encode my variable if the original c开发者_运维技巧ontent was an XML document?
//Pull the xml data using file_get_contents
$url = "http://www.website.com/xml/current_obs/myfile.xml";
$xml_data = file_get_contents($url);
//Add Time Stamp to backup
$time = date('c');
$string = $xml_data;
$find = "</privacy_policy_url>";
$replace = "</privacy_policy_url><updated_date>". $time ."</updated_date>";
$xml_data = str_replace($find,$replace,$string);
//Create Backup file
$myFile = "data_backup.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$xml_data";
fwrite($fh, $stringData);
fclose($fh);
If I am understanding you correctly, the code you have should work fine. Are you experiencing any trouble with it?
精彩评论