1 Star 0 Fork 53

汉科软/Tester

forked from Hamm/Tester 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api.php 4.13 KB
一键复制 编辑 原始数据 按行查看 历史
Hamm 提交于 2021-09-23 09:17 . fix: JSON空请求的BUG
<?php
header("Content-Type: application/json;chartset=uft-8");
if (isset($_GET['key'])) {
$key = htmlspecialchars($_GET['key']);
$path = "./data/" . $key . ".php";
$result = '';
if (file_exists($path)) {
$result = file_get_contents($path);
}
jok('', $result);
die;
}
$postStr = file_get_contents('php://input');
$post = json_decode($postStr, true);
if (!$post) {
jerr('参数错误');
}
$url = $post['url'];
if (!$url) {
jerr('请填写API请求地址');
}
$method = $post['method'] ?? 'GET';
$body = $post['body'] ?? "";
$header = $post['header'] ?? "";
$cookie = $post['cookie'] ?? "";
$contentType = $post['contentType'] ?? "application/json; charset=utf-8";
if (substr($header, 0 - strlen(PHP_EOL)) == PHP_EOL) {
$header = substr($header, 0, strlen($header) - strlen(PHP_EOL));
}
if($header){
$header = explode(PHP_EOL, $header);
}else{
$header = [];
}
$key = sha1($postStr);
$dir = "./data/" . date('Ymd');
if (!is_dir($dir)) {
mkdir($dir);
}
file_put_contents($dir . "/" . $key . ".php", json_encode([
"url" => $url,
"body" => $body,
"header" => $header,
"cookie" => $cookie,
"method" => $method,
"contentType" => $contentType,
]));
if (isset($_GET['local'])) {
return jok('', date('Ymd') . "/" . $key);
}
for($i=0;$i<count($header);$i++){
if(empty($header)){
array_splice($header,$i);
}
}
switch($contentType){
case 'application/json;':
if($body){
$body = json_encode(json_decode($body,true));
}else{
$body = '{}';
}
$contentType = 'application/json; charset=utf-8';
break;
default;
}
$header[] = 'Content-Type: '.$contentType;
$header[] = 'Content-Length: ' . strlen($body);
$result = curlHelper($url, $body, $header, $cookie, $method);
$result['key'] = date('Ymd') . "/" . $key;
jok('', $result);
function jok($msg, $data)
{
echo json_encode([
'code' => 200,
'msg' => $msg,
'data' => $data,
]);
die;
}
function jerr($msg)
{
echo json_encode([
'code' => 500,
'msg' => $msg,
]);
die;
}
function curlHelper($url, $data = null, $header = [], $cookies = "", $method = 'GET')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
switch ($method) {
case "GET":
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case "PATCH":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case "TRACE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "TRACE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case "OPTIONS":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "OPTIONS");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case "HEAD":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
default:
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
$output = [];
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容
$output['header'] = substr($response, 0, $headerSize);
$output['body'] = substr($response, $headerSize, strlen($response) - $headerSize);
$output['detail'] = curl_getinfo($ch);
curl_close($ch);
return $output;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/hanke-soft/tester.git
git@gitee.com:hanke-soft/tester.git
hanke-soft
tester
Tester
master

搜索帮助