fsockopen与HTTP 1.1/HTTP 1.0

在前面的例子中,HTTP请求信息头有些指定了 HTTP 1.1,有些指定了 HTTP/1.0,有些又没有指定,那么他们之间有什么区别呢?

关于HTTP 1.1与HTTP 1.0的一些基本情况,可以参考下 HTTP 1.1的详细介绍 。

我们先来看一下 fsockopen 不指定 HTTP 的情况:

function asyn_sendmail()
{
    $ip = ‘121.199.24.143‘;
    $url = ‘/php/sock.php‘;
    $fp = fsockopen($ip, 80, $errno, $errstr, 5);
    if (!$fp)
    {
        echo "$errstr ($errno)<br />\n";
    }
    $end = "\r\n";
    $input = "GET $url$end";
    $input.="$end";
    fputs($fp, $input);
    $html = ‘‘;
    while (!feof($fp))
    {
        $html.=fgets($fp);
    }
    fclose($fp);
    writelog($html);
    echo $html;
}
function writelog($message)
{
    $path = ‘F:\log2.txt‘;
    $handler = fopen($path, ‘w+b‘);
    if ($handler)
    {
        $success = fwrite($handler, $message);
        fclose($handler);
    }
}
asyn_sendmail();

sock.php:

<?php
    echo "Welcome to NowaMagic";
?> 

程序输出:

Welcome to NowaMagic

log2.txt 内容也是:

Welcome to NowaMagic

那些接下来再看看在标头加上 HTTP 1.1 的程序:

function asyn_sendmail()
{
    $ip = ‘121.199.24.143‘;
    $url = ‘/php/sock.php‘;
    $fp = fsockopen($ip, 80, $errno, $errstr, 5);
    if (!$fp)
    {
        echo "$errstr ($errno)<br />\n";
    }

    $end = "\r\n";
    $input = "GET $url HTTP/1.1$end";
    //如果不加下面这一句,会返回一个http400错误
    $input.="Host: $ip$end";
    //如果不加下面这一句,请求会阻塞很久
    $input.="Connection: Close$end";     $input.="$end";
    fputs($fp, $input);
    $html = ‘‘;
    while (!feof($fp))
    {
        $html.=fgets($fp);
    }
    fclose($fp);
    writelog($html);
    echo $html;
}

function writelog($message)
{
    $path = ‘F:\log.txt‘;
    $handler = fopen($path, ‘w+b‘);
    if ($handler)
    {
        $success = fwrite($handler, $message);
        fclose($handler);
    }
}
asyn_sendmail();

程序输出:

HTTP/1.1 200 OK
Date: Fri, 07 Feb 2014 13:50:14 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.3.3
Vary: Accept-Encoding
Content-Length: 21
Connection: close
Content-Type: text/html; charset=UTF-8

Welcome to NowaMagic 

留意到注释:

//如果不加下面这一句,请求会阻塞很久
$input.="Connection: Close$end";     $input.="$end";

原因是什么呢? 可以参考 fsockopen用feof读取http响应内容的一些问题

//如果不加下面这一句,会返回一个http400错误
$input.="Host: $ip$end";    

报400错误:

HTTP/1.1 400 Bad Request
Date: Fri, 07 Feb 2014 13:54:57 GMT
Server: Apache/2.2.3 (CentOS)
Content-Length: 305
Connection: close
Content-Type: text/html; charset=iso-8859-1

使用http1.1连接,要加上Host请求表头。

小结:

  • HTTP 1.0, Apache Web 服务器中 $input.="Connection: Close$end"; 与 $input.="Connection: Close$end" 可都不需要。
  • HTTP 1.0, Nginx Web 服务器中 $input.="Connection: Close$end"; 与 $input.="Connection: Close$end" 都必需。
  • HTTP 1.1, Apache Web 服务器中 $input.="Connection: Close$end"; 必须要,$input.="Connection: Close$end" 可不用。
  • HTTP 1.1, Nginx Web 服务器中 $input.="Connection: Close$end"; 与 $input.="Connection: Close$end" 都必需。
时间: 2024-08-04 02:05:36

fsockopen与HTTP 1.1/HTTP 1.0的相关文章

分享一个强大的采集类,还可以模拟php多进程

