Core PHP Programming - Learn about the HTTP protocol and analog HTTP request

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/yangmolulu/article/details/91474876

HTTP protocol

table of Contents

HTTP protocol

HTTP protocol concept

HTTP protocol features

HTTP protocol classification

HTTP request

Request line

Request header

Request body

HTTP response

Response line

Response header

Response Body

Common HTTP status codes

HTTP response

Common HTTP response settings and use

PHP simulate HTTP requests

principle

Curl extension library use


HTTP protocol concept

HTTP protocol that is the Hypertext Transfer Protocol (Hypertext Transfer Protocol) . Is a detailed sets out the rules of mutual communication between the browser and the Web server.

 

HTTP protocol features

1) Client / Server

2) simple and fast: a customer service request to the server, simply transfer request method and path .

3) Flexible: HTTP protocol allows for transmission of any type of data objects

4) No connection: Meaning No connection is to limit the processing only one request per connection.

5) stateless: HTTP protocol is stateless protocol. Stateless means for transaction processing without memories. If the lack of state means that the subsequent processing required in front of the information, it must be retransmitted. On the other hand, when the server does not require previous information in its response relatively quickly.

 

HTTP protocol classification

HTTP request protocol: When the browser sends a request to the server to follow protocol

HTTP response protocol: when the server initiates a response to the browser to follow protocol.

 

HTTP request

Request line

1) Form: mode request resource path protocol version number

2)GET/index.php HTTP/1.1

 

The first time through the HTTP 1.0 protocol has requested exclusive row line (first line)

Request header

Request header is the contents of the agreement, the specific contents of the agreement will not always use all

1) Host: host address request (to be)

2) Accept: able to accept the current request type (MIME type returned by the server)

3) Accept-Language: language received

4) User-Agent: some information about the client browser's computer

 

Request header is not fixed, each request protocol is its own line, and finally there is a blank line (used to distinguish the request header and request body)

Request body

Data request: POST request will have the request body. GET request with all the data is, the path will be reflected in the resource request line after the URL.

 

The basic format: name = resource resource resource name = value & resource values ​​...

 

HTTP response

Response line

1) in the form: protocol version number of the status code state information

2) 200 ok: Success

3) 403 Forbidden: No access

4) 404 Not Found: Page Not Found

5) 500 Server Interval Error: internal server error

Response header

Specific agreement

1) Time: Wed 16 Sep 2018 11:43:33 GMT

2) server Server: Apache / 2.4 (win64) PHP / 7

3) The length of the Content-Length: 1571, data of a specific number of bytes (response body)

4) Content Type Conetent-Type: tell the browser to a data format

 

It cited several common response headers, not all: a response header per line, the last blank line (to distinguish response headers and response body)

Response Body

The actual server response content to the browser

 

 

Common HTTP status codes

Status code 200: Success

Status code 403: forbidden, deny access (no permission)

Status code 404: not found, page not found

Status code 500: Server Issues

The following W3C HTML inside HTTP messages, view the status code

1XX: Information 2XX: Success 3XX: Redirection 4XX: Client Error 5XX: Server Error

 

HTTP response

Common HTTP response settings and use

PHP is performed for the underlying design of the HTTP protocol (response), you may be implemented to modify HTTP response (response headers) by the function header

 

Precautions:

1, Header HTTP response may be designed, because the HTTP protocol is characterized by: in response to the line, in response to the head (the end of the blank line), the response body. Think time response header by header design, should not have any content output, so once generated content output (even if it is a space), the system will return to consider the response headers has ended and the response body began, so set the response if the first output content header (header use), set theory is not valid.

After allowing the server script in the output of content, does not return directly to the browser but now the use of server-side caching program retention (php.ini used output_buffering), with the contents: 2, after PHP5, increasing the program cache content and automatically adjusted in response to the first response member (allowing response head again after the content has been output is provided), but this time would be an error (warning) in the cache.

 

Summary: header settings do not have any output before the response body.

 

Location: Redirect, and jumps immediately (response body without parsing)

Browser server when parsing the response: first determination in response to row, continue to respond to the first, the last response body: location in response header, so that once the browser to see the agreement, no longer resolve down.

 

Refersh: Redirect timing jumps (Experience response analysis)

Delay Redirect: the browser will jump after a specific time delay for the specified link in access based: the browser until you are ready to jump access, will continue to parse the HTTP protocol (response headers and response body)

 

Conetent-type: the type of content, MIME types

By informing content (MIME type), the browser correctly parse the contents

 

Content-disposition: content type, MIMIE type extension, to activate the browser file download dialog box.

When the browser parses the content, the default is resolved directly: So sometimes you need to not resolve, as the contents of the file to download

 

 

PHP simulate HTTP requests

principle

PHP HTTP request can be initiated by simulating HTTP protocol

CURL is a very powerful open source library that supports many protocols, including HTTP, FTP, Telnet, and we use it to send HTTP requests. The benefits it has brought us is that you can set different parameters of the HTTP protocol through flexible options, and support HTTPS. CURL be "HTTP" or "HTTPS" automatically select whether to send the encrypted content based on the URL prefix

 

Prerequisites: HTTP protocol is a client / server mode, HTTP protocol does not have to be limited to browser access.

 

Curl extension library use

1, open CURL extension

2, sometimes, PHP version will even open the extension can not use the extended possibilities: because CURL not find the corresponding dll files related DLL files need to be placed in C: \ windows

3, restart Apache application

1) to establish a connection: curl_init (): activate a connection function CURL

2)设置请求选项:curl_setopt():设定选项

CURLOPT_URL:需要获取的 URL 地址,也可以在curl_init() 初始化会话的时候。

CURLOPT_RETURNTRANSFER:将服务器执行的结果(响应)以文件流的形式返回给请求界面(PHP脚本)

CURLOPT_POST :是否采用POST方式发起请求(默认请求是GET)

CURLOPT_POSTFIELDS:用来传递POST提交的数据。分为两种方式:字符串(name =abc&password=123)以及数组形式(array(‘name’=>’abc’,…))

CURLOPT_HEADER:是否得到相应的header信息(响应头),默认不获取。 启用时会将头文件的信息作为数据流输出。

 

3)执行请求:curl_exec():执行选项(与服务器发起请求),得到服务器返回的内容

如有乱码,加一个header('Content-type:text/html;charset=utf-8');

4)关闭连接:curl_close():关闭资源

Guess you like

Origin blog.csdn.net/yangmolulu/article/details/91474876