Retrieve Data from a Remote Webpage

PHP’s file() functions are great for opening, reading, writing to, and doing other dirty tricks with files. Any html or php page is of course a text file so we can open them and extract data in many different ways. Here are just a couple.

Retrieving meta tags from a remote webpage

PHP has a useful function called get_meta_tags that allows you to read meta data as an array and then extract certain elements.

As an example, the following code will snatch the meta data from www.waypoints.ws.

I use a variant of the get_meta_tags function to display page information as part of a preview function for fat.ly short URL service.

Retrieving title tags from a remote webpage

The get_meta_tags function will only parse data above the closing head tag in your page so it’s relatively quick. The function won’t read the title tag of your page – so to do that we open up the entire page and extract the desired text. This process is far more time consuming.

Using the same code above, we could extract any text element of a page between two defined and unique strings of text. This means you that you can effectively snatch portions of remote web pages for inclusion into your own site. Before you did anything remotely resembling this you would should ensure you have permission to do so. Not doing so is theft.

Marty is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Mailing List

First Name:

Email address:

Security code:

About the Author

Marty has been involved with online application development for a number of years and has worked with large scale applications both as a coder and project manager. He is currently actively contributing to 7 blogs and maintains over 300 websites.

BeliefMedia Pty Limited. BeliedMedia is a media company based in Sydney, Australia. We specialise in all facets of media, including: online marketing, podcast production, programming, software development, Android/iDevice development, voiceovers and television production. Make contact with us via our website.