做采集的时候,可以使用file_get_contents()去获取网页源代码,但是使用file_get_contents采集,速度慢,而且超时时间,不好控制.如果采集的页面不存在,需要等待的时间很长.一般来说,curl的速度最快,其次是socket,最后是file_get_contents.现在跟大家分享一个很强大的采集类,会根据你的服务器当前的配置,自动选择最快的方式.已经封装了curl和socket,file_get_contents 用法很简单:1,采用get方法请求Http::doGet

鬃嘴释怀说太多就成真不了。

子阻撞砖奏尊仔籽着 释怀说太多就成真不了. http://passport.baidu.com/?business&un=vip&un=%E5%A4%A9%E6%B0%B4%E4%B8%8A%E9%97%A8%E8%BF%99%E5%B0%8F%E5%A7%90#0 http://passport.baidu.com/?business&un=vip&un=%E7%99%BD%E9%93%B6%E4%B8%8A%E9%97%A8%E8%BF%99%E5%B0%8F%E5%A

澜星粘鼐贩逊耐盼系甭妊倏纪傲傲sdfghjk

http://passport.baidu.com/?business&un=R&un=%E5%A4%A7%E5%AE%81%E6%A1%91%E6%8B%BF%E9%80%9A%E5%B0%8F%E5%A7%90#0 http://passport.baidu.com/?business&un=R&un=%E4%B9%A1%E5%AE%81%E6%A1%91%E6%8B%BF%E9%80%9A%E5%B0%8F%E5%A7%90#0 http://passport.bai

在Ubuntu14.04上OpenStack Juno安装部署

在Ubuntu14.04上OpenStack Juno安装部署 0 安装方式 0.1 安装方式 安装方式 说明 目标 备注 单结点 一台服务器运行所有的nova-xxx组件,同时也驱动虚拟实例. 这种配置只为尝试Nova,或者为了开发目的进行安装.   1控制节点+N个计算节点 一个控制结点运行除nova-compute外的所有nova-services,然后其他compute结点运行nova-compute.所有的计算节点需要和控制节点进行镜像交互,网络交互,控制节点是整个架构的瓶颈. 这种配

2008 SCI 影响因子(Impact Factor)

Excel download 期刊名缩写 影响因子 ISSN号 CA-CANCER J CLIN 74.575 0007-9235 NEW ENGL J MED 50.017 0028-4793 ANNU REV IMMUNOL 41.059 0732-0582 NAT REV MOL CELL BIO 35.423 1471-0072 PHYSIOL REV 35.000 0031-9333 REV MOD PHYS 33.985 0034-6861 JAMA-J AM MED ASSOC 3

serv-u and hway3.0

一个非常好用的su提权脚本,在支持php的环境下,目录可读可写,基本秒杀. <? //HWay && Serv-U by r00t //[email protected] //www.007team.net error_reporting(0); extract($_POST); extract($_GET); function scanPort($start,$end,$host="127.0.0.1") { $a=false; if(intval($start

利用 fsockopen() 函数开放端口扫描器

利用 fsockopen() 函数开放端口扫描器 1.前言 本文利用 fsockopen() 函数,编写一个功能简单的端口扫描器. 2.关键技术 本实例的端口号是固定的,通过对数组的遍历,利用 fsockopen() 函数连接,如果连接成功,则该端口处于开放状态,否则该端口处于关闭状态. 核心代码如下: foreach ($port as $key => $value) { echo '<tr>'; echo '<td>' . $key . '</td>'; ec

PHP的fsockopen函数详解

先来看看手册是如何定义fsockopen函数的. fsockopen - 打开一个网络连接或者一个Unix套接字连接. resource fsockopen    ( string $hostname   [, int $port = -1   [, int &$errno   [, string &$errstr   [, float $timeout = ini_get("default_socket_timeout")  ]]]] 使用fsockopen方法和使用

基于php socket(fsockopen)的应用实例分析

fsockopen函数能够运用,首先要开启php.ini中的allow_url_open=on;fsockopen是对socket客户端代码的封装,该函数中封装了socket_create,socket_connect.服务器端代码:server.php . 代码如下: <?phperror_reporting(E_ALL);set_time_limit(0);$address = '127.0.0.1';$port = 10008;//创建端口if (($sock = socket_creat