We have most of the podcast stored locally.
That means if a new episode comes out we have to update the XML data ourselves.
Yes, you could just link to the RSS feed like we did with full stack radio.
But this is a lot slower to process and, of course,
this course is about taking control.
Let's find out what it takes to control the XML data with PHP.
Create another file in the inc directory.
We'll name this one write_xml.
We'll start by loading the podcast file we wish to modify into an XML object.
We'll be using the data from the add_episode form.
Simplexml_load_file and our file.
We want to add an additional item.
So, we're going to call the add child method on the XML channel.
AddChild('item').
Then we can add our child details.
I'm going to create a new date object from the form input so
that I can format it properly.
Now, we can add the publication date.
Date -> format, the day name,
day, month, year, hour.
And then for the description, I'm going to copy the title again.
Let's write out what we have so far so we can preview this in the browser.
We can use the method asXML.
Simple XML doesn't really handle making the XML pretty or easy to read.
There are packages that can do this.
But we aren't really going to be looking at the code itself, so
it doesn't really matter.
It doesn't need line breaks to be valid XML.
But to make this a little easier for
us to read right now, let's add our own line breaks.
Anywhere the tags meet.
We're going to add a line break.
Now, let's preview this in a browser.
Let's pull the latest episode of Educate Yourself.
Add episode.
Choose the Educate Yourself.
And we'll enter the details.
Great, as you can see our new episode was added.
But there are a few more bits of information that I want to add.
XML offers many additional options for adding details.
I'm going to demonstrate one of them.
As you see in the previous episodes, there are some tags that start with iTunes and
a colon before the name of the tag.
This is the XML way of namespacing.
At the top of the file.
We see these xmlns for namespace.
And here's our iTunes namespace.
We'll copy this and go back to workspaces.
Itunes.
And we just need this URL.
Then we can add the namespace as the third parameter of the addChild method.
We'll copy this line for summary, explicit, and duration.
Now, let's preview these changes in the browser again.
Now, we see the iTunes information as well.
And we're ready to write this to a file.
Let's comment out this display.
Instead of echoing the asXML,
we can call xml->asXML and pass the file.
We'll then redirect.
To podcasts.
Back in the browser let's resubmit the form.
Since these are random, we can refresh until we see the new episode.
We could also specify a specific episode if we wanted too.