Earlier this month, I wrote about finding the destination of a redirect using cURL in PHP. This can be a useful function to use whenever:

  • you know the URLs with which you’re dealing are going to redirect,
  • you know that the number of redirects will be limited to one.

Granted, in the latter case, it’s becoming more difficult because sites like, say, Twitter, have multiple redirects before you get to the destination.

But that’s a topic for another post (unless you just want to implement a recursive or iterative way of working through requests until you find the final destination).

Anyway, there’s another thing that can also be useful whenever you’re working with redirects and with cURL, and that’s determining if the specified URL takes you to a valid page.

Is the Specified URL a Valid Page?

The code in this post won’t be altogether different than what you may have seen in previous posts; however, the key things that we’re going to do are as follows:

  • initialize cURL with the URL that we’ve been provided,
  • set the CUROPT_RETURNTRANSFER value to true (which returns the value of the transfer as a string),
  • and then evaluate the HTTP status code that’s returned from the request.

cURL Options

You can read all about the codes on this site but the two we’re most concerned with, at least for this post, are 404 and 200.

Is the Specified URL a Valid Page?

So, first, the code:

And here’s how it works:

  1. the function accepts a URL as an argument,
  2. we initialize cURL with the proper settings, and then we execute the request,
  3. we read the value of the status code that’s stored from the respond,
  4. we close the cURL handle and then return the evaluation of the status code.

It seems simple enough, doesn’t it?

And, in general, it is. You may have to do a bit of tweaking on your own for your status code, or you may want to parse information out of the response. This can be done by parsing the string, using regular expressions, or more. But that’s beyond the scope of this post.

At the bare minimum, this will tell you if the URL to which you’re being taken is a valid page or not.