Home >Backend Development >PHP Tutorial >How to use curl to determine webpage 404 (does not exist) in PHP. Original, curl404_PHP tutorial
This article describes how PHP uses curl to determine web page 404 (does not exist). Share it with everyone for your reference, the details are as follows:
<?php /* php使用curl判断404 * Created on 2016-6-22 * Writer www.jb51.net */ function chkurl($url){ $handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);//设置超时时间 curl_exec($handle); //检查是否404(网页找不到) $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); if($httpCode == 404) { return false; }else{ return true; } curl_close($handle); } $url="http://www.bkjia.com/asdasd.html"; if(chkurl($url)==true){ echo "存在"; }else{ echo "不存在"; } ?>
The operation result is: does not exist
Supplement: For HTTP request header information, please refer to the online tool on this site:
HTTP status code list:
http://tools.jb51.net/table/http_status_code
PS: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:
php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat
In addition, since php belongs to the C language style, the following tool can also format php code:
C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json
Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP mathematical operation skills", "Summary of PHP operating office document skills (including word, excel, access, ppt)", "PHP array ( Array) operating skills collection", "php sorting algorithm summary", "php common traversal algorithms and techniques summary", "php data structure and algorithm tutorial", "php programming algorithm summary", "php regular expression usage summary", "Summary of PHP operations and operator usage", "Summary of PHP string usage" and "Summary of common PHP database operation skills"
I hope this article will be helpful to everyone in PHP programming.