目录

. 引言
. 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&amp;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)的更多相关文章

  1. 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 ...

  2. ROS_Kinetic_29 kamtoa simulation学习与示例分析(一)

    致谢源代码网址:https://github.com/Tutorgaming/kamtoa-simulation kamtoa simulation学习与示例分析(一) 源码学习与分析是学习ROS,包 ...

  3. BUAAOO P5-P7 Elevator Simulation

    目录 Abstract Introduction Topic Request Elevator Analysis Reading Requests Coordinating Scheduling an ...

  4. Go 语言相关的优秀框架,库及软件列表

    If you see a package or project here that is no longer maintained or is not a good fit, please submi ...

  5. Cygwin Run in the Windows(Simulation of UNIX)

    Preface Environment Cygwin Run in the Windows(Simulation of UNIX) Resource Cygwin Install:http://cyg ...

  6. Airport Simulation (数据结构与算法 – 队列 / Queue 的应用)

    Airport Simulation 是数据结构与算法教材中用于演示Queue的一个小程序(大多数教师似乎会跳过这个练习).主程序会通过输入总的运行时间.队列里可以等待的最多飞机数量,平均每个时间单元 ...

  7. Concepts:Request 和 Task

    当SQL Server Engine 接收到Session发出的Request时,SQL Server OS将Request和Task绑定,并为Task分配一个Workder.在TSQL Query执 ...

  8. 解决托管在Windows上的Stash的Pull request无法合并的问题

    最近尝试合并一个托管在Windows的Stash系统中的pull request时,发现合并按钮被禁用,显示有冲突不能合并,但是在diff页面中没有现实冲突,而且代码实际上并没有任何冲突. 后来在这篇 ...

  9. 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 ...

随机推荐

  1. Linux下php安装memcache扩展

    安装环境:CentOS 6.4 php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法. 1.下载 下载地址:http://pecl.php.net/packag ...

  2. C#往线程里传递参数

    Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. Thread (ThreadStart) 初始化 Th ...

  3. 通过TTL值判断系统

    我们做网络的渗透测试,开始的时候会ping一下网站判断一下网站使用的系统,默认情况下, UNIX 255 LINUX 64WIN2K/NT 128WINDOWS 32 但是 每经过一个路由器就会减1这 ...

  4. hydra爆破用法

    -R 根据上一次进度继续破解 -S 使用SSL协议连接 -s 指定端口 -l 指定用户名 -L 指定用户名字典(文件) -p 指定密码破解 -P 指定密码字典(文件) -e 空密码探测和指定用户密码探 ...

  5. javascript中的数组操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  6. ios开发中如何隐藏各种bar

    转载自http://www.cnblogs.com/lovecode/articles/2234557.html 状态条Status Bar [UIApplication sharedApplicat ...

  7. 传递消息--第三方开源--EventBus的简单使用

    EventBus下载地址:https://github.com/greenrobot/EventBus MyEvent: package com.zzw.testeventbus; public cl ...

  8. Linux ssh登录和软件安装详解

    阿哲Style   Linux第一天 ssh登录和软件安装详解 Linux学习第一天 操作环境: Ubuntu 16.04 Win10系统,使用putty_V0.63 本身学习Linux就是想在服务器 ...

  9. sed 4个功能

    [root@lanny test]# cat test.txt test liyao lanny 经典博文: http://oldboy.blog.51cto.com/2561410/949365 h ...

  10. Caffe学习系列(21):caffe图形化操作工具digits的安装与运行

    经过前面一系列的学习,我们基本上学会了如何在linux下运行caffe程序,也学会了如何用python接口进行数据及参数的可视化. 如果还没有学会的,请自行细细阅读: caffe学习系列:http:/ ...