How to use PHP to access and work with another website's HTML.
By Luke Johnson
Jun 23, 2019
5 minute read
Quick Summary ~ How to use PHP to access and work with another website's HTML.
Table of Contents
We're heading on a short road trip with our young kids. At 6 and nearly-3 years old, a nine-hour car ride is quite confining. But with a stack of "Adventures in Odyssey" episodes, the trip is much more fun for them! The radio show offers a bunch of free episodes for download. I took one look at the page and dreaded the thought of right-clicking "Save link as" for each of the 80+ links on the page. So, I wrote some code to make it easier.
You can use this setup for isolating content on any website. First I'll explain how this works, and then I'll include the code below.
How it works
Run the page's URL through a get_data() function that grab's the page's full HTML code.
Turn the page's HTML into an array, separating each line into a separate element. (This will allow you to target the contents of each line so you can keep or toss it, according to your search parameters.)
Loop through your $html array of HTML lines, removing stuff from the string you don't need.
Check each line for your search parameters so that you are left with relevant lines. (E.g., I only kept a line if '.mp3' was found within it.)
Run each line through a url_exists() function to make sure your list is made up of valid links. If it's valid, save it to a new array ($list).
Loop over the lines that made it into the $list array, and print them to the page. Now you can do anything you like with your shiny list of links!