I am using this code to check status code. of any URL.
public function callAPI($method, $url, $data = false, $headers = [])
{
$curl = curl_init();
if (count($headers) > 0) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
default:
if ($data) {
$url = sprintf("%s?%s", $url, http_build_query($data));
}
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return [
'result' => $result,
'statusCode' => $httpcode,
];
}
//this will send request to given URL and function will return response code and whole response.
$response = callAPI("GET",$url);
$responseCode = $response["statusCode"];
if($responseCode == '404'){
echo "404 not found";
}