Last active
August 3, 2021 12:58
-
-
Save sashachabin/edd7271c407a3f5cc4dcea44e10a32d4 to your computer and use it in GitHub Desktop.
УрФУ. Отправка запросов с помощью Socket на PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Исходный код: | |
<html> | |
<head> | |
<title>Реализация сокетов на PHP</title> | |
</head> | |
<body> | |
<pre> | |
<?php | |
$host = "kontur.ru"; | |
$ip = gethostbyname($host); | |
$port = 80; | |
// Создание конечной точки соединения | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
// Установка соединения с сервером | |
socket_connect($socket, $ip, $port); | |
// Запрос к серверу | |
$request = "GET /?request HTTP/1.1\r\n" | |
."Host: $host\r\n" | |
."Connection: close\r\n" | |
."\r\n"; | |
// Отправка запроса | |
socket_write($socket, $request, strlen($request)); | |
// Вывод ответа | |
$answer = socket_read($socket, 2048); | |
echo $answer; | |
?> | |
</pre> | |
</body> | |
</html> | |
-------------------------------------------- | |
Ответ: | |
HTTP/1.1 301 Moved Permanently | |
Date: Sat, 30 Sep 2017 18:58:24 GMT | |
Content-Type: text/html; charset=UTF-8 | |
Content-Length: 141 | |
Connection: close | |
Set-Cookie: sr=1; Path=/ | |
Location: https://kontur.ru/ | |
<head><title>Document Moved</title></head> | |
<body><h1>Object Moved</h1>This document may be found <a HREF="https://kontur.ru/">here</a> | |
-------------------------------------------- | |
Сервер вернул ответ с кодом 301 Moved Permanently («перемещено навсегда»). | |
Перенаправление с "http://" на "https://". | |
-------------------------------------------- | |
Пример работы: http://nibach.ru/socket.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Живой пример:
http://nibach.ru/socket.php