API interface test for PHP

Source: Internet
Author: User
Recently write API interface, each write an interface, I need to test first, see there is no syntax error, the requested data is not correct, but many are post requests, can not directly open the link in the browser to test, so there must be a local HTTP request to the simulation tool to simulate the data request.
I did it at first, in this machine. WampserverRun directory to create a file, write curl in the request, the simulation request test, but each interface requires different parameters, I need to constantly modify the requested parameters and API, it is inconvenient. By the end of my request, I could not tell the messy data in the file:

Find the relevant tools on the Internet, there are many online testing, such as: Atool online tools, Apizza and so on, see what they do is very good, the use of very convenient, very beautiful interface, service is very thoughtful. But I was thinking. Security Issues, while it gave me The original JSON-formatted data is returned, I am accustomed to look at the array format, more intuitive.
As a result, in the spirit of their own hands-on concept, I wrote a simple API test page, after submitting the data in the local implementation of API request testing function, regardless of security issues, but also can be arbitrarily converted results. Only need two files to be done, one is to fill in the data of the page post.html, the other is to receive the Post.html page transmitted data and processing requests to implement the function of the post.php file.
1. front Page File post.html


is simply a simple page, no complex layout, no JS effects, temporarily only write 6 parameters, generally enough, enough to add their own. The default is to pass the body request parameter, and the request method only uses Get and post.


2. Data Processing file post.php

Receive the data from the Post.html page, and send the request and then process the request results, the front-end page is sent over the body request parameters, if you need the header parameter, you can add it manually in this file.

<?phpecho ' <title>api interface request Response </title> ';/** * Set Network request configuration * @param [string] $curl requested URL * @param [bool] True || False whether the HTTPS request * @param [string] $method the request method, the default get * @param [array] $header the requested header parameter * @param [object] $data put please The data object that is sent when requested * @return [object] Returns the request response */function Ihttp_request ($curl, $https =true, $method = ' GET ', $header =array (), $data    =null) {//Create a new curl resource $ch = Curl_init ();//Set URL and corresponding options curl_setopt ($ch, Curlopt_url, $curl); To access the website//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); Send POST request curl_setopt ($ch, Curlopt_postfields, $data);} Fetch the URL and pass it to the browser $content = curl_exec ($ch); if ($content = = = False) {return "Network request error:".  Curl_error ($ch); Exit ();} Close the Curl resource and release the system Resource Curl_close ($ch); return $content;} Check if the link Format 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;      }}//check is not httpsfunction Check_https ($url) {$str = "/^https:/"; if (!preg_match ($str, $url)) {return false;      }else{return true; }}if ($_server[' request_method ']! = ' POST ') exit (' Request way wrong! '); /Send Request 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 ');//Array de-empty $data = Array_filter ($data);//post request parameter if (empty ($data)) exit (' Please fill in Parameters '); $   url = $_post[' curl '];//api interface if (!checkurl ($url)) exit (' Link format error ');//Check the format of the connection $is_https = Check_https ($url); Whether it is an HTTPS request $method = $_post[' method '];//request method (GET POST) $header = Array ();//Carry header parameter//$header [] = ' CacHe-control:max-age=0 ';//$header [] = ' connection:keep-alive ', if ($method = = ' POST ') {$res = Ihttp_request ($url, $is _ HTTPS, $method, $header, $data);p Rint_r (Json_decode ($res, True));} else if ($method = = ' GET ') {$curl = $url. '? '. Http_build_query ($data);//get request parameter Stitching $res = Ihttp_request ($curl, $is _https, $method, $header);p Rint_r (Json_decode ($ res,true));} Else{exit (' Error request method ');}} Curl_query ();? >

The

Write is very simple, the function is not very comprehensive, the normal situation of post and get requests can still be satisfied, at least the local test to see the result is no problem, the need for a small partner can download the code, and then according to their own needs to modify the perfect function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.