PHP code example of graymatterlabs / pingtree

1. Go to this page and download the library: Download graymatterlabs/pingtree 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/ */

    

graymatterlabs / pingtree example snippets


$tree = new Tree($strategy, $offers);

$response = $tree->ping($lead);

$tree = new Tree($strategy, $offers);

$response = $tree->ping($lead);

if ($response instanceof RedirectResponse) {
  return redirect($response->url());
}



namespace App\Offers;

use App\Http;
use App\Leads\AutoLead;
use GrayMatterLabs\PingTree\Contracts\Offer;
use GrayMatterLabs\PingTree\Contracts\Lead;
use GrayMatterLabs\PingTree\Contracts\Response;

class Example implements Offer
{
    private string $url = 'https://example.com';
    
    public function __construct(private Http $http, private string $key)
    {
    }

    public function getIdentifier(): string|int
    {
        return 'example';
    }

    public function ping(AutoLead $lead): int
    {
        $response = $this->http
            ->withHeader('Authorization', $this->key)
            ->post($this->url . '/value', [
                // ...
            ]);
        
        return (int) $response->json('value');
    }

    public function send(AutoLead $lead): Response
    {
        $response = $this->http
            ->withHeader('Authorization', $this->key)
            ->post($this->url . '/send', [
                // ...
            ]);

        return new RedirectResponse(!$response->ok(), $response->json('accepted'), $response->json('url'));
    }

    public function isEligible(AutoLead $lead): bool
    {
        return $lead->validate();
    }
    
    public function isHealthy(): bool
    {
        return $this->http->get($this->url . '/health')->ok();
    }
}

$tree = new Tree($strategy, $offers);

// listen for any events
$tree->listen($event, $callable);
$tree->listen($event, $other_callable);

$response = $tree->ping($lead);