This package provides a simple way to execute ICMP ping commands and parse the results into structured data. It wraps the system's ping command and returns detailed information about packet loss, response times, and connectivity status.
use Spatie\Ping\Ping;
$result = (new Ping('8.8.8.8'))->run(); // returns an instance of \Spatie\Ping\PingResult
// Basic status
echo $result->isSuccess() ? 'Success' : 'Failed';
echo $result->hasError() ? "Error: {$result->error()?->value}" : 'No errors';
// Packet statistics
echo "Packets transmitted: {$result->packetsTransmitted()}";
echo "Packets received: {$result->packetsReceived()}";
echo "Packet loss: {$result->packetLossPercentage()}%";
// Timing information
echo "Min time: {$result->minimumTime()}ms";
echo "Max time: {$result->maximumTime()}ms";
echo "Average time: {$result->averageTime()}ms";
echo "Standard deviation: {$result->standardDeviationTime()}ms";
// Individual ping lines
foreach ($result->lines() as $line) {
echo "Response: {$line->getRawLine()} ({$line->getTimeInMs()}ms)";
}
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via Composer:
composer require spatie/ping
The simplest way to ping a host:
use Spatie\Ping\Ping;
$result = (new Ping('8.8.8.8'))->run();
if ($result->isSuccess()) {
echo "Ping successful! Average response time: {$result->averageResponseTimeInMs()}ms";
} else {
echo "Ping failed: {$result->error()?->value}";
}
You can customize the ping behavior using constructor parameters:
$result = (new Ping(
hostname: '8.8.8.8',
timeout: 5, // seconds
count: 3, // number of packets
interval: 1.0, // seconds between packets
packetSize: 64, // bytes
ttl: 64 // time to live
))->run();
Or use the fluent interface:
$result = (new Ping('8.8.8.8'))
->timeout(10)
->count(5)
->interval(0.5)
->packetSize(128)
->ttl(32)
->run();
The PingResult
object provides detailed information about the ping operation:
$result = (new Ping('8.8.8.8', count: 3))->run();
// Basic status
echo $result->isSuccess() ? 'Success' : 'Failed';
echo $result->hasError() ? "Error: {$result->error()?->value}" : 'No errors';
// Packet statistics
echo "Packets transmitted: {$result->packetsTransmitted()}";
echo "Packets received: {$result->packetsReceived()}";
echo "Packet loss: {$result->packetLossPercentage()}%";
// Timing information
echo "Min time: {$result->minimumTime()}ms";
echo "Max time: {$result->maximumTime()}ms";
echo "Average time: {$result->averageTime()}ms";
echo "Standard deviation: {$result->standardDeviationTime()}ms";
// Individual ping lines
foreach ($result->lines() as $line) {
echo "Response: {$line->getRawLine()} ({$line->getTimeInMs()}ms)";
}
// Raw ping output
echo $result->raw;
You can convert the result to an array for easy serialization:
$result = (new Ping('8.8.8.8'))->run();
$array = $result->toArray();
// The array contains all ping data including:
// - success status and error information
// - packet statistics and timing data
// - configuration options used
// - raw output and parsed lines
You can also reconstruct a PingResult
from an array:
$newResult = PingResult::fromArray($array);
The error()
method on a PingResult
will return a case of the Spatie\Ping\Enums\PingError
enum.
use Spatie\Ping\Ping;
$result = (new Ping('non-existent-host.invalid'))->run();
if (! $result->isSuccess()) {
return $result->error() // returns the enum case Spatie\Ping\Enums\PingError::HostnameNotFound
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.