PHP Simulation HTTP Request(undone)

时间:2023-03-08 17:14:14
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&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