How to test API interface locally
This time I will show you how to test the API interface locally and what are the precautions for testing the API interface locally. The following is a practical case, let's take a look.
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 This is what I did at the beginning. I created a file in the localwampserver running directory, wrote Curl requests in it, and conducted simulated request tests, but every time Each interface requires different parameters. 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:
Online Tools, Apizza, etc. I looked at them and they all do a good job. They are 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.
Ever since, 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 don’t have to consider security issues and can evaluate the results. Convert as you like. 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
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
Receive the data passed by the post.html page, and Send a request and then process the request result. All the body request parameters are passed from the front-end page. 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(); ?>
Access directory service permissions of phpstudy2018
ThinkPHP Detailed explanation of the process tutorial for implementing WeChat payment (jsapi payment) _php example
The above is the detailed content of How to test API interface locally. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What do you think of furmark? 1. Set the "Run Mode" and "Display Mode" in the main interface, and also adjust the "Test Mode" and click the "Start" button. 2. After waiting for a while, you will see the test results, including various parameters of the graphics card. How is furmark qualified? 1. Use a furmark baking machine and check the results for about half an hour. It basically hovers around 85 degrees, with a peak value of 87 degrees and room temperature of 19 degrees. Large chassis, 5 chassis fan ports, two on the front, two on the top, and one on the rear, but only one fan is installed. All accessories are not overclocked. 2. Under normal circumstances, the normal temperature of the graphics card should be between "30-85℃". 3. Even in summer when the ambient temperature is too high, the normal temperature is "50-85℃

The "Inaction Test" of the new fantasy fairy MMORPG "Zhu Xian 2" will be launched on April 23. What kind of new fairy adventure story will happen in Zhu Xian Continent thousands of years after the original work? The Six Realm Immortal World, a full-time immortal academy, a free immortal life, and all kinds of fun in the immortal world are waiting for the immortal friends to explore in person! The "Wuwei Test" pre-download is now open. Fairy friends can go to the official website to download. You cannot log in to the game server before the server is launched. The activation code can be used after the pre-download and installation is completed. "Zhu Xian 2" "Inaction Test" opening hours: April 23 10:00 - May 6 23:59 The new fairy adventure chapter of the orthodox sequel to Zhu Xian "Zhu Xian 2" is based on the "Zhu Xian" novel as a blueprint. Based on the world view of the original work, the game background is set

Introduction to PHP interface and how it is defined. PHP is an open source scripting language widely used in Web development. It is flexible, simple, and powerful. In PHP, an interface is a tool that defines common methods between multiple classes, achieving polymorphism and making code more flexible and reusable. This article will introduce the concept of PHP interfaces and how to define them, and provide specific code examples to demonstrate their usage. 1. PHP interface concept Interface plays an important role in object-oriented programming, defining the class application

Go language function closures play a vital role in unit testing: Capturing values: Closures can access variables in the outer scope, allowing test parameters to be captured and reused in nested functions. Simplify test code: By capturing values, closures simplify test code by eliminating the need to repeatedly set parameters for each loop. Improve readability: Use closures to organize test logic, making test code clearer and easier to read.

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

Functional testing verifies function functionality through black-box and white-box testing, while code coverage measures the portion of code covered by test cases. Different languages (such as Python and Java) have different testing frameworks, coverage tools and features. Practical cases show how to use Python's Unittest and Coverage and Java's JUnit and JaCoCo for function testing and coverage evaluation.

As a new operating system launched by Huawei, Hongmeng system has caused quite a stir in the industry. As a new attempt by Huawei after the US ban, Hongmeng system has high hopes and expectations. Recently, I was fortunate enough to get a Huawei mobile phone equipped with Hongmeng system. After a period of use and actual testing, I will share some functional testing and usage experience of Hongmeng system. First, let’s take a look at the interface and functions of Hongmeng system. The Hongmeng system adopts Huawei's own design style as a whole, which is simple, clear and smooth in operation. On the desktop, various

Interfaces and abstract classes are used to create extensible PHP code, and there is the following key difference between them: Interfaces enforce through implementation, while abstract classes enforce through inheritance. Interfaces cannot contain concrete methods, while abstract classes can. A class can implement multiple interfaces, but can only inherit from one abstract class. Interfaces cannot be instantiated, but abstract classes can.
