简体   繁体   English

PHP和HTTP请求和响应消息之间的链接

[英]Link between PHP and HTTP Request and Response Messages

When I did a networks course I learned about HTTP Request and Response messages and I know how to code in php reasonably enough to get around. 当我参加网络课程时,我了解了HTTP请求和响应消息,并且知道如何在php中进行合理的编码以解决问题。 Now my question is, the PHP has to have some link to HTTP request and response message but how. 现在我的问题是,PHP必须具有一些指向HTTP请求和响应消息的链接,但是如何链接。 I can't seem to see the link between the two. 我似乎看不到两者之间的联系。 My reasoning for asking this is that I am using the Twitter API console tool to query their api. 我问这个的原因是我正在使用Twitter API 控制台工具来查询其api。 The tool sends the following HTTP request: 该工具发送以下HTTP请求:

GET /1.1/search/tweets.json?q=%40twitterapi HTTP/1.1
Authorization:
OAuth oauth_consumer_key="DC0se*******YdC8r4Smg",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1410970037",oauth_nonce="2453***055",oauth_version="1.0",oauth_token="796782156-ZhpFtSyPN5K3G**********088Z50Bo7aMWxkvgW",oauth_signature="Jes9MMAk**********CxsKm%2BCJs%3D"
Host:
api.twitter.com
X-Target-URI:
https://api.twitter.com
Connection:
Keep-Alive 

and then I get a HTTP response: 然后我得到一个HTTP响应:

HTTP/1.1 200 OK
x-frame-options:
SAMEORIGIN
content-type:
application/json;charset=utf-8
x-rate-limit-remaining:
177
last-modified:
Wed, 17 Sep 2014 16:07:17 GMT
status:
200 OK
date:
Wed, 17 Sep 2014 16:07:17 GMT
x-transaction:
491****a8cb3f7bd
pragma:
no-cache
cache-control:
no-cache, no-store, must-revalidate, pre-check=0, post-check=0
x-xss-protection:
1; mode=block
x-content-type-options:
nosniff
x-rate-limit-limit:
180
expires:
Tue, 31 Mar 1981 05:00:00 GMT
set-cookie:
lang=en
set-cookie:
guest_id=v1%3A14109******2451388; Domain=.twitter.com; Path=/; Expires=Fri, 16-Sep-2016 16:07:17 UTC
content-length:
59281
x-rate-limit-reset:
1410970526
server:
tfe_b
strict-transport-security:
max-age=631138519
x-access-level:
read-write-directmessages

So how do these HTTP request and response messages fit into PHP? 那么这些HTTP请求和响应消息如何适合PHP? Does PHP auto generate this? PHP是否会自动生成? How do I add authorization to PHP requests etc? 如何向PHP请求等添加授权? I'm confused about the deeper workings of PHP 我对PHP更深入的工作感到困惑

When the client sends the HTTP request to the server, there has to be something to receive the HTTP request, which is called a web server. 当客户端将HTTP请求发送到服务器时,必须有一些东西可以接收HTTP请求,这称为Web服务器。 Examples of web servers are Apache, IIS, Nginx, etc. You can also write your own server, which can handle input however it wants. Web服务器的示例包括Apache,IIS,Nginx等。您还可以编写自己的服务器,该服务器可以根据需要处理输入。 In this case, I'll assume that you are requesting a PHP file. 在这种情况下,我假设您正在请求一个PHP文件。

When the web server captures the HTTP request, it determines how it should be handled. 当Web服务器捕获HTTP请求时,它确定应如何处理它。 If the file requested is tweets.json , it will go make sure that file exists, and then pass control over to PHP. 如果请求的文件是tweets.json ,它将确保该文件存在,然后将控制权交给PHP。

PHP then begins its execution, and performs any logic that the script needs to do, meaning it could go to the database, it reads, writes and makes decisions based cookies, it does math, etc. 然后,PHP开始执行,并执行脚本需要执行的任何逻辑,这意味着它可以进入数据库,可以基于cookie进行读取,写入和决策,还可以进行数学运算等。

When the PHP script is done, it will return a HTML page as well as a bunch of headers back to the web server that called it. 完成PHP脚本后,它将返回HTML页面以及一堆标头返回给调用它的Web服务器。 From there, the web server turns the HTML page and headers back into a HTTP request to respond. 从那里,Web服务器将HTML页面和标头转换回HTTP请求进行响应。

That is a pretty simple overview, and web servers can work in many different ways, but this is a simple example of how it could work in a introductory use-case. 这是一个非常简单的概述,Web服务器可以以许多不同的方式工作,但这只是一个简单的示例,说明了它如何在介绍性用例中工作。 In more complex scenarios, people can write their own web servers, which perform more complex logic inside of the web server software, rather than passing it off to PHP. 在更复杂的场景中,人们可以编写自己的Web服务器,该服务器在Web服务器软件内部执行更复杂的逻辑,而不是将其传递给PHP。

When it comes down to it, PHP files are just scripts that the web server executes when they are called, they provide the HTTP request as input, and get a web page and headers as output. 归根结底,PHP文件只是Web服务器在被调用时执行的脚本,它们提供HTTP请求作为输入,并获得网页和标头作为输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM