mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-01 12:30:57 +02:00
Merge pull request #42 from restoreddev/HttpServerChapter
Http server chapter
This commit is contained in:
@@ -17,13 +17,13 @@ button {
|
|||||||
}
|
}
|
||||||
code {
|
code {
|
||||||
background-color: #F0F0F0;
|
background-color: #F0F0F0;
|
||||||
font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
font-family: Menlo, Monaco, Consolas, "Noto Mono", "Liberation Mono", "Courier New", monospace !important;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
padding: 0.10em 0.25em;
|
padding: 0.10em 0.25em;
|
||||||
}
|
}
|
||||||
pre, pre code {
|
pre, pre code {
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
font-family: Menlo, Monaco, Consolas, "Noto Mono", "Liberation Mono", "Courier New", monospace !important;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
margin: 0 !important;
|
margin: 0 !important;
|
||||||
border-radius: 0 !important;
|
border-radius: 0 !important;
|
||||||
|
@@ -31,6 +31,7 @@
|
|||||||
<ol>
|
<ol>
|
||||||
<li><a href="<?= page_path('web/http') ?>">HTTP</a></li>
|
<li><a href="<?= page_path('web/http') ?>">HTTP</a></li>
|
||||||
<li><a href="<?= page_path('web/http-post') ?>">HTTP POST</a></li>
|
<li><a href="<?= page_path('web/http-post') ?>">HTTP POST</a></li>
|
||||||
|
<li><a href="<?= page_path('web/http-server') ?>">PHP HTTP Server</a></li>
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<a href="<?= page_path('credits') ?>">Credits</a>
|
<a href="<?= page_path('credits') ?>">Credits</a>
|
||||||
|
@@ -32,5 +32,3 @@ POST requests are used to send data to a server. A server will process the data
|
|||||||
We have been talking a lot about how a server can process requests and send responses. How do programmers develop a server to handle HTTP requests? With PHP!
|
We have been talking a lot about how a server can process requests and send responses. How do programmers develop a server to handle HTTP requests? With PHP!
|
||||||
|
|
||||||
PHP was built with web server development in mind. Let us write our first PHP server script.
|
PHP was built with web server development in mind. Let us write our first PHP server script.
|
||||||
|
|
||||||
|
|
||||||
|
58
chapters/web/03-http-server.md
Normal file
58
chapters/web/03-http-server.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
In PHP, you usually use a separate web server program that accepts HTTP requests and passes them to PHP to create a response. Common examples of separate web server programs are Apache and Nginx. However, PHP has a built in web server we can use during development.
|
||||||
|
|
||||||
|
To use the development web server, open a terminal and a new folder for holding your code. Create a file called `index.php` and put this PHP code in it:
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
echo 'Hello World!';
|
||||||
|
```
|
||||||
|
Go back to your terminal and start a PHP web server using this command:
|
||||||
|
```bash
|
||||||
|
php -S localhost:8080
|
||||||
|
```
|
||||||
|
Now, you can open `localhost:8080` in your web browser and you will see 'Hello World!' in the content of the page.
|
||||||
|
|
||||||
|
Next, we can use PHP to analyze all the information about the HTTP request sent from the browser to your PHP script. Change your `index.php` to have this code:
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
|
$uri = $_SERVER['REQUEST_URI'];
|
||||||
|
$protocol = $_SERVER['SERVER_PROTOCOL'];
|
||||||
|
$headers = getallheaders();
|
||||||
|
|
||||||
|
echo "$method $uri $protocol <br/>";
|
||||||
|
foreach ($headers as $key => $header) {
|
||||||
|
echo "$key: $header <br/>";
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Reload `localhost:8080` and you will see the main request line and all of the headers printed on the web page. The `<br/>` tag is HTML for a new line. Since we are outputting to a web browser, we can no longer use `"\n"` to create a new line.
|
||||||
|
|
||||||
|
By default, PHP will generate the necessary response line and headers. If you open your browser console and open the network tab, you can see the response code and headers.
|
||||||
|
```http
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
Host: localhost:8080
|
||||||
|
Date: Sun, 28 Apr 2019 10:19:25 -0500
|
||||||
|
Connection: close
|
||||||
|
X-Powered-By: PHP/7.2.17
|
||||||
|
Content-type: text/html; charset=UTF-8
|
||||||
|
```
|
||||||
|
PHP set the response code to `200 OK` and the content type to `text/html`. Even though PHP will output good defaults, we can change them in our code if we want. Update your `index.php` file with this code:
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
http_response_code(404);
|
||||||
|
header('Content-Type: text/plain');
|
||||||
|
|
||||||
|
echo "Not Found!";
|
||||||
|
```
|
||||||
|
We are changing the status code to `404 Not Found` and the content type to plain text. If you open `localhost:8080` in your browser, you will now see this HTTP response in your console:
|
||||||
|
```http
|
||||||
|
HTTP/1.1 404 Not Found
|
||||||
|
Host: localhost:8080
|
||||||
|
Date: Sun, 28 Apr 2019 10:26:40 -0500
|
||||||
|
Connection: close
|
||||||
|
X-Powered-By: PHP/7.2.17
|
||||||
|
Content-type: text/plain;charset=UTF-8
|
||||||
|
```
|
||||||
|
PHP added the other necessary headers for the response, but this time used `404` in the main response line and set the `Content-type` header to `text/plain`.
|
@@ -165,6 +165,12 @@ return [
|
|||||||
'title' => 'HTTP POST',
|
'title' => 'HTTP POST',
|
||||||
'subtitle' => 'Sending data to a server',
|
'subtitle' => 'Sending data to a server',
|
||||||
'previous' => 'web/http',
|
'previous' => 'web/http',
|
||||||
|
'next' => 'web/http-server',
|
||||||
|
]),
|
||||||
|
Page::create('web/http-server', 'web/03-http-server.md', [
|
||||||
|
'title' => 'PHP HTTP Server',
|
||||||
|
'subtitle' => 'Handling HTTP Requests in PHP',
|
||||||
|
'previous' => 'web/http-post',
|
||||||
'next' => '',
|
'next' => '',
|
||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
|
41
package-lock.json
generated
41
package-lock.json
generated
@@ -3895,7 +3895,8 @@
|
|||||||
"ansi-regex": {
|
"ansi-regex": {
|
||||||
"version": "2.1.1",
|
"version": "2.1.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"aproba": {
|
"aproba": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
@@ -3916,12 +3917,14 @@
|
|||||||
"balanced-match": {
|
"balanced-match": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"brace-expansion": {
|
"brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"balanced-match": "^1.0.0",
|
"balanced-match": "^1.0.0",
|
||||||
"concat-map": "0.0.1"
|
"concat-map": "0.0.1"
|
||||||
@@ -3936,17 +3939,20 @@
|
|||||||
"code-point-at": {
|
"code-point-at": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"concat-map": {
|
"concat-map": {
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"console-control-strings": {
|
"console-control-strings": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"core-util-is": {
|
"core-util-is": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
@@ -4063,7 +4069,8 @@
|
|||||||
"inherits": {
|
"inherits": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"ini": {
|
"ini": {
|
||||||
"version": "1.3.5",
|
"version": "1.3.5",
|
||||||
@@ -4075,6 +4082,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"number-is-nan": "^1.0.0"
|
"number-is-nan": "^1.0.0"
|
||||||
}
|
}
|
||||||
@@ -4089,6 +4097,7 @@
|
|||||||
"version": "3.0.4",
|
"version": "3.0.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"brace-expansion": "^1.1.7"
|
"brace-expansion": "^1.1.7"
|
||||||
}
|
}
|
||||||
@@ -4096,12 +4105,14 @@
|
|||||||
"minimist": {
|
"minimist": {
|
||||||
"version": "0.0.8",
|
"version": "0.0.8",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"minipass": {
|
"minipass": {
|
||||||
"version": "2.2.4",
|
"version": "2.2.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"safe-buffer": "^5.1.1",
|
"safe-buffer": "^5.1.1",
|
||||||
"yallist": "^3.0.0"
|
"yallist": "^3.0.0"
|
||||||
@@ -4120,6 +4131,7 @@
|
|||||||
"version": "0.5.1",
|
"version": "0.5.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"minimist": "0.0.8"
|
"minimist": "0.0.8"
|
||||||
}
|
}
|
||||||
@@ -4200,7 +4212,8 @@
|
|||||||
"number-is-nan": {
|
"number-is-nan": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"object-assign": {
|
"object-assign": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.1",
|
||||||
@@ -4212,6 +4225,7 @@
|
|||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"wrappy": "1"
|
"wrappy": "1"
|
||||||
}
|
}
|
||||||
@@ -4297,7 +4311,8 @@
|
|||||||
"safe-buffer": {
|
"safe-buffer": {
|
||||||
"version": "5.1.1",
|
"version": "5.1.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"safer-buffer": {
|
"safer-buffer": {
|
||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
@@ -4333,6 +4348,7 @@
|
|||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"code-point-at": "^1.0.0",
|
"code-point-at": "^1.0.0",
|
||||||
"is-fullwidth-code-point": "^1.0.0",
|
"is-fullwidth-code-point": "^1.0.0",
|
||||||
@@ -4352,6 +4368,7 @@
|
|||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"ansi-regex": "^2.0.0"
|
"ansi-regex": "^2.0.0"
|
||||||
}
|
}
|
||||||
@@ -4395,12 +4412,14 @@
|
|||||||
"wrappy": {
|
"wrappy": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"yallist": {
|
"yallist": {
|
||||||
"version": "3.0.2",
|
"version": "3.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user