PHP local API test instance, phpapi instance

Source: Internet
Author: User

PHP local API test instance, phpapi instance

I have recently written an API interface. Each time I write an API, I need to test it first to see if there are any syntax errors. The requested data is correct, but many of them are POST requests, you cannot directly open the link in your browser for testing. Therefore, you must have a simulation tool that can send HTTP requests locally to simulate data requests.

In the beginning, I did this.WampserverCreate a file in the running directory, write a Curl request in it, and perform a simulated request test. However, each interface requires different parameters. I need to constantly modify the Request Parameters and APIs, it is inconvenient. After that, I couldn't tell the messy data in my request file:

I found related tools on the Internet and found many online testing tools, such as ATOOL online tools and Apizza. After reading these tools, they are doing very well and very convenient to use, the interface is beautiful and the service is considerate. But I'm thinkingSecurity QuestionsAnd give it to meThe returned data is in the original JSON format., I am used to viewing the array format, which is more intuitive.

As a result, I wrote a Simple API test page locally based on my own practice. After I submitted the data, I implemented the API request test function locally without considering security issues, you can also convert the results as needed. Only two files are required. One is post.html, and the other is a post. php file that receives data from the post.html page and processes the request implementation function.

1、front-end page file post.html

It's just a simple page. There is no complicated layout, no JS special effects, and currently only six parameters are written. Generally, this is enough. If not, you can add them on your own. The body request parameters are passed by default, and the request method only uses GET and POST.

<Html xmlns = "http://blog.csdn.net/sinat_35861727? Viewmode = contents "> 

2. Data Processing file post. php

The system receives data from the post.html page, sends the request, and processes the request result. The front-end page transmits the Body request parameters. If you still need Header parameters, You can manually add them to the file.

<? Phpecho '<title> API Interface request response </title> '; /*** set network request configuration * @ param [string] $ curl request URL * @ param [bool] true | false whether https Request * @ param [string] $ method request Method, by default, the GET * @ param [array] $ header Request header parameter * @ param [object] $ data object sent when a data PUT Request * @ 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 the URL and the corresponding options curl_seto Pt ($ ch, CURLOPT_URL, $ curl); // the website to be accessed // curl_setopt ($ ch, CURLOPT_HEADER, false); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); if ($ https) {curl_setopt ($ ch, success, false); // curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, true );} if ($ method = 'post') {curl_setopt ($ ch, CURLOPT_POST, true); // send the POST request curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data) ;} // Capture 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 whether the URL format is 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 whether HTTPSfunction check_https ($ url) {$ str = "/^ https: /"; if (! Preg_match ($ str, $ url) {return false;} else {return true ;}} if ($ _ SERVER ['request _ method']! = 'Post') exit ('request method error! '); // Send the 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 ']); // empty array $ data = array_filter ($ data ); // post request parameter if (empty ($ data) exit ('Enter the parameter'); $ url = $ _ POST ['curl']; // API interface if (! CheckUrl ($ url) exit ('link format error'); // check the connection format $ is_https = check_https ($ url ); // whether it is an HTTPS request $ method =$ _ POST ['method']; // Request method (get post) $ header = array (); // carry the header parameter // $ 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 request parameter concatenation $ res = ihttp_request ($ curl, $ is_https, $ method, $ header); print_r (json_decode ($ res, true);} else {exit ('error request method') ;}} curl_query ();?>

Writing is very simple, and the functions are not comprehensive. Under normal circumstances, POST and GET requests can still be satisfied. At least the local test results are correct, you can download the code and modify the functions as needed.

:Click to download

The above example of testing the local API of PHP is all the content shared by the editor. I hope you can give us a reference and support the help house.

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.