search
Homephp教程PHP源码测试api代码,简单的接口测试代码。

php代码

<html>
<head>
    <meta charset="utf-8">
    <title>接口测试</title>
    <link href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css?1.1.6" rel="stylesheet">
    <link href="http://cdn.bootcss.com/font-awesome/4.1.0/css/font-awesome.min.css?1.1.6" 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?1.1.6"></script>
</head>
<body>
<?php
header(&#39;Content-type:text/html;charset=utf-8&#39;);
function fly_curl($url, $postFields = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, &#39;Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.1)&#39;);
    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[&#39;submit&#39;])) {
    if (strstr($_POST[&#39;url&#39;], &#39;?&#39;)) {
        $url = sprintf("%s&auth=%s", $_POST[&#39;url&#39;], $auth);
    } else {
        $url = sprintf("%s?auth=%s", $_POST[&#39;url&#39;], $auth);
    }
    $param = array();
    if (isset($_POST[&#39;param&#39;])) {
        foreach($_POST[&#39;param&#39;] as $k => $item) {
            if (!empty($item[&#39;method&#39;]) && !empty($item[&#39;name&#39;])) {
                $param[$item[&#39;method&#39;]][$item[&#39;name&#39;]] = $item[&#39;value&#39;];
            }
        }
    }
    
    if (isset($param[&#39;get&#39;]) && !empty($param[&#39;get&#39;])) {
        foreach ($param[&#39;get&#39;] as $name => $value) {
            $url = sprintf("%s&%s=%s", $url, $name, $value);
        }
    }
    $post_data = null;
    if (isset($param[&#39;post&#39;]) && !empty($param[&#39;post&#39;])) {
        $post_data = $param[&#39;post&#39;];
    }
    
    $start =  microtime_float();
    $return = fly_curl($url, $post_data);
    $content = json_decode(urldecode($return), TRUE);
    if ( ! $content) {
        $content = $return;
    }
    $end =  microtime_float();
}
?>


    <p>
        <p class="row row-offcanvas row-offcanvas-right">
            <p class="col-xs-12 col-sm-12">
                <p >
                    <p class="col-xs-1 col-lg-4">
                        <h1>接口测试</h1>
                        <p>
                        <form action="" method="post">
                            <b>请填URL</b>:
                            <input value="<?php echo isset($_POST[&#39;url&#39;])?$_POST[&#39;url&#39;]:&#39;&#39;;?>" placeholder="填写完整地址,以http://开头" type="text" name="url" required><br>
                            <?php if (isset($_POST[&#39;param&#39;]) && !empty($_POST[&#39;param&#39;])) :?>
                                <?php foreach ($_POST[&#39;param&#39;] as $k => $item) :?>
                                    <?php if (!empty($item[&#39;method&#39;]) && !empty($item[&#39;name&#39;])) :?>
                                        <p>
                                            <b>参数name</b>:
                                            <input value="<?php echo $item[&#39;name&#39;];?>" placeholder="请填写" type="text" name="param[<?php echo $k;?>][name]"><br>
                                            <b>参数value</b>:
                                            <input value="<?php echo $item[&#39;value&#39;];?>" placeholder="请填写" type="text" name="param[<?php echo $k;?>][value]"><br>
                                            <b>请求方式</b>:
                                            <label><input <?php if($item[&#39;method&#39;]==&#39;get&#39;):?>checked<?php endif;?> value="get" type="radio" name="param[<?php echo $k;?>][method]">get</label>
                                            <label><input <?php if($item[&#39;method&#39;]==&#39;post&#39;):?>checked<?php endif;?> value="post" type="radio" name="param[<?php echo $k;?>][method]">post</label><br />
                                            <a href="#" onclick="del_param(this)">删除</a>
                                        </p>
                                    <?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>
                        </p>
                    </p>
                    <p class="col-xs-1 col-lg-8">
                        <?php
                            if (isset($_POST[&#39;submit&#39;])) {
                                echo "<pre class="brush:php;toolbar:false">";
                                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[&#39;result&#39;])) {
                                    echo "<br />code:";
                                    var_dump($content[&#39;result&#39;][&#39;code&#39;]);
                                    echo "message:";
                                    var_dump($content[&#39;result&#39;][&#39;message&#39;]);
                                    echo "data:";
                                    var_dump($content[&#39;result&#39;][&#39;data&#39;]);
                                } else {
                                    echo $content;
                                }
                                
                                echo "
"; } ?>


<script> $("#add_param").click(function(){ var input_len = $("form input").size(); input_len++; $(this).before(&#39;\ <p>\ <b>参数name</b>:\ <input value="" placeholder="请填写" type="text" name="param[&#39;+ input_len +&#39;][name]"><br>\ <b>参数value</b>:\ <input value="" placeholder="请填写" type="text" name="param[&#39;+ input_len +&#39;][value]"><br>\ <b>请求方式</b>:\ <label><input checked value="get" type="radio" name="param[&#39;+ input_len +&#39;][method]">get</label>\ <label><input value="post" type="radio" name="param[&#39;+ input_len +&#39;][method]">post</label><br />\ <a href="#" onclick="del_param(this)">删除</a>\ </p>\ &#39;); }); function del_param(obj) { $(obj).parent().remove(); } </script>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.