How do you check the url is valid or not in php?

In this post we will show you different method for test url, URL is exist or not exist(broken url or not).

Method 1 :

This is very fast method to test your url by using filter_var($your_url, FILTER_VALIDATE_URL); function.

NOTE : FILTER_VALIDATE_URL will not validate for like url hase the protocol ssh://, ftp:// etc..

Example:

// add your url. $your_url = "//www.onlinecode"; if (filter_var($your_url, FILTER_VALIDATE_URL) === FALSE) { // broken url. echo ('Not a valid URL'); } else { // Not broken url. echo ('valid URL'); }

Method 2 :

If we have to check image, pdf, txt or other type of url at that this method will help you lost. In this method we use fopen with rad methods and check any result is available.

Example:

$url_handle = @fopen('//www.onlinecode.org/example/www-onlinecode-org-image-test.png','r'); if(!$url_handle) { // broken url. echo ('Not a valid URL'); } else { // Not broken url. echo ('valid URL'); }

Method 3 :

In this method we use file_get_contents and check url exist or not. In this method, It is also work for image, pdf, txt or other type of url.

Example:

function check_url_exists($your_url) { if (@file_get_contents($your_url,false,NULL,0,1)) { return true; } return false; } // add your url. $your_url = "//www.onlinecode"; var_dump (check_url_exists($your_url));

Method 3 :

In this method we use curl it will work for any type of url.

// add your url. $your_url = "//www.onlinecode.org/example/www-onlinecode-org-image-test.png"; $connection = curl_init(); curl_setopt($connection, CURLOPT_URL, $your_url); // add url curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); curl_setopt($connection, CURLOPT_FOLLOWLOCATION, 1); $curl_result = curl_exec($connection); $curl_info = curl_getinfo($connection); if($curl_info['http_code'] == 404) { // broken url. echo ('Not a valid URL'); } else { // Not broken url. echo ('valid URL'); }

Method 4 :

This Method is advance for find url exist or not with HTTP status codes. In this method, it will show you result why url is not exist with HTTP status codes, example for 404 page not found.

function check_url_exists($your_url, array $set_options = array()) { if (empty($your_url)) { throw new Exception('URL is empty'); } // list of HTTP status codes $http_status_codes = array( 100 => 'Continue', // status codes 100 101 => 'Switching Protocols', // status codes 101 102 => 'Processing', // status codes 102 200 => 'OK', // status codes 200 201 => 'Created', // status codes 201 202 => 'Accepted', // status codes 202 203 => 'Non-Authoritative Information', // status codes 203 204 => 'No Content', // status codes 204 205 => 'Reset Content', // status codes 205 206 => 'Partial Content', // status codes 206 207 => 'Multi-Status', // status codes 207 208 => 'Already Reported', // status codes 208 226 => 'IM Used', // status codes 226 300 => 'Multiple Choices', // status codes 300 301 => 'Moved Permanently', // status codes 301 302 => 'Found', // status codes 302 303 => 'See Other', // status codes 303 304 => 'Not Modified', // status codes 304 305 => 'Use Proxy', // status codes 305 306 => 'Switch Proxy', // status codes 306 307 => 'Temporary Redirect', // status codes 307 308 => 'Permanent Redirect', // status codes 308 400 => 'Bad Request', // status codes 400 401 => 'Unauthorized', // status codes 401 402 => 'Payment Required', // status codes 402 403 => 'Forbidden', // status codes 403 404 => 'Not Found', // status codes 404 405 => 'Method Not Allowed', // status codes 405 406 => 'Not Acceptable', // status codes 406 407 => 'Proxy Authentication Required', // status codes 407 408 => 'Request Timeout', // status codes 408 409 => 'Conflict', // status code 409 410 => 'Gone', // status codes 410 411 => 'Length Required', // status codes 411 412 => 'Precondition Failed', // status codes 412 413 => 'Payload Too Large', // status codes 413 414 => 'Request-URI Too Long', // status codes 414 415 => 'Unsupported Media Type', // status codes 415 416 => 'Requested Range Not Satisfiable', // status codes 416 417 => 'Expectation Failed', // status codes 417 418 => 'I\'m a teapot', // status codes 418 422 => 'Unprocessable Entity', // status codes 422 423 => 'Locked', // status codes 423 424 => 'Failed Dependency', // status codes 424 425 => 'Unordered Collection', // status codes 425 426 => 'Upgrade Required', // status codes 426 428 => 'Precondition Required', // status codes 428 429 => 'Too Many Requests', // status codes 429 431 => 'Request Header Fields Too Large', // status codes 431 449 => 'Retry With', // status codes 449 450 => 'Blocked by Windows Parental Controls', // status codes 450 500 => 'Internal Server Error', // status codes 500 501 => 'Not Implemented', // status codes 501 502 => 'Bad Gateway', // status codes 502 503 => 'Service Unavailable', // status codes 503 504 => 'Gateway Timeout', // status codes 504 505 => 'HTTP Version Not Supported', // status codes 505 506 => 'Variant Also Negotiates', // status codes 506 507 => 'Insufficient Storage', // status codes 507 508 => 'Loop Detected', // status codes 508 509 => 'Bandwidth Limit Exceeded', // status codes 509 510 => 'Not Extended', // status codes 510 511 => 'Network Authentication Required', // status codes 511 599 => 'Network Connect Timeout Error' // status codes 599 ); $result_return = ""; $connection = curl_init($your_url); curl_setopt($connection, CURLOPT_NOBODY, true); curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true); if (isset($set_options['timeout'])) { $timeout = (int) $set_options['timeout']; curl_setopt($connection, CURLOPT_TIMEOUT, $timeout); } curl_exec($connection); $returned_status_code = curl_getinfo($connection, CURLINFO_HTTP_CODE); curl_close($connection); if (array_key_exists($returned_status_code, $http_status_codes)) { $result_return = " URL : '{$your_url}' - Error code: {$returned_status_code} - Definition: {$http_status_codes[$returned_status_code]}"; } else { $result_return = "'{$your_url}' does not exist"; } return $result_return; } // add your url. $your_url = "//www.onlinecode"; var_dump (check_url_exists($your_url));

Method 5 :

function check_url($test_url) { $ch_init = curl_init(); curl_setopt($ch_init, CURLOPT_URL, $test_url); curl_setopt($ch_init, CURLOPT_HEADER, 1); curl_setopt($ch_init , CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch_init); $headers_result = curl_getinfo($ch_init); curl_close($ch_init); return $headers_result['http_code']; } $test_url = "//onlinecode"; $check_url_result = check_broken_url($test_url); if ($check_url_result == '200') { echo "This is Working Link."; } else { echo "This is Broken Link."; }

How can I check if a URL is valid in PHP?

PHP FILTER_VALIDATE_URL Filter.
Example. Check if the variable $url is a valid URL: $url = "//www.w3schools.com"; ... .
Example 1. First remove all illegal characters from the $url variable, then check if it is a valid URL: ... .
Example 2. Here, the URL is required to have a query string to be valid:.

How do you check if a URL is valid or not?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

How validate URL in PHP regex?

Use the filter_var() function to validate whether a string is URL or not: var_dump(filter_var('example.com', FILTER_VALIDATE_URL)); It is bad practice to use regular expressions when not necessary.

How can I get current URL in PHP?

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

Postingan terbaru

LIHAT SEMUA