Làm cách nào để kiểm tra sự tồn tại của URL trong PHP? (ok)
https://www.geeksforgeeks.org/how-to-check-the-existence-of-url-in-php/
How to check the existence of URL in PHP?
Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn’t exist.
Used Functions:
get_headers() Function: It fetches all the headers sent by the server in response to the HTTP request.
strpos() Function: This function is used to find the first occurrence of a string into another string.
Example 1: This example checks for the status code 200 in response header. If the status code is 200, it indicates URL exist otherwise not.filter_none
brightness_4
|
Output:
Example 2: This example checks for the status code 404 in response header. If the status code is 404, it indicates URL doesn’t exist otherwise URL exist. filter_none
brightness_4
|
Output:
Example 3: This example uses curl_init() method to check the existence of an URL.filter_none
brightness_4
|
Last updated