Php obtains the http status code.

Source: Internet
Author: User
Tags curl socket

It is often necessary to determine whether a file can be accessed. You can identify it by using the http status code. 200 indicates normal access, and 404 indicates that the page cannot be found. The code is as follows:

The code is as follows: Copy code

<? Php
// Set the url
$ Url = 'http: // ';
Function get_http_status_code ($ url ){
If (empty ($ url) return false;
$ Url = parse_url ($ url );
$ Host = isset ($ url ['host'])? $ Url ['host']: '';
$ Port = isset ($ url ['port'])? $ Url ['port']: '80 ';
$ Path = isset ($ url ['path'])? $ Url ['path']: '';
$ Query = isset ($ url ['query'])? $ Url ['query']: '';

$ Request = "HEAD $ path? $ Query HTTP/1.1rn"
. "Host: $ hostrn"
. "Connection: closern"
. "Rn ";

$ 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 ('', socket_read ($ socket, 1024 ));
Socket_close ($ socket );
Return trim ($ response [1]);
Echo get_http_status_code ($ url );

Another way to get the http status code

To use curl, you must enable it in php. ini> <on a Windows Server, open php. ini and find:
Extension = php_curl.dll
Remove the preceding annotations.


The code is as follows: Copy code
$ Curl = curl_init ();
$ Url = 'http: // ';
Curl_setopt ($ curl, CURLOPT_URL, $ url); // sets the URL
Curl_setopt ($ curl, CURLOPT_HEADER, 1); // Get the Header
Curl_setopt ($ curl, CURLOPT_NOBODY, true); // we just need Head
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // save the data to a string. Don't output it to the screen.
$ Data = curl_exec ($ curl); // start execution ~
Echo curl_getinfo ($ curl, CURLINFO_HTTP_CODE); // I know the HTTPSTAT code ~
Curl_close ($ curl); // when used up, remember to turn it off.

Some status code

1xx: request received, continue processing
2xx: The operation is successfully received, analyzed, and accepted.
3xx: The request must be further processed.
4xx: The request contains an error syntax or cannot be completed
5xx: The server failed to execute a fully valid request.

100 -- the customer must continue to send the request
101 -- the client requests the server to convert the HTTP protocol version according to the request

200 -- Transaction successful
201 -- prompt to know the URL of the new file
202 -- accept and process, but not complete
203 -- the returned information is uncertain or incomplete
204 -- the request is received, but the returned information is null.
205 -- when the server completes the request, the user agent must reset the file that has been browsed.
206 -- the server has completed some users' GET requests

300 -- the requested resources can be obtained in multiple places
301 -- delete request data
302 -- request data found at other addresses
303 -- we recommend that you access other URLs or access methods.
304 -- the client has executed GET, but the file has not changed
305 -- the requested resource must be obtained from the address specified by the server
306 -- code used in HTTP of the previous version, which is not used in the current version
307 -- declaring temporary deletion of requested resources

400 -- Incorrect request, such as syntax error
401 -- authorization request failed
402 -- retain valid ChargeTo header response
403 -- the request is not allowed
404 -- no file, query, or URl found
405 -- the method defined in the Request-Line field is not allowed.
406 -- the requested resource is inaccessible due to the user's Accept drag.
407 -- similar to 401, the user must first be authorized on the proxy server
408 -- the client did not complete the request within the specified time
409 -- the request cannot be completed due to the current resource status
410 -- this resource is no longer available on the server and there is no further reference address
411 -- the server rejects the user-defined Content-Length attribute request
412 -- one or more Request Header fields are incorrect in the current request
413 -- the requested resource is larger than the size allowed by the server
414 -- the requested resource URL is longer than the length allowed by the server
415 -- the requested resource does not support the format of the requested Project
416 -- the request contains the Range request header field, which has no range indication value in the current request resource Range.
It does not contain the If-Range request header field.
417 -- the server does not meet the expectation specified in the header field of the response Ct request. If it is a proxy server, it may be that the next-level server cannot meet the request

500 -- Internal error occurred on the server
501 -- the server does not support the requested function
502 -- the server is temporarily unavailable, sometimes to prevent system overload
503 -- server overload or service suspension
504 -- the gateway is overloaded. The server uses another gateway or service to respond to the user. The waiting time is long.
505 -- the server does not support or reject the specified HTTP version in the request header

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.