Skip to content

Instantly share code, notes, and snippets.

@dereknguyen269
Created June 24, 2020 13:17
Show Gist options
  • Save dereknguyen269/5918d08a203f14608b3568a764bf15ab to your computer and use it in GitHub Desktop.
Save dereknguyen269/5918d08a203f14608b3568a764bf15ab to your computer and use it in GitHub Desktop.
How To Check URL Is Working Or Not In Programing Languages?
$url = "http://www.domain.com/demo.jpg";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOBODY, true);
$result = curl_exec($curl);
if ($result !== false)
{
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 404)
{
echo "URL Not Exists"
}
else
{
echo "URL Exists";
}
}
else
{
echo "URL not Exists";
}
$url = "http://www.domain.com/demo.jpg";
$headers = @get_headers($url);
if(strpos($headers[0],'404') === false)
{
echo "URL Exists";
}
else
{
echo "URL Not Exists";
}
from urllib2 import urlopen
code = urlopen("https://kipalog.com").code
if code == 200:
print "Exists!"
# Or
import urllib2
ret = urllib2.urlopen('https://kipalog.com')
if ret.code == 200:
print "Exists!"
require 'net/http'
require 'open-uri'
def working_url?(url_str)
url = URI.parse(url_str)
Net::HTTP.start(url.host, url.port) do |http|
http.head(url.request_uri).code == '200'
end
rescue
false
end
#!/bin/bash
http_code=$(curl -I -s -o /dev/null -w "%{http_code}" "https://kipalog.com/")
if [ "$http_code" == "200" ]; then
echo "Exist!!!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment