PHP code example of jcolombo / leadfeeder-api-php

1. Go to this page and download the library: Download jcolombo/leadfeeder-api-php 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/ */

    

jcolombo / leadfeeder-api-php example snippets


use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Account;

// Establish a connection using your Leadfeeder API token
$lf = Leadfeeder::connect('your-api-token-here');

// List all accounts accessible to this token
$accounts = Account::list($lf)->fetch();

foreach ($accounts as $account) {
    echo $account->name . ' (' . $account->id . ')' . PHP_EOL;
}

// Bind the connection to a specific account for subsequent requests
$lf->setAccount('your-account-id');

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Fetch one page of leads for a date range
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->pageSize(25)
    ->fetch();

foreach ($leads as $lead) {
    echo $lead->name . ' — ' . $lead->website_url . PHP_EOL;
}

// Fetch a single lead by ID
$lead = Lead::new($lf)->fetch('abc123');
echo $lead->name . PHP_EOL;

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Visit;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$visits = Visit::list($lf)
    ->dateRange('2024-06-01', '2024-06-30')
    ->pageSize(50)
    ->fetch();

foreach ($visits as $visit) {
    echo $visit->started_at . ' — ' . $visit->source . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// dateRange maps to start_date and end_date server-side filters
$leads = Lead::list($lf)
    ->dateRange('2024-03-01', '2024-03-31')
    ->fetch();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Default: page 1, 100 results
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->fetch();

echo count($leads) . ' leads on this page' . PHP_EOL;

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Fetch page 3 with 25 results per page
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->page(3)
    ->pageSize(25)
    ->fetch();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Retrieve all leads across all pages (up to 10,000)
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-12-31')
    ->pageSize(100)
    ->fetchAll();

echo 'Total leads loaded: ' . count($leads) . PHP_EOL;

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Filter by a specific custom feed (server-side)
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->where('custom_feed_id', 'feed-abc123')
    ->fetch();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Only   ->dateRange('2024-01-01', '2024-01-31')
    ->where('custom_feed_id', 'feed-abc123')
    ->has('employee_count', 50, '>')
    ->has('status', 'new')
    ->fetch();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Request location data to be ll) {
        echo $lead->name . ' is from ' . $location->city . ', ' . $location->country . PHP_EOL;
    }
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Visit;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$visits = Visit::list($lf)
    ->dateRange('2024-06-01', '2024-06-30')
    ->

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\CustomFeed;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$feeds = CustomFeed::list($lf)->fetch();

