Http:超文本传输协议(英文:HyperText Transfer
Protocol,缩写:HTTP)是互联网上应用最为广泛的一种网络协议。
http请求协议
- 请求行
Request URL:http://www.hotel.wsq.com/
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:80
- 请求头
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 #可接受的内容类型
Accept-Encoding:gzip, deflate, sdch #可接受的数据编码类型(压缩编码)
Accept-Language:zh-CN,zh;q=0.8 #浏览器可接受语言
Cache-Control:max-age=0 #缓存控制选项
Connection:keep-alive #tcp连接类型
Host:www.hotel.wsq.com #标识一台web服务器上的一个虚拟主机
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
#空行表示头部分结束
- 请求内容
username:123@123.com
password:123
模拟请求
连接目标服务器,发送符合请求协议格式的数据
服务器将其视为请求发出响应
连接:fsockopen()
处理请求数据:请求头
发送请求:fwrite()
处理响应:fgets()
断开连接:fclose()
//GET
<?php
$host = 'www.hotel.wsq.com';
$port = '80';
$link = fsockopen($host,$port);
//var_dump($link);
define('CRLF',"\r\n");
//请求行
$req = 'GET /index.php HTTP/1.1'.CRLF;
//请求头
$req.='HOST:www.hotel.wsq.com'.CRLF;
$req.='User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'.CRLF;
$req.='Connection:keep-alive'.CRLF;
//空行表结束
$req.=CRLF;
//请求主体
//GET没有主体
//发送请求
fwrite($link,$req);
//处理响应数据
while(!feof($link)){
echo fgets($link,1024);
}
//断开连接
fclose($link);
//POST
<?php
$host = 'www.hotel.wsq.com';
$port = '80';
$link = fsockopen($host,$port);
//var_dump($link);
define('CRLF',"\r\n");
//请求行
$req = 'POST /index.php HTTP/1.1'.CRLF;
//请求头
$req.='HOST:www.hotel.wsq.com'.CRLF;
$req.='User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'.CRLF;
$req.='Connection:keep-alive'.CRLF;
//post特殊请求头
$arr = array('username'=>"root",'password'=>'root');
$len = http_build_query($arr);
$req.='Content-Length:'.strlen($len).CRLF;
//echo $len.strlen($len) ;die;
$req.='Content-Type:application/x-www-form-urlencoded'.CRLF;
//空行表结束
$req.=CRLF;
//请求主体
$req.=$len;
//发送请求
fwrite($link,$req);
//处理响应数据
while(!feof($link)){
echo fgets($link,1024);
}
//断开连接
fclose($link);
curl模拟请求
client url
php扩展,可以用来模拟url客户端(浏览器,请求代理)的工具扩展
;extension=php_curl.dll
- 依赖类库:
libeay32.dll
ssleay32.dll
//GET
<?php
$curl = curl_init();
$url = 'www.hotel.wsq.com';
curl_setopt($curl,CURLOPT_URL,$url);
curl_exec($curl);
curl_close($curl);
//POST
<?php
$curl = curl_init();
$url = 'www.hotel.wsq.com';
$data = array('username'=>'wsqhotel@qq.com','password'=>'manager');
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
//curl_setopt($curl,CURLOPT_HEADER,true);
curl_exec($curl);
curl_close($curl);
//Upload
<?php
$curl = curl_init();
$url = 'www.wsq.com/app/http/curl/upload.php';
$data = array('logo'=>'@D:\phpStudy\WWW\app\http\curl\2015-10-21_150123.png');
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_exec($curl);
curl_close($curl);
//Cookie
<?php
$curl = curl_init();
$url = 'www.hotel.wsq.com';
$data = array('username'=>'wsqhotel@qq.com','password'=>'manager');
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_COOKIEJAR,'D:\phpStudy\WWW\app\http\curl\cookie.txt');
//curl_setopt($curl,CURLOPT_HEADER,true);
curl_exec($curl);
curl_close($curl);
$curl = curl_init();
$url = 'www.hotel.wsq.com/index.php?p=admin&c=index&a=index&l=1';
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_COOKIEFILE,'D:\phpStudy\WWW\app\http\curl\cookie.txt');
curl_exec($curl);
curl_close($curl);
响应协议
- 响应行
Request URL:http://www.hotel.wsq.com/
Request Method:POST
//重定向
Status Code:302 Found
Remote Address:127.0.0.1:80
响应状态码与响应消息一一对应
404 Not Found
403 Forbidden
200 OK
500 ServerInternalError
- 响应头
服务器告知浏览器属性信息
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:383
Content-Type:text/html
Date:Wed, 06 Apr 2016 02:53:54 GMT
//设置缓存
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Location:index.php?p=admin&c=index&a=index&l=1
Pragma:no-cache
Server:Apache/2.4.10 (Win32) OpenSSL/0.9.8zb PHP/5.3.29
X-Powered-By:PHP/5.3.29
- 响应主体
通过浏览器查看源代码看到的信息
操作响应
- 操作响应头:header()
//操作缓存时间
<?php
header('Expires:'.gmdate('D, d M Y H:i:s',time()+5).' GMT');
echo date('s');
?>
<hr>
<a href='re_header.php'>self</a>
- 操作响应主体:任何输出
//文件下载
<?php
$file = './2015-10-21_150123.png';
header('Content-Disposition:Attachment;filename='.basename($file));
$finfo = new Finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($file);
//echo $mime;
header('Content-Type: '.$mime);
header('Content-Length: '.filesize($file));
$mo = 'rb';
$link = fopen($file,$mo);
while(!feof($link)){
echo fgets($link,1024);
}
fclose($link);