PHP之Http请求
来源:互联网 发布:mac键盘大小写灯不亮 编辑:程序博客网 时间:2024/06/05 11:10
Http:超文本传输协议(英文:HyperText Transfer
Protocol,缩写:HTTP)是互联网上应用最为广泛的一种网络协议。
http请求协议
- 请求行
Request URL:http://www.hotel.wsq.com/Request Method:GETStatus Code:200 OKRemote 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:1User-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.compassword: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 FoundRemote 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=0Connection:Keep-AliveContent-Length:383Content-Type:text/htmlDate:Wed, 06 Apr 2016 02:53:54 GMT//设置缓存Expires:Thu, 19 Nov 1981 08:52:00 GMTKeep-Alive:timeout=5, max=100Location:index.php?p=admin&c=index&a=index&l=1Pragma:no-cacheServer:Apache/2.4.10 (Win32) OpenSSL/0.9.8zb PHP/5.3.29X-Powered-By:PHP/5.3.29
- 响应主体
通过浏览器查看源代码看到的信息
操作响应
- 操作响应头:header()
//操作缓存时间<?phpheader('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);
0 0
- PHP之Http请求
- PHP安全编程之HTTP请求欺骗
- php中Http请求!
- php请求http
- PHP模拟http请求
- PHP请求http(file_get_content)
- PHP 发送HTTP请求
- php 发送 http 请求
- php异步http请求
- php模拟http请求
- php http请求
- PHP模拟HTTP请求
- PHP模拟http请求
- php模拟http请求
- PHP HTTP请求签名
- PHP Laravel Http请求
- php之curl实现http与https请求的方法
- PHP之curl实现http与https请求的
- 网易公开课《Linux内核分析》学习心得-Linux内核如何装载和启动一个可执行程序
- SQL Server 存储过程
- 存储过程的优缺点
- 视图、索引、存储过程优缺点
- The remote SSH server rejected X11 forwarding request
- PHP之Http请求
- liunx 下scp出现 Permission denied, please try again 的解决办法
- 概率分布及其统计特性
- LeetCode 233. Number of Digit One
- 230. Kth Smallest Element in a BST
- LeetCode 234. Palindrome Linked List(对称链表)
- [NumPy]入门-学习笔记
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最低公共祖先)
- 字符串中数字子串的求和