1
00:00:00,840 --> 00:00:04,790
XML used to be king when it came
to sharing data on the web, but

2
00:00:04,790 --> 00:00:09,960
XML is a lot more complicated and
people appreciate the ease of use of JSON.

3
00:00:09,960 --> 00:00:13,200
But XML is also a lot more detailed.

4
00:00:13,200 --> 00:00:16,790
You can convey a lot more
than just a simple object.

5
00:00:16,790 --> 00:00:21,130
As with so many things, one is not
necessarily better than the other.

6
00:00:21,130 --> 00:00:25,650
They're just different, because they're
different, they have different uses.

7
00:00:25,650 --> 00:00:29,410
You'll often find XML being used for
things such as our assessed feeds for

8
00:00:29,410 --> 00:00:31,340
blogs or podcasts.

9
00:00:31,340 --> 00:00:34,160
I thought this would be a great
opportunity to share some of my

10
00:00:34,160 --> 00:00:35,700
favorite podcasts.

11
00:00:35,700 --> 00:00:39,760
At the same time, we use some of
PHP's built in functionality for

12
00:00:39,760 --> 00:00:40,860
working with XML.

13
00:00:41,880 --> 00:00:46,240
Like I said before, XML can be a lot
more detailed and complicated.

14
00:00:46,240 --> 00:00:50,020
So, if you're trying to do anything very
complicated, I'd suggest you look for

15
00:00:50,020 --> 00:00:53,000
a package that will give
you a lot more features.

16
00:00:53,000 --> 00:00:55,768
See the notes attached to this video for
more information.

17
00:00:55,768 --> 00:00:58,550
Open podcasts.php.

18
00:00:58,550 --> 00:00:59,900
Once again.

19
00:00:59,900 --> 00:01:03,400
I've started by showing
the contents of an XML file.

20
00:01:03,400 --> 00:01:07,250
Since XML uses tags, just like HTML does,

21
00:01:07,250 --> 00:01:12,420
we need to use the htmlspecialchars
function for these tags to be visible.

22
00:01:12,420 --> 00:01:16,250
I have also included a sample podcast and
an episode.

23
00:01:16,250 --> 00:01:17,605
Let's preview this in a browser.

24
00:01:21,012 --> 00:01:26,010
We can see that the XML file has a lot
more details than the JSON file.

25
00:01:26,010 --> 00:01:30,920
So it's not quite as clean, but we have
a channel for the podcast with the details

26
00:01:30,920 --> 00:01:37,960
about the podcast itself, and then items
with details about individual episodes.

27
00:01:37,960 --> 00:01:41,517
Let's go back to work spaces,
remove the file display and

28
00:01:41,517 --> 00:01:44,866
replace the sample podcast
with the data from the XML.

29
00:01:52,405 --> 00:01:57,560
For this example, I have multiple
XML files, one for each podcast.

30
00:01:57,560 --> 00:02:01,570
Let's use what we learned in the first
section, to read the files from the XML

31
00:02:01,570 --> 00:02:05,870
directory, and put them into an array
first we'll create an empty array.

32
00:02:05,870 --> 00:02:07,385
We'll call it files.

33
00:02:11,047 --> 00:02:12,044
Then we'll set our directory.

34
00:02:21,104 --> 00:02:27,722
If Opendir,

35
00:02:27,722 --> 00:02:33,728
our directory And closedir.

36
00:02:39,886 --> 00:02:42,522
Now we'll loop through the directory.

37
00:02:42,522 --> 00:02:52,108
while (($entry=readdir($fh))

38
00:02:52,108 --> 00:02:55,670
!==false.

39
00:03:00,210 --> 00:03:02,869
I don't want any files
that start with a dot.

40
00:03:06,758 --> 00:03:12,726
If substring, the entry,
starting with the first character and

41
00:03:12,726 --> 00:03:19,707
polling one character Does not equal dot.

42
00:03:24,664 --> 00:03:26,876
Then we're going to add
this entry to the files.

43
00:03:33,040 --> 00:03:38,496
Directory slash entry.

44
00:03:38,496 --> 00:03:40,364
Now we can loop through each of our files.

45
00:03:44,763 --> 00:03:48,850
If not empty files.

46
00:03:51,708 --> 00:03:57,826
For each $files as $file.

47
00:03:57,826 --> 00:04:02,470
And then, we're going to use
a simplexml load file function,

48
00:04:02,470 --> 00:04:09,977
to parse the XML file into an object,
xml = simplexml_

49
00:04:09,977 --> 00:04:15,785
load_file(file).

50
00:04:15,785 --> 00:04:20,627
Now we're ready to replace the sample
podcast with the details from our

51
00:04:20,627 --> 00:04:21,362
XML file.

52
00:04:43,683 --> 00:04:44,508
First we add our link.

53
00:04:46,974 --> 00:04:51,305
XML channel link,

54
00:05:04,005 --> 00:05:13,970
Title, Description.

55
00:05:13,970 --> 00:05:16,570
Now I don't want to show every episode.

56
00:05:16,570 --> 00:05:19,062
So instead, I'm going to pull a random

57
00:05:19,062 --> 00:05:29,062
item.

58
00:05:35,161 --> 00:05:40,922
Make sure that we subtract one from the
count, since the rand range is inclusive.

59
00:05:52,230 --> 00:05:58,863
Item random, And then the title.

60
00:06:03,478 --> 00:06:05,774
Let's copy this for
the rest of our episode.

61
00:06:19,230 --> 00:06:25,549
Our audio source is a little tricky,
Enclosure,

62
00:06:28,429 --> 00:06:32,458
Attributes, And then, URL.

63
00:06:38,046 --> 00:06:40,330
And we'll use the link to
the podcast once more.

64
00:06:44,180 --> 00:06:50,459
Let's take a look at what we have so
far An error, let's go fix that.

65
00:07:02,788 --> 00:07:08,550
We have a list of podcasts with a random
episode to try, and a link to learn more.

66
00:07:08,550 --> 00:07:13,250
When we refresh, we get a different
episode for each podcast.

67
00:07:13,250 --> 00:07:15,280
There's one more thing
that I want to show you.

68
00:07:15,280 --> 00:07:16,624
So let's go back to workspaces.

69
00:07:18,257 --> 00:07:22,130
This is a great place to
demonstrate pulling external files.

70
00:07:22,130 --> 00:07:27,030
Any of the functions that read files
can read any file from anywhere.

71
00:07:27,030 --> 00:07:30,670
As long as you have access and
correct permissions.

72
00:07:30,670 --> 00:07:35,780
It doesn't matter if the files are local
to your computer, on a local network or

73
00:07:35,780 --> 00:07:36,760
even somewhere on the web.

74
00:07:38,050 --> 00:07:40,001
Let's add another of my favorite podcasts.

75
00:07:51,423 --> 00:07:52,190
Files.

76
00:08:09,249 --> 00:08:10,678
Now let's view this in the browser again.

77
00:08:16,472 --> 00:08:20,050
This new podcast was added,
right along with all the others.

