search
Homephp教程php手册php获取http状态码程序代码

经常需要判断文件是否可以访问,可以通过http状态码判别,200为正常访问,404为找不到该页面,代码如下:

<?php
// 设置url 
$url = &#39;http://www.phprm.com&#39;; 
function get_http_status_code($url) { 
 if(emptyempty($url)) return false; 
 $url = parse_url($url); 
 $host = isset($url[&#39;host&#39;]) ? $url[&#39;host&#39;] : &#39;&#39;; 
 $port = isset($url[&#39;port&#39;]) ? $url[&#39;port&#39;] : &#39;80&#39;; 
 $path = isset($url[&#39;path&#39;]) ? $url[&#39;path&#39;] : &#39;&#39;; 
 $query = isset($url[&#39;query&#39;]) ? $url[&#39;query&#39;] : &#39;&#39;; 
 
 
 $request = "HEAD $path?$query HTTP/1.1\r\n" 
   ."Host: $host\r\n" 
   ."Connection: close\r\n" 
   ."\r\n"; 
 
 $address = gethostbyname($host); 
 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
 socket_connect($socket, $address, $port); 
 
 socket_write($socket, $request, strlen($request)); 
 
 $response = split(&#39; &#39;, socket_read($socket, 1024)); 
 socket_close($socket); 
 return  trim($response[1]); 
} 
echo get_http_status_code($url); 
?>

另一种获取http状态码的办法

使用curl需要在php.ini中设置启用才行 >

extension=php_curl.dll

去掉前面的注释既可.

PHP实例代码如下:

<?php
$curl = curl_init(); 
$url=&#39;http://www.phprm.com&#39;; 
curl_setopt($curl, CURLOPT_URL, $url); //设置URL 
curl_setopt($curl, CURLOPT_HEADER, 1); //获取Header 
curl_setopt($curl,CURLOPT_NOBODY,true); //Body就不要了吧,我们只是需要Head 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //数据存到成字符串吧,别给我直接输出到屏幕了 
$data = curl_exec($curl); //开始执行啦~ 
echo curl_getinfo($curl,CURLINFO_HTTP_CODE); //我知道HTTPSTAT码哦~ 
curl_close($curl); //用完记得关掉他  
?>


本文地址:

转载随意,但请附上文章地址:-)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor