Author
By Luke Johnson
Date
Jun 23, 2019
Reading Time
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

    1. Run the page's URL through a get_data() function that grab's the page's full HTML code.
    2. 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.)
    3. Loop through your $html array of HTML lines, removing stuff from the string you don't need.
    4. 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.)
    5. 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).
    6. 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!

    Okay, that's the theory. Here's the code:

    Here is what the code outputs:

    • http://media.focusonthefamily.com/aio/mp3/aiopodcast163.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcastpeaceonearth.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast57.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast137.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast22.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast129.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast37.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast180.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast69.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast20.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcastamazinggrace.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast241.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast103.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast111.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast76.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast77.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast53.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast85.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast24.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast27.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast267.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast61.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast215.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast281.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcastcheatercheated.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcastkidsboro1.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcastkidsboro2.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcastkidsboro3.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast127.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast89.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast155.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast188.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast189.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast202.mp3
    • http://media.focusonthefamily.com/aio/mp3/aiopodcast225.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_033_The_Day_Independence_Came.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_080_A_Prisoner_for_Christ.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_149_By_Dawns_Early_Light.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_153_Mayor_for_a_Day.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_161_Isaac_the_True_Friend.mp3
    • http://media.focusonthefamily.com/aio/mp3/aio-the-star-pt1.mp3
    • http://media.focusonthefamily.com/aio/mp3/aio-the-star-pt2.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_184_Isaac_the_Chivalrous.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_209_Columbus_The_Grand_Voyage.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_253_A_Time_for_Christmas.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_259_The_Potential_in_Elliot.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_383_BTV_Thanks.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_385_Its_a_Pokenberry_Christmas_Part_1_of_2.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_386_Its_a_Pokenberry_Christmas_Part_2_of_2.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_392_When_in_Doubt_Pray.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_402_Arizona_Sunrise.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_404_Hide_and_Seek.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_416_The_Tower.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_562_The_Last_I_Do.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_601_Bernard_and_Jeremiah.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_608_Run_of_the_Mill_Miracle.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_625_Something_Significant.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_TTC06_Stepping_Stones_Part_01.mp3
    • http://media.focusonthefamily.com/aio/mp3/AIO_TTC07_Stepping_Stones_Part_02.mp3
    • http://media.focusonthefamily.com/free-downloads/mp3/AIO_01_777_First_Things_First.mp3

    Have fun experimenting with other websites' HTML!

    Web advice in your inbox