Archive

Posts Tagged ‘pear’

Ping A Website Using PEAR’s Net_Ping

April 22nd, 2009 No comments

Net_Ping is a PHP wrapper for the ping program and provides a neat way pinging a host or checking if a site is up and running. To install it you just need to have pear installed. Once that is done you can do a search for PEAR Net_Ping by opening a terminal and typing:

>ping search ping

This will return the following:

Retrieving data...0%.MATCHED PACKAGES, CHANNEL PEAR.PHP.NET:
=======================================
PACKAGE           STABLE/(LATEST) LOCAL
Net_Ping          2.4.4 (stable)        Execute ping
Services_Pingback 0.2.2 (alpha)         A Pingback User-Agent class.

You can install Net_Ping by running the following:

H:>pear install Net_Ping

Which should produce the following output.

downloading Net_Ping-2.4.4.tgz ...
Starting to download Net_Ping-2.4.4.tgz (9,563 bytes)
.....done: 9,563 bytes
install ok: channel://pear.php.net/Net_Ping-2.4.4

You can now use Net_Ping. To use the class in your code you need to include it first, after this you need to get an instance of the Net_Ping object so that you can work with it. The following code will create a Net_Ping object that you can work with.

require_once "Net/Ping.php";
$ping = Net_Ping::factory();

To ping a site you just need to run the ping() function. This function takes a single parameter which is the IP address or the name of the server you want to ping. The function returns a Net_Ping_Result object that contains all of the information returned from the ping.

$results = $ping->ping('www.example.com');

Here is a print_r() output of this object.

Net_Ping_Result Object
(
    [_icmp_sequence] => Array
        (
            [1] => 176
            [2] => 177
        )
 
    [_target_ip] => 208.77.188.166
    [_bytes_per_request] => 32
    [_bytes_total] => 64
    [_ttl] => 54
    [_raw_data] => Array
        (
            [0] => 
            [1] => Pinging www.example.com [208.77.188.166] with 32 bytes of data:
            [2] => 
            [3] => Reply from 208.77.188.166: bytes=32 time=176ms TTL=54
            [4] => Reply from 208.77.188.166: bytes=32 time=177ms TTL=54
            [5] => 
            [6] => Ping statistics for 208.77.188.166:
            [7] =>     Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
            [8] => Approximate round trip times in milli-seconds:
            [9] =>     Minimum = 176ms, Maximum = 177ms, Average = 176ms
        )
 
    [_sysname] => windows
    [_round_trip] => Array
        (
            [min] => 176
            [max] => 177
            [avg] => 176
        )
 
    [_transmitted] => 2
    [_received] => 2
    [_loss] => 0
)

You can access the different properties within the Net_Ping_Result to find out what happened with the ping. The percentage of pings lost is also worked out for you in the _loss property.

echo '<p>Transmitted: '.$results->_transmitted.'</p>';
echo '<p>Received: '.$results->_received.'</p>';
echo '<p>% Loss: '.$results->_loss.'%</p>';

The _round_trip parameter contains an array that details the minimum, maximum and average times for the requests.

You might have noticed in my original example that I only sent out 2 pings. This is because the Net_Ping object has a function that allows you to set certain options before you make the request. This function is called setArgs() and takes an associative array as the argument. To set the number of ping requests to be sent out just use the following line of code before you make your ping request.

$ping->setArgs(array('count' => 2));

You should be aware that different operating systems have different options, so for a full list of options please see the Net_Ping::setArgs() page on the PEAR website.

If you just want to check the host to make sure it is still active you can use the checkhost() function. This works in the same way as the ping() function, but will return true on success and false on failure. You can see the output of checkhost() by viewing the var_dump() output.

var_dump($ping->checkhost('example.com'));

This function is really a wrapper for the ping() function, but will parse the output to determine if the ping request worked or not.

Categories: PHP Tags: , , , , ,

Install PHPDocumentor

January 21st, 2009 No comments

PHPDocumentor is a fast and convent way of creating API documentation for your PHP programs and classes. If you are familiar with the world of Java, it works in much the same way as the JavaDoc program, indeed, it is based on this program.

PHPDocumentor can be run in a number of different ways, but I have found that the easiest way is to, again, use PEAR to install everything you need. To install PHPDocumentor using PEAR use the following command.

phpdoc install phpdocumentor

To run PHPDocumentor and see a list of commands just type in the following:

phpdoc -h