foreach ($feeds as $feed) {
    echo $feed->id . ' — ' . $feed->name . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// forFeed() sets the request path to: custom-feeds/{feedId}/leads
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->forFeed('your-feed-id')
    ->fetch();

foreach ($leads as $lead) {
    echo $lead->name . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Visit;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$visits = Visit::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->forLead('lead-id-here')
    ->fetch();

foreach ($visits as $visit) {
    echo 'Visit: ' . $visit->started_at . PHP_EOL;

    // visit_route is an array:object — each item describes a page in the visit path
    $route = $visit->visit_route;
    if (is_array($route)) {
        foreach ($route as $step) {
            echo '  -> ' . ($step['path'] ?? '') . PHP_EOL;
        }
    }
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\WebsiteTrackingScript;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Fetch the singleton — no ID argument needed
$script = WebsiteTrackingScript::new($lf)->fetch();

echo 'Script hash: ' . $script->script_hash . PHP_EOL;
echo 'Timezone: ' . $script->timezone . PHP_EOL;

// script_html is typed as 'html' — raw HTML string ready to embed
echo $script->script_html . PHP_EOL;

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Export\ExportManager;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Step 1: Create the export job
$export = ExportManager::create($lf, [
    'account_id' => 'your-account-id',
    'start_date' => '2024-01-01',
    'end_date'   => '2024-01-31',
]);

echo 'Export created: ' . $export->getExportId() . PHP_EOL;

// Step 2: Wait for completion (polls every 10s, up to 30 attempts = 5 minutes max)
$export->waitForCompletion();

// Step 3: Download the data
$rows = $export->download();

foreach ($rows as $row) {
    echo $row['id'] . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Export\ExportManager;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$export = ExportManager::create($lf, [
    'account_id' => 'your-account-id',
    'start_date' => '2024-06-01',
    'end_date'   => '2024-06-30',
]);

// Poll manually until status changes from 'pending'
$attempts = 0;
while ($export->getStatus() === 'pending' && $attempts < 60) {
    sleep(5);
    $status = $export->checkStatus();
    echo 'Status: ' . $status . PHP_EOL;
    $attempts++;
}

if ($export->getStatus() === 'processed') {
    $rows = $export->download();
    echo count($rows) . ' rows downloaded' . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Export\ExportManager;

$lf = Leadfeeder::connect('your-api-token-here');

$export = ExportManager::create($lf, [
    'account_id'     => 'your-account-id',
    'start_date'     => '2024-01-01',
    'end_date'       => '2024-01-31',
    'custom_feed_id' => 'your-feed-id',
]);

$export->waitForCompletion(pollIntervalSeconds: 15, maxAttempts: 40);
$rows = $export->download();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;

// Create an IP Enrichment client via the factory (cached singleton per API key)
$enrichClient = Leadfeeder::connectIpEnrich('your-discover-api-key');

// Look up a company by IP address
$company = $enrichClient->lookup('203.0.113.42');

if ($company !== null) {
    // $company is a raw associative array — not an entity object
    echo $company['name'] . PHP_EOL;
    echo $company['domain'] . PHP_EOL;
} else {
    // null indicates no company was found (404) or an error occurred
    echo 'No company identified for this IP' . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Configuration;

// Load a configuration file, merging with defaults (path to file or directory)
Configuration::overload('/path/to/your/project');

// Or load a file at an explicit path
Configuration::load('/path/to/leadfeederapi.config.json');

// Read a configuration value
$timeout = Configuration::get('connection.timeout');         // 30
$devMode = Configuration::get('devMode', false);            // false (with default)

// Override a single value at runtime
Configuration::set('devMode', true);
Configuration::set('rateLimit.perMinute', 60);

use Jcolombo\LeadfeederApiPhp\Configuration;

Configuration::set('enabled.cache', true);

define('LFAPI_REQUEST_CACHE_PATH', '/tmp/my-app-cache');

use Jcolombo\LeadfeederApiPhp\Configuration;

Configuration::set('enabled.cache', true);

use Jcolombo\LeadfeederApiPhp\Cache\Cache;
use Jcolombo\LeadfeederApiPhp\Utility\RequestResponse;

Cache::registerCacheMethods(
    read: function (string $key): ?RequestResponse {
        $data = redis()->get('lfapi:' . $key);
        return $data !== false ? unserialize($data) : null;
    },
    write: function (string $key, RequestResponse $response): void {
        redis()->setex('lfapi:' . $key, 300, serialize($response));
    },
    clear: function (?string $key): void {
        if ($key !== null) {
            redis()->del('lfapi:' . $key);
        } else {
            // Clear all lfapi:* keys
            foreach (redis()->keys('lfapi:*') as $k) {
                redis()->del($k);
            }
        }
    }
);

use Jcolombo\LeadfeederApiPhp\Configuration;

Configuration::set('rateLimit.enabled', false);

use Jcolombo\LeadfeederApiPhp\Configuration;

// Silence all fatal errors (use with caution)
Configuration::set('error.handlers.fatal', []);

// Add echo output to warn-level errors
Configuration::set('error.handlers.warn', ['log', 'echo']);

// Trigger native PHP errors (useful for frameworks with custom error handlers)
Configuration::set('error.triggerPhpErrors', true);

// Disable error handling entirely
Configuration::set('error.enabled', false);

use Jcolombo\LeadfeederApiPhp\Leadfeeder;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

// Access to the raw response is available indirectly; entity fetch methods return
// the entity itself on success. For most usage, the fluent resource API is sufficient.
// The RequestResponse is available inside the Leadfeeder::execute() pipeline.

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$lead = Lead::new($lf)->fetch('abc123');

// Magic accessor (preferred for reading)
echo $lead->name;
echo $lead->employee_count;  // integer
echo $lead->first_visit_date;  // date string

// Explicit method (preferred when the property name is dynamic)
echo $lead->get('website_url');

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$lead = Lead::new($lf)->fetch('abc123');

// 'tags' is typed as 'array' — a flat array of strings
$tags = $lead->tags;
if (is_array($tags)) {
    echo implode(', ', $tags) . PHP_EOL;
}

// 'industries' is typed as 'array:object' — array of associative arrays
$industries = $lead->industries;
if (is_array($industries)) {
    foreach ($industries as $industry) {
        echo $industry['name'] ?? '' . PHP_EOL;
    }
}

// 'employees_range' is typed as 'object' — a single associative array
$range = $lead->employees_range;
if (is_array($range)) {
    echo $range['min'] . ' - ' . $range['max'] . PHP_EOL;
}

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$lead = Lead::new($lf)->fetch('abc123');

// Convert single entity to array or JSON
$array = $lead->toArray();
$json  = $lead->toJson();

// Collections implement JsonSerializable — json_encode works directly
$leads = Lead::list($lf)
    ->dateRange('2024-01-01', '2024-01-31')
    ->fetch();

$jsonString = json_encode($leads);

// flatten() extracts a single property from all entities in the collection
$names = $leads->flatten('name');  // array of all lead names

// raw() returns the keyed array of entity objects (keyed by entity ID)
$raw = $leads->raw();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Account;

// Two separate connections for two different API users
$lf1 = Leadfeeder::connect('token-for-client-a');
$lf2 = Leadfeeder::connect('token-for-client-b');

$lf1->setAccount('account-id-a');
$lf2->setAccount('account-id-b');

// Each connection operates independently
$accountsA = Account::list($lf1)->fetch();
$accountsB = Account::list($lf2)->fetch();

// Disconnect a specific token when done
Leadfeeder::disconnect('token-for-client-a');

// Or disconnect everything
Leadfeeder::disconnect();

use Jcolombo\LeadfeederApiPhp\Leadfeeder;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Lead;
use Jcolombo\LeadfeederApiPhp\Entity\Resource\Visit;

$lf = Leadfeeder::connect('your-api-token-here');
$lf->setAccount('your-account-id');

$enrichClient = Leadfeeder::connectIpEnrich('your-discover-api-key');

// Fetch leads for the past week
$leads = Lead::list($lf)
    ->dateRange('2024-01-22', '2024-01-28')
    ->has('status', 'new')
    ->fetch();

foreach ($leads as $lead) {
    // Fetch the most recent visit for this lead to get its IP context
    $visits = Visit::list($lf)
        ->dateRange('2024-01-22', '2024-01-28')
        ->forLead($lead->id)
        ->pageSize(1)
        ->fetch();

    foreach ($visits as $visit) {
        // Enrich with Discover API using visitor IP (if available from visit context)
        $company = $enrichClient->lookup('203.0.113.' . rand(1, 254));
        if ($company !== null) {
            echo $lead->name . ' enriched with: ' . $company['name'] . PHP_EOL;
        }
    }
}
bash
composer