PHP Simulation HTTP Request(undone)
目录
. 引言
. file_get_contents版本
. Socket版本
. Curl版本
. Curl版本()
. 模拟文件上传
0. 引言
本文总结了通过PHP代码方式模拟各种HTTP请求
1. file_get_contents版本
<?php
/**
* 发送post请求
* @param string $url 请求地址
* @param array $post_data post键值对数据
* @return string
*/
function send_post($url, $post_data)
{
//使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => * // 超时时间(单位:s)
)
);
//创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context); return $result;
} $post_data = array(
'username' => 'zhenghan',
'password' => ''
);
$result = send_post('http://localhost/test/index.php', $post_data);
echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.http-build-query.php
http://php.net/manual/zh/function.stream-context-create.php
2. Socket版本
<?php
/**
* Socket版本
*/
function request_by_socket($remote_server, $remote_path, $post_string, $port = , $timeout = )
{
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket)
{
die("$errstr($errno)");
}
fwrite($socket, "POST $remote_path HTTP/1.0");
fwrite($socket, "User-Agent: Socket Example");
fwrite($socket, "HOST: $remote_server");
fwrite($socket, "Content-type: application/x-www-form-urlencoded");
fwrite($socket, "Content-length: " . (strlen($post_string) + ) . "");
fwrite($socket, "Accept:*/*");
fwrite($socket, "");
fwrite($socket, "mypost=$post_string");
fwrite($socket, "");
$header = "";
while ($str = trim(fgets($socket, )))
{
$header .= $str;
} $data = "";
while (!feof($socket))
{
$data .= fgets($socket, );
} return $data;
} $post_string = "app=socket&version=beta";
$result = request_by_socket('localhost', '/test.php', $post_string); echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.fsockopen.php
3. Curl版本
<?php
/**
* Curl版本
*/
function request_by_curl($remote_server, $post_string)
{
//初始化一个新的会话,返回一个cURL句柄,供curl_setopt(), curl_exec()和curl_close() 函数使用。
$ch = curl_init();
//curl_setopt — 设置一个cURL传输选项
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "http://littlehann.cnblogs.com CURL Example beta");
//curl_exec — 执行一个cURL会话,这个函数应该在初始化一个cURL会话并且全部的选项都被设置后被调用。
$data = curl_exec($ch);
//关闭一个cURL会话并且释放所有资源。cURL句柄ch 也会被释放。
curl_close($ch); return $data;
} $post_string = "app=request&version=beta";
$result = request_by_curl('http://localhost/test.php', $post_string);
echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.curl-init.php
http://php.net/manual/zh/function.curl-setopt.php
http://php.net/manual/zh/function.curl-exec.php
http://blog.51yip.com/php/1039.html
4. Curl版本(2)
<?php
/**
* 发送HTTP请求
*
* @param string $url 请求地址
* @param string $method 请求方式 GET/POST
* @param string $refererUrl 请求来源地址
* @param array $data 发送数据
* @param string $contentType
* @param string $timeout
* @param string $proxy
*/
function send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application/json', $timeout = , $proxy = false)
{
$ch = null;
if('POST' === strtoupper($method))
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, );
curl_setopt($ch, CURLOPT_HEADER, );
curl_setopt($ch, CURLOPT_FRESH_CONNECT, );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, );
curl_setopt($ch, CURLOPT_FORBID_REUSE, );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($refererUrl)
{
curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
}
if($contentType)
{
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
}
if(is_string($data))
{
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else
{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
}
else if('GET' === strtoupper($method))
{
if(is_string($data))
{
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
}
else
{
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
} $ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_HEADER, );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, );
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($refererUrl)
{
curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
}
}
else
{
//返回一个数组,其中每个元素都是目前用户自定义函数的参数列表的相应元素的副本
$args = func_get_args();
return false;
} if($proxy)
{
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$ret = curl_exec($ch);
//获取最后一次传输的相关信息。
$info = curl_getinfo($ch);
$contents = array(
'httpInfo' => array(
'send' => $data,
'url' => $url,
'ret' => $ret,
'http' => $info
)
); curl_close($ch);
return $contents;
} $data = array( => "hello world!");
$r_url = "http://localhost/test.php";
$result = send_request($r_url, json_encode($data), NULL, 'POST');
echo $result; ?>
Relevant Link:
http://php.net/manual/zh/function.curl-getinfo.php
http://blog.snsgou.com/post-161.html
http://www.cnblogs.com/simpman/p/3549816.html
5. 模拟文件上传
sender.php
<?php
function getFileList($directory)
{
$files = array();
if(is_dir($directory))
{
if($dh = opendir($directory))
{
while(($file = readdir($dh)) !== false)
{
if($file != '.' && $file != '..' && $file !== "rule.php" && $file !== "filter.php" && $file !== "vul_rules.json" && $file !== "bad" && $file !== "good")
{
$files[] = $file;
}
}
closedir($dh);
}
}
return $files;
} /**
* 发送post请求
* @param $post_data 待上传文件
* @param $post_url 文件上传地址
* @return string
*/
function file_post($post_data, $post_url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_POST, );
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, );
curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0");
$result = curl_exec($curl);
$error = curl_error($curl);
return $error ? $error : $result;
} ///遍历bad目录,逐个文件上传
$files = getFileList("./bad");
foreach ($files as $key => $value)
{
//上传文件
$url = "http://localhost/test/index.php?action=uploadfixfile";
$data = array(
"file" => "@" . dirname(__FILE__) . "\\bad\\$value"
);
$result .= file_post($data, $url);
} echo $result; ?>
receiver.php
<?php
if ($_GET['action'] == "uploadfixfile")
{
if($_FILES)
{
$filename = $_FILES['file']['name'];
$tmpname = $_FILES['file']['tmp_name'];
$newname = dirname(__FILE__) . '\\fix\\' . $filename;
//die(var_dump($newname)); if(move_uploaded_file($tmpname, $newname))
{
echo "$filename 上传成功" . "</br>";
}
else
{
echo "$filename 上传失败" . "</br>";
}
}
} ?>
Relevant Link:
http://flashphp.org/blog/2010/03/php%E4%BD%BF%E7%94%A8curl%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E7%9A%84%E5%87%BD%E6%95%B0/
http://book.51cto.com/art/201404/437024.htm
http://my.oschina.net/adamboy/blog/54436
Copyright (c) 2014 LittleHann All rights reserved
PHP Simulation HTTP Request(undone)的更多相关文章
- IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...
- ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)
致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation kamtoa simulation学习与示例分析(一) 源码学习与分析是学习ROS,包 ...
- BUAAOO P5-P7 Elevator Simulation
目录 Abstract Introduction Topic Request Elevator Analysis Reading Requests Coordinating Scheduling an ...
- Go 语言相关的优秀框架,库及软件列表
If you see a package or project here that is no longer maintained or is not a good fit, please submi ...
- Cygwin Run in the Windows(Simulation of UNIX)
Preface Environment Cygwin Run in the Windows(Simulation of UNIX) Resource Cygwin Install:http://cyg ...
- Airport Simulation (数据结构与算法 – 队列 / Queue 的应用)
Airport Simulation 是数据结构与算法教材中用于演示Queue的一个小程序(大多数教师似乎会跳过这个练习).主程序会通过输入总的运行时间.队列里可以等待的最多飞机数量,平均每个时间单元 ...
- Concepts:Request 和 Task
当SQL Server Engine 接收到Session发出的Request时,SQL Server OS将Request和Task绑定,并为Task分配一个Workder.在TSQL Query执 ...
- 解决托管在Windows上的Stash的Pull request无法合并的问题
最近尝试合并一个托管在Windows的Stash系统中的pull request时,发现合并按钮被禁用,显示有冲突不能合并,但是在diff页面中没有现实冲突,而且代码实际上并没有任何冲突. 后来在这篇 ...
- Lesson 16 A polite request
Text If you park your car in the wrong place, a traffic policeman will soon find it. You will be ver ...
随机推荐
- java 14-11 对象数组
有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息. 创建学生类: 学生:Student 成员变量:name,age 构造方法:无参,带参 成员方法:getXxx()/ ...
- Android优化——UI优化(一)优化布局层次
优化布局层次 1.避免布局镶嵌过深(如下) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- HTML5和css3的总结三
继续总结H5的新东西 1>序列化与反序列化 序列化:其实就是一个json->string的过程 JSON.stringify(json); 反序列化:string->json的过程( ...
- nvl函数
NVL(E1, E2)的功能为:如果E1为NULL,则函数返回E2,否则返回E1本身. 但此函数有一定局限,所以就有了NVL2函数. NVL2(E1, E2, E3)的功能为:如果E1为NULL,则函 ...
- 对比git rm和rm的使用区别
在这里说一下git rm和rm的区别,虽然觉得这个问题有点肤浅,但对于刚接触git不久的朋友来说还是有必要的. 用 git rm 来删除文件,同时还会将这个删除操作记录下来:用 rm 来删除文件,仅仅 ...
- [转]iptables详解
FROM : http://blog.chinaunix.net/uid-26495963-id-3279216.html 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的 ...
- 通过数据库方式访问excel 2007及其以后(xlsx)文件的连接字符串
sqlconn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test3.xls;Extended Properties=&q ...
- Java系列:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
Javaweb工程类中没有添加Tomcat运行时相关类导致. 下面是具体的解决方法: 1.右击web工程->属性或Build Path->Java Build Path->Libra ...
- [Android] emualtor-5554 offline的解决方法
现象:用adb devices命令总发现emualtor-5554 offline,在.android目录下面并没有发现这个设备,没法删除.原因:有程序占用5555端口,导致adb认为5554不能作为 ...
- 解决方法:未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
win7或win8 64位调试程序,出现这样的错误提示:未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序 解决方法如下: 方法一:“设置应用程序池默认属性”/“常规”/” ...