TestURL: a PHP script for . . . uh . . . testing URLs

So recently I’ve been having a lot of fun with the HTTP_Request PEAR module. It’s a lot like the cURL module but a bit easier to use, and with all the PEAR object goodness.

I had a little problem: I wanted to be able to test a series of URL’s and make sure they send back the right headers. My need was pretty specific. I wanted be able to plugin a list of URL’s and mkae the assertion that they will all return a response code of 302 (redirection). So I cooked up this little script that lets me do just that – and very cleanly I may add:
require "TestURL.class.php";

// Check a url to make sure its redirects

$suite = new TestURL("http://www.venturevoice.com/vv.xml",302);
$suite->test(true);
The test() function takes one parameter and that specifies whether to print the output of the tests to the screen. Otherwise, or no matter what, you can specify an email address and a log file where the logs will be sent. The mail function currently only emails you if there is an error.

You’re asking: “Why would I want it to email me if I could just see the output?”. The answer is very very simple – CRON. You can set up a cron job to run a series of URL tests on a schedule. This is the real purpose of this script. A way to check if all is well with your server/web application without having to be near a computer. Its a good way to be the first to know if your server goes down – before you start getting email from clients/customers. OBVIOUSLY you shouldn’t put this script on the same server that you’re monitoring.

Some other notes

The constructor can also take an array of urls instead of just one, making testing even easier:
// Check a couple urls to make sure they appear as MP3s

$mp3s = array('http://www.audiokio.com/download/Scenario45_-_Crack_Kills.mp3',
'http://www.audiokio.com/download/The_Educataz_-_Enjoy_the_Ride.mp3',
'http://www.audiokio.com/download/Zeps_and_AQ_-_Troopin.mp3');

$correct_headers = array('Content-Type' => 'audio/mpeg');

$suite = new TestURL($mp3s,200,$correct_headers);
$suite->test(true);
As you can see you can also test if it sends the correct headers on top of the response code.

Use this

Use this for whatever you want as long as it falls under the category of “GOOD” and not “EVIL”. If you use it for something awesome, or have any suggestions or ways to make it better, email me or leave a comment.

Download it

TestURL.class.php – zipped with an example

View the source

View the example

One Response to “TestURL: a PHP script for . . . uh . . . testing URLs”

Trishah Says: #

Nice bit of work!

I’m a novice php person, more an html gal, myself 😉

I found you because I have a client who needs to add a function to his site where people can test their links to make sure they don’t break frames. I know this function is out there on other sites but I don’t have access to the code. By any chance, would you know how to adapt this script for my purposes?

Or am I talking apples and oranges? Like I said, novice here.

About

QuirkeyBlog is Aaron Quint's perspective on the ongoing adventure of Code, Life, Work and the Web.

twitter/@aq.

instagram/@quirkey.

github/quirkey.

QuirkeyBlog is proudly powered by WordPress

Categories