HTTP(一)

http
php
http请求

HTTP请求:请求行、消息报头、请求正文。格式如下:

Method Request-URI HTTP-Veraion CRLF

参数说明

  • Method 请求方法
  • Request-URI 一个统一资源标识符
  • HTTP-Version 请求的HTTP协议版本
  • CRLF 回车和换行

响应:状态行、消息报头、响应正文

HTTP-Version Status-Code Reason-Phrase CRLF

参数说明

  • HTTP-Verson 服务器HTTP协议的版本
  • Status-Code 服务器发回的响应状态码
  • Reson-Phrase 状态代码的文本描述
  • CRLF 回车和换行

响应状态码:

  1. 1XX:指示信息——请求已接收、已处理
  2. 2XX:成功——请求已被成功接收、理解、接收
  3. 3XX:重定向——要完成请求必须进行更进一步的操作
  4. 4XX:客户端错误——请求有语法错误或请求无法实现
  5. 5XX:服务端错误——服务端未能实现合法的请求

常见状态码

  • 200 OK:客户端请求成功
  • 400 Bad Request:客户端请求有语法错误,不被服务器所理解
  • 401 Unauthorize:请求未经授权,此状态码必须和WWW Authenticate 报头域一起使用
  • 403 Forbidden:服务器收到请求,但是拒绝提供服务
  • 404 Not Found:请求资源不存在
  • 500 Internal Server Error :服务器发生不可预期的错误
  • 503 Server Unavailable:服务器当前不能处理客户端的请求,一段时间后可能恢复正常

相关函数:

get_headers(string $url [, int $format = 0 ] ) — 取得服务器响应一个 HTTP 请求所发送的所有标头

返回包含有服务器响应一个 HTTP 请求所发送标头的索引或关联数组,如果失败则返回 FALSE 。 (通过判断状态码是否为200,就可以判断请求的资源存在与否)

telnet模拟请求

  1. cmd ->telnet localhost(域名) 80
  2. 按下crtl+] ,仔按下enter (打开回显功能)
3. 发送报文
GET /test/httptest.php?id=1 HTTP/1.1
Connection: close
Host: localhost
Content-type:application/x-www-form-urlencoded
content-length:20
(两个回车)
#通过1.1版本协议请求index.html页面;connection: close是实用短连接,即服务器返回后就断开连接;Host字段知名页面所在的主机名。

测试服务端 httptest.php

<?php

	$dataGet = $_GET;
$dataPost = $_POST;
$dataGetstr = http_build_query($dataGet);
$dataPoststr = http_build_query($dataPost); if( $dataGet ){
echo 'get '.$dataGetstr;
} if( $dataPost ){
echo 'post '.$dataPoststr;
}
?>

PHP实现HTTP请求

$postData =http_build_query(
array(
'title' => "这里是 file_get_contents 提交的数据",
'content' => "你好 !",
'type' => 1
)
); #file_get_contents
$opts = array(
'http' =>array(
'method' => "POST" ,
'header' => "Host:localhost\r\n".
"Content-type:application/x-www-form-urlencoded\r\n".
"Content-length:".(strlen($postData))."\r\n".
"Cookie: foo=bar\r\n",
"content" => $postData,
//'timeout' => 60 * 60 // 超时时间(单位:s)
)
); $context = stream_context_create ( $opts );//创建数据流上下文 //file_get_contents( 'http://localhost/test/httptest.php',false,$context ); #fopen
$postData = http_build_query(
array(
'title' => "这里是 fopen 提交的数据",
'content' => "你好 !",
'type' => 1
));
$fp = fopen( 'http://localhost/test/httptest.php','r',false,$context );
  • stream_context_create 创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

curl方式提交

$url = "http://localhost/test/httptest.php";
$postData = http_build_query(
array(
'title' => "这里是 fopen 提交的数据",
'content' => "你好 !",
'type' => 1
));
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL,$url );
curl_setopt( $ch,CURLOPT_POST,1 );
curl_setopt( $ch,CURLOPT_POSTFIELDS,$postData );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER,1 );
curl_exec();
curl_close();

socket方式提交

$postData = http_build_query(
array(
'title' => "这里是 fopen 提交的数据",
'content' => "你好 !",
'type' => 1
));
$fp = fsockopen( "localhost",80,$errno,$errorStr,5 );
$request = "POST http://localhost/test/httptest.php HTTP/1.1\r\n";
$request .= "Host:localhost\r\n";
$request .= "Content-type:application/x-www-form-urlencoded\r\n";
$request .= "Content-length:".(strlen($postData))."\r\n";
$request .= $postData;
fwrite( $fp,$request ); while(!feof($fp)){
echo fgets($fp,1024);
}
fclose($fp);

-- 这段时间读读书,理解理解原理,生活也很充实。

随机推荐

  1. redis和memcache缓存击穿,缓存失效问题

    我们在用缓存的时候,不管是Redis或者Memcached,基本上会通用遇到以下三个问题: 缓存穿透 缓存并发 缓存失效 一.缓存穿透 Paste_Image.png Paste_Image.png ...

  2. 【wordpress】wordpress自定义主题

    wordpress每个主题至少要有这两个文件 – style.css 和 index.php. index.php 告诉主题中所有的元素如何布局; style.css 则告诉主题中所有的元素该如何展示 ...

  3. Unity GL画折线

    新建一个脚本,这个物体得挂在有摄像机组件的物体上才能生效 OnPostRender() 这个函数才会被自动调用(类似生命周期自动调用) 然后就可以代码画线了,原理是openGL的画线 using Un ...

  4. CSS的引入

    CSS的引入方式: 1.将样式规则写在css样式文件中,再以<link>标签引入. <link rel=stylesheet type="text/css" hr ...

  5. nexus开机启动

    在/etc/init.d目录下创建nexus文件 #!/bin/bash #chkconfig: #description:nexus3 #processname:nexus3 export JAVA ...

  6. Java入门系列-17-多态

    这篇文章贯穿游戏中的一些功能带你掌握多态的使用 为什么要使用多态 在一款对战类游戏中(如有雷同纯属巧合),有两个不同的法师英雄:小乔.妲己. 两个法师英雄的都有攻击的方法,小乔的攻击伤害为10,消耗魔 ...

  7. Golang教程:Map

    什么是 map? Map 是 Go 中的内置类型,它将键与值绑定到一起.可以通过键获取相应的值. 如何创建 map? 可以通过将键和值的类型传递给内置函数 make 来创建一个 map.语法为:mak ...

  8. vue中echarts随窗体变化

    <div id="myChart" :style="{width: '100%', height: '345px'}"></div> & ...

  9. Effective C++ .06 阻止编译器自动生成函数以及被他人调用

    这节讲了下如何防止对象拷贝(隐藏并不能被其他人调用) 两种方法: 1. 将拷贝构造函数声明为private 并且声明函数但不进行定义 #include <iostream> #includ ...

  10. js如何判断字符串里面是否含有某个字符串

    方法一: indexOf() (推荐) var str = "123"; console.log(str.indexOf("3") != -1 ); // tr ...