I have been writing API interfaces recently. Every time I write an interface, I need to test it myself first to see if there are any syntax errors and whether the requested data is correct. However, many of them are POST requests and I cannot directly open the link in the browser for testing. , so there must be a simulation tool that can send HTTP requests locally to simulate data requests.
This is what I did at the beginning, create a file in the local wampserver running directory, write Curl requests in it, and conduct simulated request tests, but each interface needs The parameters are all different. I need to constantly modify the requested parameters and API, which is very inconvenient. Later, I couldn’t distinguish the messy data in my request file:
I searched for related tools on the Internet. There are many online tests, such as: ATOOL online tools, Apizza, etc., I looked at them and they all do a good job, very easy to use, the interface is beautiful, and the service is very considerate. But I am considering security issues, and at the same time it gives meThe data returned is the original JSON format, I am used to looking at arrays The format is relatively intuitive.
So, in line with the concept of having enough food and clothing by myself, I wrote a simple API test page locally. After submitting the data, I implemented the API request testing function locally. I didn't need to consider security issues, and I could convert the results at will. Only two files are needed to complete it, one is the page post.html for filling in the data, and the other is the post.php file that receives the data from the post.html page and processes the request to implement the function.
1. The front-end page file post.html
is just a simple page, no complex layout, no JS special effects, just write There are 6 parameters, which are generally enough. If not, you can add them yourself. By default, body request parameters are passed here, and only GET and POST are used for request methods.
<html xmlns="http://blog.csdn.net/sinat_35861727?viewmode=contents"> <head> <meta http-equiv = "Content-Type" content = "text/html;charset = utf8"> <meta name = "description" content = "提交表单"> <title>API接口请求表单</title> </head> <style type="text/css"> .key1{ width:100px; } .value1{ width:230px; margin:0 0 0 10px; } .main{ margin:0 auto; width:450px; height:auto; background:lightgray; padding:40px 40px; } .refer{ width:100px; height:24px; } .url{ width:350px; } </style> <body> <p class="main"> <form method="POST" action="post.php" target="_blank"> <p>请求地址:<input class="url" type="text" name="curl" placeholder="API接口地址"></p> <p>参 数1: <input class="key1" type="text" name="key1" placeholder="参数名"> <input class="value1" type="text" name="value1" placeholder="参数值"></p> <p>参 数2: <input class="key1" type="text" name="key2" placeholder="参数名"> <input class="value1" type="text" name="value2" placeholder="参数值"></p> <p>参 数3: <input class="key1" type="text" name="key3" placeholder="参数名"> <input class="value1" type="text" name="value3" placeholder="参数值"></p> <p>参 数4: <input class="key1" type="text" name="key4" placeholder="参数名"> <input class="value1" type="text" name="value4" placeholder="参数值"></p> <p>参 数5: <input class="key1" type="text" name="key5" placeholder="参数名"> <input class="value1" type="text" name="value5" placeholder="参数值"></p> <p>参 数6: <input class="key1" type="text" name="key6" placeholder="参数名"> <input class="value1" type="text" name="value6" placeholder="参数值"></p> <p>请求方式: <select name="method"> <option value="POST">POST请求</option> <option value="GET">GET请求</option> </select></p> <p style="text-align:center;"><input class="refer" type="submit" value="提交"></p> </form> </p> </body> </html>
2. Data processing file post.php
Receives the data from the post.html page, sends a request and then processes the request result. What is passed from the front-end page is the Body Request parameters, if you still need Header parameters, you can add them manually in this file.
<?php echo '<title>API接口请求响应</title>'; /** * 设置网络请求配置 * @param [string] $curl 请求的URL * @param [bool] true || false 是否https请求 * @param [string] $method 请求方式,默认GET * @param [array] $header 请求的header参数 * @param [object] $data PUT请求的时候发送的数据对象 * @return [object] 返回请求响应 */ function ihttp_request($curl,$https=true,$method='GET',$header=array(),$data=null){ // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, $curl); //要访问的网站 //curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($https){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); } if($method == 'POST'){ curl_setopt($ch, CURLOPT_POST, true); //发送 POST 请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } // 抓取URL并把它传递给浏览器 $content = curl_exec($ch); if ($content === false) { return "网络请求出错: " . curl_error($ch); exit(); } //关闭cURL资源,并且释放系统资源 curl_close($ch); return $content; } //检查是否是链接格式 function checkUrl($C_url){ $str="/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/"; if (!preg_match($str,$C_url)){ return false; }else{ return true; } } //检查是不是HTTPS function check_https($url){ $str="/^https:/"; if (!preg_match($str,$url)){ return false; }else{ return true; } } if($_SERVER['REQUEST_METHOD'] != 'POST') exit('请求方式错误!'); //发送请求 function curl_query(){ $data = array( $_POST['key1'] => $_POST['value1'], $_POST['key2'] => $_POST['value2'], $_POST['key3'] => $_POST['value3'], $_POST['key4'] => $_POST['value4'], $_POST['key5'] => $_POST['value5'], $_POST['key6'] => $_POST['value6'] ); //数组去空 $data = array_filter($data); //post请求的参数 if(empty($data)) exit('请填写参数'); $url = $_POST['curl']; //API接口 if(!checkUrl($url)) exit('链接格式错误'); //检查连接的格式 $is_https = check_https($url); //是否是HTTPS请求 $method = $_POST['method']; //请求方式(GET POST) $header = array(); //携带header参数 //$header[] = 'Cache-Control: max-age=0'; //$header[] = 'Connection: keep-alive'; if($method == 'POST'){ $res = ihttp_request($url,$is_https,$method,$header,$data); print_r(json_decode($res,true)); }else if($method == 'GET'){ $curl = $url.'?'.http_build_query($data); //GET请求参数拼接 $res = ihttp_request($curl,$is_https,$method,$header); print_r(json_decode($res,true)); }else{ exit('error request method'); } } curl_query(); ?>
The writing is very simple, and the functions are not very comprehensive. The POST and GET requests under normal circumstances can still be satisfied. At least the local test results are no problem. Friends who need it can download the code. , and then modify and improve the functions according to your own needs.
Related recommendations:
PHP API interface testing gadget
The above is the detailed content of PHP API interface testing. For more information, please follow other related articles on the PHP Chinese website!

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
