Easily Integrate PingPing APIs in your Laravel Project

Overview

PingPing

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.

Installation

Step 1: Composer

Firstly require this package using following command.

composer require enlight/pingping

Step 2: Service Provider (Optional)

This 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::class

This will bootstrap the package into Laravel.

Step 3: Set Up Environment

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.

https://pingping.io/account/settings

You are all set to use it.

Step 4: Publish Configuration (Optional)

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.

Usage

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;

Retrieve all websites (monitors)

    use Enlight\PingPing\Client;
    
    public function index(Client $client)
    {
        $response = $client->monitors();

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Retrieve a specific website (monitor)

    use Enlight\PingPing\Client;
    
    public function show($id, Client $client)
    {
        $response = $client->monitors((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Retrieve statistics from a specific website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\MonitorIDRequiredException;
    
    /**
     * @throws MonitorIDRequiredException
     */
    public function show($id, Client $client)
    {
        $response = $client->statistics((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Create a website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\ValidUrlRequiredException;
   
    /**
     * @throws ValidUrlRequiredException
     */
    public function store(Client $client)
    {
        $url = 'https://my-cool-website.test';

        $response = $client->createMonitor($url);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Update a website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\AliasRequiredException;
    use Enlight\PingPing\Exceptions\ValidUrlRequiredException;
    use Enlight\PingPing\Exceptions\MonitorIDRequiredException;
    
    /**
     * @throws AliasRequiredException
     * @throws ValidUrlRequiredException
     * @throws MonitorIDRequiredException
     */
    public function update($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();
    }

Delete a website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\MonitorIDRequiredException;
    
    /**
     * @throws MonitorIDRequiredException
     */
    public function destroy($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.



declare(strict_types=1);

namespace App\Http\Controllers;

use Enlight\PingPing\Client;
use Enlight\PingPing\Exceptions\AliasRequiredException;
use Enlight\PingPing\Exceptions\ValidUrlRequiredException;
use Enlight\PingPing\Exceptions\MonitorIDRequiredException;

class MonitorController extends Controller
{
    /** @var Client */
    private $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function index()
    {
        $response = $this->client->monitors();

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

    public function show($id)
    {
        $response = $this->client->monitors((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

    /**
     * @throws ValidUrlRequiredException
     */
    public function store()
    {
        $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
     */
    public function update($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
     */
    public function destroy($id)
    {
        $response = $this->client->deleteMonitor((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }
}


declare(strict_types=1);

namespace App\Http\Controllers;

use Enlight\PingPing\Client;
use Enlight\PingPing\Exceptions\MonitorIDRequiredException;

class StatisticsController extends Controller
{
    /**
     * @throws MonitorIDRequiredException
     */
    public function show($id, Client $client)
    {
        $response = $client->statistics((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }
}

Contributors

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.

You might also like...
An easy way to integrate Google Maps with Laravel

An easy way to integrate Google Maps with Laravel For Laravel 5.x, check version 2.35.1 For Laravel 4.x, check version 1.27.0 Think of Googlmapper as

đź––Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.
đź––Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.

Repository Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any

Easy Way to integrate API in you laravel application.

Easy Api Easy Way to integrate API in you laravel application. Installation Guide Install Package using Composer. composer require flutterbuddy1/easy-

Ajar anak anak software looka integrate tailwind

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Laravel Backend API for the tutorial (Granular permissions with Laravel APIs & React frontend)

Laravel Granular Permissions Backend Getting Started Clone the repository. Install the dependencies composer install Update .env database credentials

A Laravel Wrapper for the CoinDCX API. Now easily connect and consume the CoinDCX Public API in your Laravel apps without any hassle.
A Laravel Wrapper for the CoinDCX API. Now easily connect and consume the CoinDCX Public API in your Laravel apps without any hassle.

This package provides a Laravel Wrapper for the CoinDCX API and allows you to easily communicate with it. Important Note This package is in early deve

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

A package to access messari apis for laravel

Messari API - Laravel Laravel wrapper for messari.io API Full API documentation could be found on messari.io Installation PHP 7.2+ and Composer are re

Get started using SELCOM APIs with Laravel framework

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Comments
  • Replace HTTP to Guzzle

    Replace HTTP to Guzzle

    Currently, We are using HTTP to send API calls which limits us to use Laravel 7 and above.

    If we use Guzzle directly then Other people who have a lower version of Laravel can also use this package.

    opened by bhushan 1
  • Replace Responses with DTO

    Replace Responses with DTO

    It Will be a huge task as

    • Check the Spatie DTO package if it adds value, we will use it, otherwise, go for a simple standard PHP object
    • Create DTOs for a different response from APIs
    • How to handle different statuses from API using DTO?
    • How to give a response back if we get a failure response from PingPing API as DTO?
    help wanted 
    opened by bhushan 0
Easily integrate single-database multi tenant features into your Laravel application

Laravel Tenant Aware Easily integrate single-database multi tenant features into your Laravel application. Installation You can install the package vi

H-FARM Innovation 9 Dec 21, 2022
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.

Webdevetc BlogEtc - Complete Laravel Blog Package Quickly add a blog with admin panel to your existing Laravel project. It has everything included (ro

WebDevEtc. 227 Dec 25, 2022
A Laravel 8 Project Implement with GraphQL With Sanctum APIs Authentications Which utilized in Any Frontend or Any Mobile Application Programs.

A Laravel 8 Project Implement with GraphQL With Sanctum APIs Authentications Which utilized in Any Frontend or Any Mobile Application Programs.

Vikas Ukani 3 Jan 6, 2022
Easily setup SEO in your laravel project with lara-head :heart: @code4mk

installation composer require code4mk/lara-head usage meta ~ inside controller use Khead; class Test { public function home() { Khead::setMeta

null 173 Dec 23, 2022
Easily add all the 58 Algerian Wilayas and its Dairas to your cool Laravel project (Migrations, Seeders and Models).

Laravel-Algereography Laravel-Algereography allows you to add Migrations, Seeders and Models of Algerian Wilayas and Dairas to your existing or new co

Hocine Saad 48 Nov 25, 2022
Integrate Astrel to your Laravel applications.

✨ Astrel Laravel Integrate Astrel to your Laravel applications. Astrel is a remote config orchestration application that enables you to change anythin

Sustainable Hustle 3 Jan 9, 2022
Integrate likes, bookmarks, favorites, reactions and custom made marks into your application

Laravel Markable This package allows you to easily add the markable feature to your application, as for example likes, bookmarks, favorites and so on.

H-FARM Innovation 500 Jan 5, 2023
Laravel 2-Step Verification is a package to add 2-Step user authentication to any Laravel project easily.

Laravel 2-Step verification is a package to add 2-Step user authentication to any Laravel project easily. It is configurable and customizable. It uses notifications to send the user an email with a 4-digit verification code. Laravel 2-Step Authentication Verification for Laravel. Can be used in out the box with Laravel's authentication scaffolding or integrated into other projects.

Jeremy Kenedy 204 Dec 23, 2022
Easy-to-use SDK for implementing Neshan APIs in your Laravel projects.

Neshan Laravel SDK Easy-to-use SDK for implementing Neshan APIs in your Laravel projects. Install The easiest way to install is by using Composer: com

null 1 Oct 22, 2022
An elegant package for integrate laravel with openwa

Whatsapp Laravel An elegant package to integrate Laravel with Whatsapp automate Features Function Reference Send text Send contact Send media (doc, im

Ardzz Jay Steve 7 Aug 21, 2022