Php+ajax Domain Name Query preparation knowledge
This query system uses the Ajax function of PHP and JQUery to realize the query of domain name information (this is the main implementation of the domain name has been registered query). The system is mainly used to provide the domain name Query API interface, the relevant knowledge points listed below:
jquery Ajax implementation: This part of the content can be seen in the jquery API documentation or the site is about to launch the jquery tutorial.
file_get_contents function: Reads the entire file into a string, which is used to read a Web page (the million web API returns the results page).
simplexml_load_string function: Used to parse an XML document into an object.
Strrpos function: The position used to locate the first occurrence of a string, used to search for keywords.
Requirements analysis of Domain name query system
According to the user entered the domain name, query whether the domain name has been registered.
The Domain name registration information (WHOIS) query, the function of this tutorial is not implemented, you can refer to the existing features to achieve.
Page/File information
Domain.html: Form submission and query result information display page.
domain_check.php: A php file that handles query domain information.
Million network domain name Query API interface
The interface uses the Http,post,get protocol.
Call url:http://panda.www.net.cn/cgi-bin/check.cgi
Parameter name: Area_domain value is standard domain name, example: 5idev.com
Call Example: http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=5idev.com
return XML:
The code is as follows |
Copy Code |
<?xml version= "1.0" encoding= "gb2312"?> <property> <returncode>200</returncode> <key>5idev.com</key> <original>211:domain name is not available</original> </property> |
XML Results Description:
Returncod: interface invocation state.
Key: Represents the domain name of the current check.
Original: The result of the domain name check.
Original has 4 results:
210:domain name is available: Indicates that a domain name can be registered
211:domain name is not available: Indicates that the domain name has been registered
212:domain name is invalid: Indicates a domain name parameter transmission error
214:unknown Error: Indicates an unknown error or query exception
Domain.html page Key Code
Domain.html page to achieve the form of Ajax submission and domain name query results information display. The complete code is as follows (note that the UTF-8 encoding):
The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1 -transitional.dtd "> <title> Domain Registration Query </title> <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <script src= "Js/jquery-1.4.2.min.js" ></script> <script type= "Text/javascript" > function Domain_check () { var domain = $.trim ($ ("#domain"). Val ()); if (domain = = ") { $ ("#check_result"). HTML (' Please enter the domain name information to query, such as: 5idev.com '); $ ("#domain"). focus (); return false; } $ ("#domain_result"). html (' is querying, please later ... '); $.ajax ({ Type: "Get", Cache:false, DataType: "Text", URL: "domain_check.php?domain=" +domain, Success:function (data) { $ ("#domain_result"). HTML (data); } }); } </script> <body> <div> <form > Please enter the domain name to query: www.<input id= "domain" type= "text" value= "5idev.com" onfocus= "this.value=" "/>" <input type= "button" value= "Query" onclick= "Domain_check ()"/> </form> </div> <div id= "Domain_result" ></div> </body>
|
In this paper, we implement the form submission of Ajax get form by using jauery, and do the initial non-empty detection for the input form.
Domain Name Query PHP code
The following is the complete query for domain name information and response Ajax request PHP Source code:
The code is as follows |
Copy Code |
<?php $domain = Htmlspecialchars (Trim ($_get[' domain ')); if (! $domain) { Echo ' Please enter the domain name to query, such as: 5idev.com '; Exit } Call the million-NET Domain name query API $area _domain = Iconv ("Utf-8", "gb2312", $domain); $domain _api = ' http://panda.www.net.cn/cgi-bin/check.cgi?area_domain= '. $area _domain; $contents = file_get_contents ($domain _api); $xml = simplexml_load_string ($contents); if (!empty ($xml)) { Switch ($xml->original) { Case ' 210:domain name is available ': $result = ' The domain name can be registered '; Case ' 211:domain name isn't available ': $result = ' The domain name has been registered '; Case ' 212:domain name is invalid ': $result = ' domain Name parameter error, please enter the domain name format '; Case ' 214:unknown error ': $result = ' query exception, please try again later '; } } else { Standby, only query international domain name $url = ' http://www.checkdomain.com/cgi-bin/checkdomain.pl?domain= ' .$_get< ' domain ' >; $fp = file_get_contents ($url); if (Strpos ($fp, ', has already been registered ')) { $result = ' The domain name has already been registered '; } else { $result = ' The domain name can be registered '; } } Echo ' <b> '. $domain. ' </b>: '. $result; ?> |
Some problems needing attention
Support Chinese domain name query.
Since the domain name is to be displayed on the page, a special HTML code conversion is made using the Htmlspecialchars function to prevent illegal input, and more stringent detection can be done using regular expressions.
Since the mesh interface provides gb2312 encoding, the Iconv () function is used to encode the conversion, without the conversion if UTF-8 encoding is not used.
If you use GB2312 encoding, the return of Ajax results may require a gb2312 of the display results to a UTF-8 encoded conversion.
When the network interface can not return the results, enable the standby interface to query, but can only query the international domain name.