PHP API Interface Test gadget

Source: Internet
Author: User

The front-end time to the mobile phone client interface, when the weak burst, after writing API interface, but also not testing, and finally the client when the call to verify the correctness of the API.

After using PHP's curl to implement the POST request, verify the correctness of the API interface, with a previous one to view the Apache error log of the gadget, can be error sweeping, with Firebug or fiddler can view HTTP traffic.

First, function description
Style HTTP communication information in the Firebug

1, Client_name, client_version, api_debug and URL These are the parameters that need to pass each time, in addition to the URL of the other 3 parameters can be modified according to the actual situation, the URL is the address of each interface

2, a row of two input box is a variable parameter, is unique to each interface needs to pass the parameters, parameter names and parameter values can be customized

3, click Add parameter can increase that row variable parameter

4. Click Test to send the data in the input box to the port

5, the data returned from the interface to JSON format directly print out

second, the HTML part
<StyleType= "Text/css">. mb20{Margin-bottom:20px}. title{Display:Inline-blockwidth:150px; text-align:right< Span style= "color: #000000;" >}{width:100px} Mr10{margin-right:10px< Span style= "color: #000000;" >}</style>  
<DivClass= "Mb20"><LabelClass= "title">client_name:</Label><InputName= "Client_name"Type= "Text"Value= "ANDROID"/></Div><DivClass= "Mb20"><LabelClass= "title">client_version:</Label><InputName= "Client_version"Type= "Text"Value= "4.0"/></Div><DivClass= "Mb20"><LabelClass= "title">api_debug:</Label><InputName= "Api_debug"Type= "Text"Value=""/></Div><DivClass= "Mb20"><LabelClass= "title">url:</Label><InputName= "Client_url"Type= "Text"Value=""/></Div><DivClass= "Mb20"><LabelClass= "title"><InputName= "Api_key"Type= "Text"Value=""Class= "W100"/>:</Label><InputName= "Api_value"Type= "Text"Value=""/></Div><DivClass= "Mb20"><LabelClass= "title"></Label><InputType= "button"Value= "Test"id= "submit" class= "Mr10"/><input type= "button" value= "Add parameter" ID = "Add"/> </div> <div id= "message"></Div >                   

Here are some simple modifications, height, width, etc. The variable parameter line uses only the name attribute, which is Api_key and Api_value, which facilitates the cloning operation.

third, the JavaScript part
<ScriptType= "Text/javascript">$("#add"). Click (function() {Var$parent=$(This). parent ();Var$clone=$parent. Prev (). Clone (); $clone. Find (‘: Text‘). Val (‘‘); $clone. InsertBefore ($parent); }); $("#submit"). Click (function() {VarApi_keys={api_debug:$ (‘Input[name=api_debug]‘). Val (), client_url:$ (‘Input[name=client_url]‘). Val ()}; $(‘Input[name=api_key]‘). Map (function() {VarKey=$.trim ($ (This). Val ());VarValue=$.trim ($ (This). Next (). Val ());VarRepeat={};If(Key!= "=< Span style= "color: #000000;" > value; Api_keys = $.extend (Api_keys, repeat);}); //  $.post ( Span style= "color: #000000;" > ' 

1. Click event with two buttons bound

2, $ ("#add") The Click event is in the cloning operation, while cloning the original two of the contents of the input box to clear

3,$ ('Input[name=api_key]'). Map is doing the operation of filtering invalid variable parameters; The valid variable parameter is the parameter name and the parameter value exist, only the existing will send the past

4,$ ("#submit") through post to test.php, through which to send messages to the interface

iv. part of PHP
<?Php$root = ' http://api.1ddian.cn/';//Customizable domain names$url =$root.$_request[' Client_url '];//Use Curl to implement post requests, cross-domain$ch =Curl_init (); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ( $ch, Curlopt_url,  $url  $ch, Curlopt_postfields, $_request); Span style= "color: #008000;" >// transfer parameters ob_start (); Curl_exec ( $ch  $result = ob_get_contents  (); ob_end_clean (); print_r (Json_decode ( $result)); // Chinese returns the Unicode encoding, which is easy to read after decode;     

1, $root This domain name can be customized

2, use curl to implement the post request, can cross the domain

3, the Chinese return is Unicode encoding, decode after easy to read

Demo Download:

Http://download.csdn.net/download/loneleaf1/7966101

PHP API Interface Test gadget

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.