测试api代码,简单的接口测试代码

时间:2022-06-08 00:58:54

<html lang=http://www.mamicode.com/"zh-CN">

<head>

    <meta charset=http://www.mamicode.com/"utf-8">

    <title>接口测试</title>

    <link href=http://www.mamicode.com/"http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel=http://www.mamicode.com/"stylesheet">

    <link href=http://www.mamicode.com/"http://cdn.bootcss.com/font-awesome/4.1.0/css/font-awesome.min.css" rel=http://www.mamicode.com/"stylesheet">

    <link href=http://www.mamicode.com/"http://static.bootcss.com/www/assets/css/site.min.css?v3" rel=http://www.mamicode.com/"stylesheet">

    <link href=http://www.mamicode.com/"http://static.bootcss.com/www/assets/ico/favicon.png" rel=http://www.mamicode.com/"shortcut icon">

    <script src=http://www.mamicode.com/"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=http://www.mamicode.com/"container">

        <div class=http://www.mamicode.com/"row row-offcanvas row-offcanvas-right">

            <div class=http://www.mamicode.com/"col-xs-12 col-sm-12">

                <div class=http://www.mamicode.com/"row" >

                    <div class=http://www.mamicode.com/"col-xs-1 col-lg-4">

                        <h1>接口测试</h1>