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 of examples.

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

The above code will return the following:

I use a variant of the get_meta_tags function to display page information as part of a preview function for my 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.

First, we open the page for reading. I’m using fopen() but you could just as easily use file_get_contents() .

Second, we extract the text between the title tags.

Retrieving any text data from a remote webpage

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. The code may require some modification.

Retrieving header information from a remote webpage

A developer can extract header information from a remote page using the following code:


If you require any assistance, please let me know.

Shortt URL for this post:


  1. thoma

    When i executes your code the following error occurs..
    This page contains the following errors:

    error on line 2 at column 1: Extra content at the end of the document
    Below is a rendering of the page up to the first error.

Leave a Reply

Your email address will not be published. Required fields are marked *