SoFunction
Updated on 2025-03-10

PHP's method of using curl to determine web page 404 (not exists) Original

This article example describes the method of using curl to judge web page 404 (not exists). Share it for your reference, as follows:

<?php
/* php uses curl to judge 404
  * Created on 2016-6-22
  * Writer
  */
 function chkurl($url){
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);//Set the timeout time    curl_exec($handle);
    //Check whether it is 404 (web page cannot be found)    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if($httpCode == 404) {
      return false;
    }else{
        return true;
    }
    curl_close($handle);
 }
 $url="https:///";
 if(chkurl($url)==true){
     echo "exist";
 }else{
     echo "Not exists";
 }
?>

The result of the operation is: does not exist

Supplement: For HTTP request header information, please refer to the online tools of this site:

HTTP status code collection:
http://tools./table/http_status_code

PS: Here I recommend a php formatting and beautification typeface tool on this site to help everyone type in code in future PHP programming:

PHP code online formatting and beautification tool:

http://tools./code/phpformat

In addition, since php belongs to the C language style, the following tool can also implement the formatting of php code:

C language style/HTML/CSS/json code formatting and beautification tools:
http://tools./code/ccode_html_css_json

For more information about PHP related content, please check out the topic of this site:Summary of PHP mathematical operation skills》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Complete collection of PHP array (Array) operation techniques》、《Summary of php sorting algorithm》、《Summary of common traversal algorithms and techniques for PHP》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of usage of php regular expressions》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.