1 Star 0 Fork 1

Sentiger / postman工具数据格式相互转换

加入 Gitee
与超过 800 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
PostManJsonTool.php 2.01 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
石文虎 提交于 2018-06-15 13:06 . 请求信息修改
<?php
/**
* Created by PhpStorm.
* User: shi_wenhu@qq.com
* Date: 2018/6/15
* Time: 11:47
*/
class PostManJsonTool
{
public static function jsonToFormData()
{
$applicationJsonArr = [];
$applicationJson = file_get_contents("php://input");
if ($_SERVER['CONTENT_TYPE'] == 'application/json' && !empty($applicationJson)) {
$applicationJsonArr = json_decode($applicationJson, true);
} else {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$applicationJsonArr = $_POST;
} else {
$applicationJsonArr = $_GET;
}
}
$attr = self::runAttr($applicationJsonArr, '');
$formate = [];
$getData = [];
foreach ($attr as $k => $v) {
$keyArr = explode('/', $k);
$t = '';
foreach ($keyArr as $k2 => $v2) {
if (count($keyArr) > 1) {
if ($k2 == 0) {
$t = $t . '' . $v2 . '';
} else {
$t = $t . '[' . $v2 . ']';
}
} else {
$t = $v2;
}
}
$formate[] = $t . ':' . $v . '';
$getData[$t] =$v;
}
echo "GET格式:\n \n";
echo http_build_query($getData) . "\n";
echo "\n \n \nPOST格式:\n \n";
foreach ($formate as $k => $v) {
echo $v . "\n";
}
}
/**
* 无限极路径转换
* @param $attr
* @param string $strPrevKey
* @return array
*/
protected static function runAttr($attr, $strPrevKey = '')
{
static $temp = [];
foreach ($attr as $k => $v) {
$strNowKey = $strPrevKey ? $strPrevKey . '/' . $k : $k;
if (is_array($v)) {
self::runAttr($v, $strNowKey);
} else {
$temp[$strNowKey] = $v;
}
}
return $temp;
}
}
PostManJsonTool::jsonToFormData();

评论 ( 0 )

你可以在登录后,发表评论

PHP
1
https://gitee.com/sentiger/postman-tool.git
git@gitee.com:sentiger/postman-tool.git
sentiger
postman-tool
postman工具数据格式相互转换
master

搜索帮助