http://www.oschina.net/code/snippet_1408874_43829
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>接口测试</title>
    <link href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="http://static.bootcss.com/www/assets/css/site.min.css?v3" rel="stylesheet">
    <link href="http://static.bootcss.com/www/assets/ico/favicon.png" rel="shortcut icon">
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<?php
header('Content-type:text/html;charset=utf-8');
function fly_curl($url, $postFields = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.1)');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FAILONERROR, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
    if (is_array($postFields) && 0 < count($postFields))
    {
        $postBodyString = "";
        $postMultipart = false;
        foreach ($postFields as $k => $v)
        {
            if("@" != substr($v, 0, 1))//判断是不是文件上传
            {
                $postBodyString .= "$k=" . urlencode($v) . "&";
            }
            else//文件上传用multipart/form-data,否则用www-form-urlencoded
            {
                $postMultipart = true;
            }
        }
        unset($k, $v);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($postMultipart)
        {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        }
        else
        {
            //var_dump($postBodyString);
            curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
        }
    }
    $reponse = curl_exec($ch);
    //return curl_getinfo($ch);
    if (curl_errno($ch))
    {
        throw new Exception(curl_error($ch),0);
    }
    else
    {
        $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (200 !== $httpStatusCode)
        {
            throw new Exception($reponse,$httpStatusCode);
        }
    }
    curl_close($ch);
    return $reponse;
}
function microtime_float(){
    list ($usec, $sec) = explode(" ", microtime());
    return ((float) $usec + (float) $sec);
}
 
$start = $end = 0;
if (isset($_POST['submit'])) {
    if (strstr($_POST['url'], '?')) {
        $url = sprintf("%s&auth=%s", $_POST['url'], $auth);
    } else {
        $url = sprintf("%s?auth=%s", $_POST['url'], $auth);
    }
    $param = array();
    if (isset($_POST['param'])) {
        foreach($_POST['param'] as $k => $item) {
            if (!empty($item['method']) && !empty($item['name'])) {
                $param[$item['method']][$item['name']] = $item['value'];
            }
        }
    }
     
    if (isset($param['get']) && !empty($param['get'])) {
        foreach ($param['get'] as $name => $value) {
            $url = sprintf("%s&%s=%s", $url, $name, $value);
        }
    }
    $post_data = null;
    if (isset($param['post']) && !empty($param['post'])) {
        $post_data = $param['post'];
    }
     
    $start =  microtime_float();
    $return = fly_curl($url, $post_data);
    $content = json_decode(urldecode($return), TRUE);
    if ( ! $content) {
        $content = $return;
    }
    $end =  microtime_float();
}
?>
 
 
    <div class="container">
        <div class="row row-offcanvas row-offcanvas-right">
            <div class="col-xs-12 col-sm-12">
                <div class="row" >
                    <div class="col-xs-1 col-lg-4">
                        <h1>接口测试</h1>
                        <div class="thumbnail">
                        <form class="form-signin" action="" method="post">
                            <b>请填URL</b>:
                            <input value="<?php echo isset($_POST['url'])?$_POST['url']:'';?>" class="form-control" placeholder="填写完整地址,以http://开头" type="text" name="url" required><br>
                            <?php if (isset($_POST['param']) && !empty($_POST['param'])) :?>
                                <?php foreach ($_POST['param'] as $k => $item) :?>
                                    <?php if (!empty($item['method']) && !empty($item['name'])) :?>
                                        <div class="thumbnail">
                                            <b>参数name</b>:
                                            <input value="<?php echo $item['name'];?>" placeholder="请填写" type="text" name="param[<?php echo $k;?>][name]"><br>
                                            <b>参数value</b>:
                                            <input value="<?php echo $item['value'];?>" placeholder="请填写" type="text" name="param[<?php echo $k;?>][value]"><br>
                                            <b>请求方式</b>:
                                            <label><input <?php if($item['method']=='get'):?>checked<?php endif;?> value="get" type="radio" name="param[<?php echo $k;?>][method]">get</label>
                                            <label><input <?php if($item['method']=='post'):?>checked<?php endif;?> value="post" type="radio" name="param[<?php echo $k;?>][method]">post</label><br />
                                            <a href="#" onclick="del_param(this)">删除</a>
                                        </div>
                                    <?php endif;?>
                                <?php endforeach;?>
                            <?php endif;?>
                             
                            <input type="button" name="add_param" id="add_param" value="添加参数" class="btn btn-lg btn-primary btn-block"><br />
                            <input type="submit" name="submit" value="测试" class="btn btn-lg btn-primary btn-block"><br />
                        </form>
                        </div>
                    </div>
                    <div class="col-xs-1 col-lg-8">
                        <?php
                            if (isset($_POST['submit'])) {
                                echo "<pre>";
                                echo "请求时间:";
                                var_dump($end - $start);
                                 
                                echo "<br />请求url:";
                                isset($url) && var_dump($url);
                                 
                                echo "<br />请求参数:";
                                isset($param) && var_dump($param);
                                 
                                echo "<hr />结果:";
                                if (isset($content['result'])) {
                                    echo "<br />code:";
                                    var_dump($content['result']['code']);
                                    echo "message:";
                                    var_dump($content['result']['message']);
                                    echo "data:";
                                    var_dump($content['result']['data']);
                                } else {
                                    echo $content;
                                }
                                 
                                echo "</pre>";
                            }
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <hr />
    </div>
    <div class="blog-masthead">
        <div class="container">
            <nav class="blog-nav">
                <p class="blog-nav-item">&copy; Company 2014</p>
            </nav>
        </div>
    </div>
