0

I am trying to create some basic SEO testing tool, now I don't know how do I check if a remote website has a 404 page or not in php ? Something like this

<?php  
  $info404 = "";
  if(404 custome page exists){
    $info404 = " Good ";
  } 
  else{
    $info404 = " Bad ";
  }
?>

Thank you very much.

Zika
  • 9
  • 3
  • You can user curl_getinfo function – Pavel Třupek Dec 21 '20 at 13:41
  • All the answers I found here mainly relate to whether a specific ulr has a response of 200 or 400. Could I ask you to write me that piece of code or give me a link if you know. – Zika Dec 21 '20 at 13:49
  • https://stackoverflow.com/questions/3602631/how-to-check-if-a-url-exists-or-not-error-404-using-php and https://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php/17832110 check out this – Lets-c-codeigniter Dec 21 '20 at 13:52
  • the answers to this topic are based on whether a particular link has a response of 200 or 400. But i I need to know if the default url given by user has a custome 404 page. – Zika Dec 21 '20 at 14:02
  • Why does it matter? How do you define "custom"? (serious question) – Julian Reschke Dec 21 '20 at 14:36
  • The point is that users will generally type a valid URL into a input field. The code need to pick that url given by user ( exp. www.myworkingsite.com ) and test does that url have a custome 404 page or not. Like on this web site: https://www.seowebpageanalyzer.com/ – Zika Dec 21 '20 at 15:41

1 Answers1

0

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";
}
Rudra
  • 704
  • 8
  • 16
  • I'm pretty new with PHP, can you describe what this does or add comments to the code. I would be very grateful. – Zika Dec 21 '20 at 14:06
  • @Zika sure, I have added and example also added a comment what will this code do. – Rudra Dec 21 '20 at 14:12
  • This function works with the initial assumption that the url entered is not good. The point is that users will generally type a valid URL. The point is to test that valid with a status of 200 entered to test exm. www.myworkingsite.com have a custome 404 page or not. – Zika Dec 21 '20 at 14:24
  • @Zika Ohkay, Let me check for that. – Rudra Dec 21 '20 at 14:35