PHP code example of spatie / ping

1. Go to this page and download the library: Download spatie/ping library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / ping example snippets


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->minimumTimeInMs()}ms";
echo "Max time: {$result->maximumTimeInMs()}ms";  
echo "Average time: {$result->averageTimeInMs()}ms";
echo "Standard deviation: {$result->standardDeviationTimeInMs()}ms";

// Individual ping lines
foreach ($result->lines() as $line) {
    echo "Response: {$line->getRawLine()} ({$line->getTimeInMs()}ms)";
}

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}";
}

$result = (new Ping(
    hostname: '8.8.8.8',
    timeoutInSeconds: 5,     // seconds
    count: 3,                // number of packets
    intervalInSeconds: 1.0,  // seconds between packets
    packetSizeInBytes: 64,   // how big the packet is we'll send to the server
    ttl: 64,                 // time to live (maximum number of hops)
    showLostPackets: true    // report outstanding replies (Linux only, enabled by default)
))->run();

$result = (new Ping('8.8.8.8'))
    ->timeoutInSeconds(10)
    ->count(5)
    ->intervalInSeconds(0.5)
    ->packetSizeInBytes(128)
    ->ttl(32)
    ->showLostPackets(false)
    ->run();

// Enabled by default on Linux (ignored on macOS)
$result = (new Ping('8.8.8.8'))->run();

// Explicitly disable
$result = (new Ping('8.8.8.8'))
    ->showLostPackets(false)
    ->run();

// Explicitly enable  
$result = (new Ping('8.8.8.8'))
    ->showLostPackets(true)
    ->run();

$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->minimumTimeInMs()}ms";
echo "Max time: {$result->maximumTimeInMs()}ms";  
echo "Average time: {$result->averageTimeInMs()}ms";
echo "Standard deviation: {$result->standardDeviationTimeInMs()}ms";

// Individual ping lines
foreach ($result->lines() as $line) {
    echo "Response: {$line->getRawLine()} ({$line->getTimeInMs()}ms)";
}

// Raw ping output
echo $result->raw;

$result = (new Ping('8.8.8.8'))->run();
$array = $result->toArray();

// The array contains all ping data:
// [
//     'success' => true,
//     'error' => null,
//     'host' => '8.8.8.8',
//     'packet_loss_percentage' => 0,
//     'packets_transmitted' => 4,
//     'packets_received' => 4,
//     'options' => [
//         'timeout_in_seconds' => 5,
//         'interval' => 1.0,
//         'packet_size_in_bytes' => 56,
//         'ttl' => 64,
//     ],
//     'timings' => [
//         'minimum_time_in_ms' => 8.5,
//         'maximum_time_in_ms' => 12.3,
//         'average_time_in_ms' => 10.2,
//         'standard_deviation_time_in_ms' => 1.8,
//     ],
//     'raw_output' => '...',
//     'lines' => [...],
// ]

$newResult = PingResult::fromArray($array);

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
}