php模拟http请求 4000
2015-11-22 17:43
609 查看
Php模拟http请求
今天有空,于是总结一下php模拟http请求的方式。如果有错漏,希望大家可以提出来,大家一起分享一起学习。方法一:File_get_contents(只能模拟get请求)
$url = “http://www.baidu.com”;$content = File_get_contents($url);
方法二:Fsockopen
模拟请求行、请求头、空行、请求内容(post请求)Get:
可以用parse_url($url)函数获取url信息
打开链接
$f = fsockopen(‘域名/ip’,端口,$errno,$error,连接时长);
模拟请求
$http = “GET /index.htmlHTTP/1.1 \r\n”;
$http .= “Host:localhost\r\n”;
$http .= “\r\n”;
写入资源
Fwirte($f,$http); //or fputs
获取数据(可能需要遍历)
Fgetc()/fgets()/fread()
处理数据
输出 or 写下(txt/db)
Post:(数据长度和转码协议必须要有)
….
//模拟请求
$http = “POST /index.html HTTP/1.1 \r\n”;
$http .= “Host:localhost\r\n”;
$http .= “Connection:keep-alive\r\n”;
$http .= “Content-length:43\r\n”; //长度最好与数据长度一致(要求知道接口返回的数据长度)
$http .= “Content-type:application/x-www-form”;
$http .= “\r\n”
$http .= “u_name=george&password=123”;
...
方法三:curl
$url
$ch = Curl_init($url);
配置(具体可以网上搜索一下curl的配置)
Curl_setopt($ch,XXXXXX)
Curl_setopt($ch,XXXXXX)
…
Curl_setopt($ch,XXXXXX)
$data = curl_exec($ch)
//处理数据
输出 or 写下(txt/db)
//关闭资源
curl_close($ch)
curl_error($ch) //输出错误信息
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- C语言实现的统计php代码行数功能源码(支持文件夹、多目录)
- php xml 入门学习资料