This composer package allows us to easily integrate PingPing APIs in your Laravel project.
What is PingPing ?
PingPing is the simplest uptime monitoring service in the world to get notified, when your website is down or your certificate becomes invalid. No more, no less.
Firstly require this package using following command.
composer require enlight/pingpingThis package support's auto discovery but for any reason you need to add ServiceProvider
into providers array manually then check follow steps below.
Open config/app.php and, within the providers array, append:
Enlight\PingPing\Providers\PingPingServiceProvider::classThis will bootstrap the package into Laravel.
Check your .env file, and ensure that your PING_PING_API_TOKEN is set with valid token.
You can get the token from below link and make sure it is enabled.
You are all set to use it.
Optionally, You can publish configuration file, so you can modify defaults values.
To Publish Configuration Run
php artisan vendor:publish --provider="Enlight\PingPing\Providers\PingPingServiceProvider" --tag="config"Exported config you can find in /config folder as pingping.php.
All methods and API calls will return Illuminate\Http\Client\Response instance. That mean's, you have access to
following methods.
$response->body() : string;
$response->json() : array|mixed;
$response->collect() : Illuminate\Support\Collection;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;useEnlight\PingPing\Client;
publicfunctionindex(Client$client)
{
$response = $client->monitors();
if ($response->failed()) {
return$response->json();
}
return$response->json();
}useEnlight\PingPing\Client;
publicfunctionshow($id, Client$client)
{
$response = $client->monitors((int) $id);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}useEnlight\PingPing\Client;
useEnlight\PingPing\Exceptions\MonitorIDRequiredException;
/** * @throws MonitorIDRequiredException */publicfunctionshow($id, Client$client)
{
$response = $client->statistics((int) $id);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}useEnlight\PingPing\Client;
useEnlight\PingPing\Exceptions\ValidUrlRequiredException;
/** * @throws ValidUrlRequiredException */publicfunctionstore(Client$client)
{
$url = 'https://my-cool-website.test';
$response = $client->createMonitor($url);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}useEnlight\PingPing\Client;
useEnlight\PingPing\Exceptions\AliasRequiredException;
useEnlight\PingPing\Exceptions\ValidUrlRequiredException;
useEnlight\PingPing\Exceptions\MonitorIDRequiredException;
/** * @throws AliasRequiredException * @throws ValidUrlRequiredException * @throws MonitorIDRequiredException */publicfunctionupdate($id, Client$client)
{
$url = 'https://my-cool-website2.test';
$alias = 'My cool website2';
$response = $client->updateMonitor((int) $id, $url, $alias);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}useEnlight\PingPing\Client;
useEnlight\PingPing\Exceptions\MonitorIDRequiredException;
/** * @throws MonitorIDRequiredException */publicfunctiondestroy($id, Client$client)
{
$response = $client->deleteMonitor((int) $id);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}Finally, your updated controllers might look like this now.
<?phpdeclare(strict_types=1);
namespaceApp\Http\Controllers;
useEnlight\PingPing\Client;
useEnlight\PingPing\Exceptions\AliasRequiredException;
useEnlight\PingPing\Exceptions\ValidUrlRequiredException;
useEnlight\PingPing\Exceptions\MonitorIDRequiredException;
class MonitorController extends Controller
{
/** @var Client */private$client;
publicfunction__construct(Client$client)
{
$this->client = $client;
}
publicfunctionindex()
{
$response = $this->client->monitors();
if ($response->failed()) {
return$response->json();
}
return$response->json();
}
publicfunctionshow($id)
{
$response = $this->client->monitors((int) $id);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}
/** * @throws ValidUrlRequiredException */publicfunctionstore()
{
$url = 'https://my-cool-website.test';
$response = $this->client->createMonitor($url);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}
/** * @throws AliasRequiredException * @throws ValidUrlRequiredException * @throws MonitorIDRequiredException */publicfunctionupdate($id)
{
$url = 'https://my-cool-website2.test';
$alias = 'My cool website2';
$response = $this->client->updateMonitor((int) $id, $url, $alias);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}
/** * @throws MonitorIDRequiredException */publicfunctiondestroy($id)
{
$response = $this->client->deleteMonitor((int) $id);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}
}<?phpdeclare(strict_types=1);
namespaceApp\Http\Controllers;
useEnlight\PingPing\Client;
useEnlight\PingPing\Exceptions\MonitorIDRequiredException;
class StatisticsController extends Controller
{
/** * @throws MonitorIDRequiredException */publicfunctionshow($id, Client$client)
{
$response = $client->statistics((int) $id);
if ($response->failed()) {
return$response->json();
}
return$response->json();
}
}This package is inspired by the Steve's blog post on Working with third party services in Laravel . I highly recommend you to read it.
Thank You :)
Happy Coding.