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.
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.
- the function accepts a URL as an argument,
- we initialize cURL with the proper settings, and then we execute the request,
- we read the value of the status code that’s stored from the respond,
- we close the cURL handle and then return the evaluation of the status code.