Network Query Tool Sample Usage
This document outlines basic examples for implementing
NetworkQueryTool in your own scripts. We welcome user contributions but we do not allow editing of this page at the present time. Please email your suggested additions for this page to shaunAtDrunkwerksDotCom with "NQT" in the subject.
Getting Started
In order to use NQT, you must first
include() or
require_once() the NetworkQueryTool.php class file in your script. From there, you'll need to instantiate a new NetworkQueryTool object, optionally set properties, and then begin calling the object's methods to perform the actions you desire. If you're unfamiliar with some of this terminology, just follow along with the examples below and you'll get the hang of it.
Example 1 - Performing a Whois
Let's start with a very basic yet functional example, getting the whois information for a domain name. This example illustrates the simplest use of Network Query Tool: including the class file, creating a NetworkQueryTool object by passing the target domain into the constructor, and then calling a single method to display the results.
<?php
require_once('NetworkQueryTool.php');
// Include the NetworkQueryTool class
$nqt =
new NetworkQueryTool
('php.net');
// Create a new object, specifying the domain
echo $nqt->
doWwwWhois();
// Display the results of a WWW whois
?>
Example 2 - Resolving a Host
Suppose that you have an IP address, and you want to determine the hostname that corresponds to it. The following code will attempt to get the hostname that corresponds to the IP address 4.2.2.4.
<?php
require_once('NetworkQueryTool.php');
// Include the NetworkQueryTool class
$nqt =
new NetworkQueryTool
('4.2.2.4');
// Create a new object, specifying the IP address
echo $nqt->
getAddr() .
' resolves to ' // Dispay the output of resolving the IP
.
$nqt->
doResolveAddr();
?>
In the above code sample, we also called NQT's getAddr() method, which returns the IP address associated with the current NQT object.
Example 3 - Testing Remote Port, With Error Handling
Here is a demonstration of wrapping Network Query Tool capabilities with error handling. In this exercise, we'll try to connect to port 80 (the HTTP server) at google.com.
<?php
$nqt =
new NetworkQueryTool
();
$nqt->
setHost('www.google.com');
// Set target using setter instead of constructor
if (!
$nqt->
getError()) { // Ask NQT if it encountered an error
echo $nqt->
doCheckPort(80) ?
'Port 80 is open' :
'Port 80 is closed';
} else {
echo 'An error occurred: ' .
$nqt->
getError();
// Display the error code
}
?>
Here, instead of passing the target into the constructor, we created an object and then set the host using the setter method. There's a possibility that this can induce an error (if, for example, an invalid hostname was passed to
setHost()), so we test to see what NQT's
getError() method returns. A return value of zero means that there is no error, at which point we proceed to check port 80 on the remote host, and display the output of that test.
Example 4 - Doing Everything
Here's an example of performing all of Network Query Tool's primary tasks against a single host.
<?php
$nqt =
new NetworkQueryTool
('freebsd.org');
$nqt->
enableDebug(true);
//Turn on debug output
if (!
$nqt->
getError()) {
echo '<h5>Resolve</h5><blockquote><pre>' .
$nqt->
getHost()
.
' resolves to ' .
$nqt->
doResolveHost() .
'</pre></blockquote>';
echo '<h5>DNS Query Results:</h5><blockquote><pre>' .
$nqt->
doDig()
.
'</pre></blockquote>';
echo '<h5>WWWhois Results:</h5><blockquote><pre>' .
$nqt->
doWwwWhois()
.
'</pre></blockquote>';
echo '<h5>RIR Whois Results:</h5><blockquote><pre>' .
$nqt->
doRirWhois()
.
'</pre></blockquote>';
echo '<h5>Check Port:</h5><blockquote><pre>' .
($nqt->
doCheckPort(80) ?
'Port 80 is open' :
'Port 80 is closed')
.
'</pre></blockquote>';
echo '<h5>Ping Results:</h5><blockquote><pre>' .
$nqt->
doPing()
.
'</pre></blockquote>';
echo '<h5>Traceroute Results:</h5><blockquote><pre>' .
$nqt->
doTraceroute()
.
'</pre></blockquote>';
}
?>
This example introduces NQT's
enableDebug() method. Passing
true to this method turns on the display of diagnostic information anytime Network Query Tool encounters a problem. If you're having difficulty integrating NQT into your site or application, enabling debug output is the best way to figure out what's going on.
There are no comments on this page. [Add comment]