How PHP uses socket to send HTTP request

WBOY
Release: 2016-07-29 09:03:45
Original
1065 people have browsed it

The example in this article describes how PHP uses socket to send HTTP requests. Share it with everyone for your reference, the details are as follows:

socket method:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec"=>20, "usec"=>0));
socket_connect($socket, 'www.baidu.com', 80);
//里面的换行代表 \r\n 注意拷贝的代码后面可能有空格
$http = <<
Copy after login

fsockopen method:

$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)
\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.baidu.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $http); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }
Copy after login

Original socket method:

$fp = stream_socket_client("tcp://www.baidu.com:80", $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)
\n"; } else { $http = <<
Copy after login

stream Method (get):

$http = <<array(
        'header' => $http,
        'timeout'=>1, //超时 秒
        'method' => 'GET', //默认方式
         'protocol_version' => '1.1', //默认为 1.0
    ),
);
//参数格式参考 http://php.net/manual/zh/context.http.php
//curl方式的格式可以参考; http://php.net/manual/zh/context.curl.php
$context = stream_context_create($hdrs);
echo file_get_contents('http://www.baidu.com', 0, $context);

Copy after login

stream way post:

$postdata = http_build_query(array('act'=>'save', 'id'=>387171));
$http = <<array(
        'header' => $http,
        'timeout'=>1, //超时 秒
        'method' => 'POST',
        'content' => $postdata,
         'protocol_version' => '1.1', //默认为 1.0
    ),
);
//参数格式参考 http://php.net/manual/zh/context.http.php
//curl方式的格式可以参考; http://php.net/manual/zh/context.curl.php
$context = stream_context_create($hdrs);
echo file_get_contents('http://test.cm/song.php', 0, $context);

Copy after login

Note: http1.1 must contain the Host header, but http1.0 does not need it

Readers who are interested in more PHP-related content can check out this site's special topic: "php socket usage Summary", "Introduction Tutorial on PHP Basic Syntax", "Summary of PHP Error and Exception Handling Methods" and "Summary of Common PHP Functions and Techniques"

I hope this article will be helpful to everyone in PHP programming.

The above introduces the method of using PHP socket to send HTTP requests, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!