To run PHPDocumentor you need to provide a couple of options, these are:

  • t The documentation directory (ie. where your documentation files will go).
  • o The output format (e. HTML, PDF etc), which is defined as output:converter:template directory.
  • d The directory (ie. where your source code files are).

Use the following syntax to run PHPDocumentor, using the above parameters.

C:\projectDir>phpdoc -t "c:\projectDir" -o HTML:default:default -d "c:\projectDir\docs"

The output format defined here is HTML, with the default style and format. This is as a document with frames and in the default style that the program uses. To see what sort of formats are available go to the PHPDocumentor install directory at <your PHP directory>\data\PhpDocumentor\phpDocumentor\Converters.

To get a different style on your PHPDocs use the following value for the -o parameter.

HTML:default:l0l33t

To create a PDF of your documentation use the following.

PDF:default:default

To product output that looks like the PHP home page use the following:

HTML:smarty:php

You might be asking how you get your PHP code to generate this documentation. Although this is not complicated, there is rather a lot to go over and so this will be the subject of a different blog post.

Categories: PHP Tags: , , , , , , ,

Installing PHPUnit

January 19th, 2009 No comments

PHPUnit is a powerful unit testing framework written in and for PHP. Rather than testing everything as a whole the idea behind PHPUnit is to test that everything works as it is expected to work before it is integrated into the rest of the program. In this way problems are found earlier rather than later and this makes fixing them a lot easier. With tests written for every small component of the program it is then possible to test the whole thing by running all of the tests at once. It is also possible to automate PHPUnit so that everything about a program is tested before it is built. If any tests fail then the build is stopped.

The best way to install PHPUnit is to use the PEAR installer. To install PHPUnit you first need to add the correct channel so that PEAR can find PHPUnit easily. You do this by typing the following into the command line.

pear channel-discover pear.phpunit.de

You can now install PHPUnit by using the following command.

pear install phpunit/PHPUnit

You can now test your install by trying to run PHPUni through the command line.

phpunit

If properly installed this command will print out usage and parameter information for PHPUnit.

You can see which version of PHPUnit is installed by using the –version parameter.

phpunit --version

For more information about PHPUnit take a look at the PHPUnit website.

Categories: PHP Tags: , , , ,

Getting And Installing Phing

January 2nd, 2009 No comments

To get Phing you will need to have PEAR installed along with PHP. On a Windows system you can install PEAR by running the go-pear.bat file and running through the prompts there.

To get Phing just run the following command.

pear install http://phing.info/pear/phing-current.tgz

You should see the install output looking like this:

downloading phing-current.tgz ...
Starting to download phing-current.tgz (361,527 bytes)
.........................................................................done: 361,527 bytes
install ok: channel://pear.php.net/phing-2.1.1

You can now run Phing by typing the following command:

phing

However, this will automatically try to find a file in the current directory called build.xml, and if it doesn’t find this file it will simply state the following:

Buildfile: build.xml does not exist!

Before stopping. This build.xml file (although it doesn’t have to be called this) is what controls what phing will do and is what you will spend most of your time looking at.

You can view which version of phing you are running by using the -v parameter.

phing -v

Which prints out something like:

Phing version 2.1.1

To view a list of available commands use the -h parameter.

phing -h

Which will print out:

phing [options] [target [target2 [target3] ...]]
Options:
-h -help print this message
-l -list list available targets in this project
-v -version print the version information and exit
-q -quiet be extra quiet
-verbose be extra verbose
-debug print debugging information
-logfile <file> use given file for log
-logger <classname> the class which is to perform logging
-f -buildfile <file> use given buildfile
-D<property>=<value> use value for given property
-find <file> search for buildfile towards the root of the
filesystem and use it
 
Report bugs to <dev@phing.tigris.org>

Categories: PHP Tags: , , , ,

Automated Build With Phing

January 2nd, 2009 No comments

Phing is a PHP tool that allows you to build projects. Phing stands for PHing Is Not Gnu make, which is kind of a common coder’s joke. It is basically a tool that will take a set of files in multiple directories and create either a zip file or a numerous other things that you might want to do with a project.

Phing itself is a PHP program, so all that is needed to run it is a working copy of PHP. Also, because Phing is part of PEAR it is easy to install, and you don’t need to install Apache to run it.

Over the next few posts I will be talking about the sort of things you can do with Phing, how to install it, how to create zip files and how to use variables to simplify things.

Categories: PHP Tags: , , , ,