note 118938 deleted from function.parse-url by cmb
| From: | cmb@php.net | Date: | Sun, 19 Jan 2020 08:19:57 +0000 |
| Subject: | note 118938 deleted from function.parse-url by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213898@lists.php.net to get a copy of this message | ||
Note Submitter: ivijan dot stefan at gmail dot com
----
If you need check if URL exists or not, here is one my good function for that.
<?php
// Return false or URL
function url_exists($url='')
{
if(empty($url)) return false;
$curl = curl_init($url);
//don't fetch the actual page, you only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,5);
curl_setopt($curl, CURLOPT_TIMEOUT , 2);
//do request
$result = curl_exec($curl);
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ((int)$statusCode === 200) return $url;
return false;
}
curl_close($curl);
return false;
}
if(url_exists("http://www.google.com")!==false)
{
// URL Exists
}
?>
This can help if you decide to do some dynamic URL parser or something where need validations.