</body>
</html>
 
<script>
    $("#add_param").click(function(){
        var input_len = $("form input").size();
        input_len++;
        $(this).before('\
            <div class="thumbnail">\
                <b>参数name</b>:\
                <input value="" placeholder="请填写" type="text" name="param['+ input_len +'][name]"><br>\
                <b>参数value</b>:\
                <input value="" placeholder="请填写" type="text" name="param['+ input_len +'][value]"><br>\
                <b>请求方式</b>:\
                <label><input checked value="get" type="radio" name="param['+ input_len +'][method]">get</label>\
                <label><input value="post" type="radio" name="param['+ input_len +'][method]">post</label><br />\
                <a href="#" onclick="del_param(this)">删除</a>\
            </div>\
        ');
    });
    function del_param(obj) {
        $(obj).parent().remove();
    }
</script>

测试api代码,简单的接口测试代码的更多相关文章

  1. HTML QQ聊天代码 简单的一行代码

    简单的一行代码: <a href="tencent://message/?uin=173007740&Site=&Menu=yes">和17300774 ...

  2. API测试之Postman使用全指南(原来使用 Postman测试API如此简单)

    Postman Postman是一个可扩展的API开发和测试协同平台工具,可以快速集成到CI/CD管道中.旨在简化测试和开发中的API工作流. Postman 工具有 Chrome 扩展和独立客户端, ...

  3. demo工程的清单文件及activity中api代码简单示例

    第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...

  4. 06-编写Hibernate API编写访问数据库的代码,使用Junit进行测试

    用到的注解: @Test:测试方法 @Before:初始化方法. @After:是否资源. 先执行Befere,然后执行Test,最后执行After. 第一步:新建一个Junit目录. 第二步:取名 ...

  5. API Studio 5.1.2 版本更新:加入全局搜索、支持批量测试API测试用例、读取代码注解生成文档支持Github与码云等

    最近在EOLINKER的开发任务繁重,许久在博客园没有更新产品动态了,经过这些日子,EOLINKER又有了长足的进步,增加了更多易用的功能,比如加入全局搜索.支持批量测试API测试用例.读取代码注解生 ...

  6. 让 API 测试变的简单。

    做开发已经四年有余了,之前在接口测试的时候最开始用的自己写的测试类进行测试,后来接触到了 postman 和 swagger ,虽然用起来比自己写的强太多了,但是总觉得差点事儿. 一方面是 postm ...

  7. 【sklearn决策树算法】DecisionTreeClassifier(API)的使用以及决策树代码实例 - 鸢尾花分类

    决策树算法 决策树算法主要有ID3, C4.5, CART这三种. ID3算法从树的根节点开始,总是选择信息增益最大的特征,对此特征施加判断条件建立子节点,递归进行,直到信息增益很小或者没有特征时结束 ...

  8. C# 防止同时调用=========使用读写锁三行代码简单解决多线程并发的问题

    http://www.jb51.net/article/99718.htm     本文主要介绍了C#使用读写锁三行代码简单解决多线程并发写入文件时提示"文件正在由另一进程使用,因此该进程无 ...

  9. DCGAN 代码简单解读

    之前在DCGAN文章简单解读里说明了DCGAN的原理.本次来实现一个DCGAN,并在数据集上实际测试它的效果.本次的代码来自github开源代码DCGAN-tensorflow,感谢carpedm20 ...

随机推荐

  1. java 级联删除文件夹下的所有文件

    public void deletefile(String delpath) throws Exception { try { File file = new File(delpath); // 当且 ...

  2. Android first---常见布局

    ###绝对布局AbsoluteLayout    * android:layout_x="120dp"   在水平方向上偏移120像素     * android:layout_y ...

  3. Web Performance Test : IP切换/IP欺骗

    译者前言 本文翻译自<Visual Studio Performance Testing Quick Reference Guide 3.6> 有关于IP Switching的章节.< ...

  4. hadoop2.0初识1.0

    1.给普通用户设置sudo权限 编辑:[root@life-hadoop /]# nano /etc/sudoers 在文件头部加入:yanglin ALL=(root)NOPASSWD:ALL 保存 ...

  5. Cheatsheet: 2016 01.01 ~ 01.31

    Mobile An Introduction to Cordova: Basics Web Angular 2 versus React: There Will Be Blood How to Bec ...

  6. C#读取XML文件中有乱码的处理办法

    1.以文本的方式读取出xml内容 2.如果xml加载文本失败,替换掉乱码的内容 private static void loadxml(XmlDocument doc, string str) { t ...

  7. Maven学习(四)-- 生命周期和插件

    标签(空格分隔): 学习笔记 Maven生命周期是抽象的,不做任何实际的工作,在Maven的设计中,实际的任务都交由插件来完成. 每个构件步骤都可以绑定一个或者多个插件行为,而且Maven为大多数构建 ...

  8. 【转载】USB2.0接口差分信号线设计

    引 言 通用串行总线(Universal Serial Bus)从诞生发展到今天,USB协议已从1.1过渡到2.0,作为其重要指标的设备传输速度,从1.5 Mbps:的低速和12 Mbps的全速,提高 ...

  9. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. Android Studio build dex jar

    Gradle配置 Build配置文件gradle.build中添加如下task task clearJar(type: Delete) { delete 'build/outputs/mylib.ja ...