Change language

How do I check for the existence of a URL in PHP?

Functions used:
  • The get_headers() function:retrieves all headers sent by the server in response to an HTTP request.
  • strpos() function: this function is used to find the first occurrence of a string in another string.
Example 1.This example checks for status code 200 in the response header. If the status code is 200, it means the URL exists, otherwise it doesn’t.  
// Initialize the URL for the variable $url = " https://www.engineerforengineer.org " ;  
// Using the get_headers() function $headers = @get_headers ( $url );  
// Use a condition to check if a URL exists if ( $headers & amp; & amp; strpos ( $headers [0], ’200’ )) { $status = " URL Exist " ; } else { $status = "URL Doesn’t Exist" ; }  
// Show result echo ( $status );  
?>
Output:
URL Exist 
Example 2:This example checks the 404 status code in the response header. If the status code is 404, it means the URL does not exist, otherwise the URL exists.  
// Initialize the URL for the variable $url = " https://www.engineerforengineer.org " ;  
// Using the get_headers() function $headers = @get_headers ( $url );  
// Use a condition to check if a URL exists if ( $headers || strpos ( $headers [0], ’404’ )) { $status = "URL Doesn’t Exist" ; } else { $status = "URL Exist" ; }  
// Show result echo ( $status );  
?>
Output:
URL Doesn’t Exist 
Example 3:This example uses the curl_init() method to check if a URL exists.  
// Initialize URL for variable $url = " https://www.engineerfengineer.org " ;  
// Use the curl_init() function to initialize the cURL session $curl = curl_init ( $url );  
// Use curl_setopt() to set the option to pass cURL curl_setopt ( $curl , CURLOPT_NOBODY, true );  
// Use curl_exec() to execute a cURL session $result = curl_exec ( $curl ); if ( $result ! == false) { // Use curl_getinfo() to get information // relative to a specific translation $statusCode = curl_getinfo ( $curl , CURLINFO_HTTP_CODE); if ( $statusCode == 404) { echo "URL Doesn’t Exist" ; } else { echo "URL Exist" ; } } else { echo "URL Doesn’t Exist" ; }  
?>
Output:
URL Doesn’t Exist 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method