In this article we will see how to check the existence of the URL using PHP. Here existence means we are checking content which we are requesting is available on server or not.
We can check the existence of URL in two ways using PHP. First one is the get_header function and second one is CURL.
Basically we are checking HTTP header of the URL and based on that we can determine existence of the url. 200 Code stands for OK header and 404 stands for Not Found.
Let’s get into both of the methods.
Using this get_headers function we can get the HTTP header information of the given URL.
Note: If you set second parameter of the get_headers() to true then you will get result in associative array. Just try for it.
Note: We have used CURLOPT_NOBODY to just check for the connection and not to fetch whole body.
So we are done with the checking the existence of the URL with PHP, share your views/comments/suggestions.
Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.
View all posts by: Avinash