PHP code example of knops / sendfox-client

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

    

knops / sendfox-client example snippets




use GuzzleHttp\Client;use GuzzleHttp\Psr7\HttpFactory;use Knops\SendfoxClient\SendfoxClient;

// Setup HTTP client en factories
$httpClient = new Client();
$httpFactory = new HttpFactory();

// Initialiseer Sendfox client
$sendfox = new SendfoxClient(
    bearerToken: 'your-sendfox-api-token',
    httpClient: $httpClient,
    requestFactory: $httpFactory,
    streamFactory: $httpFactory
);

// Gebruik de API
$campaigns = $sendfox->campaigns()->all();
$contacts = $sendfox->contacts()->all();



use Knops\SendfoxClient\SendfoxClient;use Nyholm\Psr7\Factory\Psr17Factory;use Symfony\Component\HttpClient\Psr18Client;

$httpClient = new Psr18Client();
$psr17Factory = new Psr17Factory();

$sendfox = new SendfoxClient(
    bearerToken: 'your-sendfox-api-token',
    httpClient: $httpClient,
    requestFactory: $psr17Factory,
    streamFactory: $psr17Factory
);

// Alle campaigns ophalen
$campaigns = $sendfox->campaigns()->all();

// Specifieke campaign ophalen
$campaign = $sendfox->campaigns()->get(2490607);

// Alle contacten ophalen
$contacts = $sendfox->contacts()->all();

// Contacten filteren
$filtered = $sendfox->contacts()->all(
    query: 'john',              // Zoek op naam/email
    unsubscribed: true,         // Alleen uitgeschreven contacten
    email: '[email protected]'   // Specifiek emailadres
);

// Specifiek contact ophalen
$contact = $sendfox->contacts()->get(125534483);

// Nieuw contact aanmaken
$newContact = $sendfox->contacts()->create(
    email: '[email protected]',
    firstName: 'John',
    lastName: 'Doe',
    ipAddress: '192.168.1.1',
    lists: [123, 456],  // Voeg toe aan lijsten
    contactFields: [
        ['name' => 'company', 'value' => 'Acme Corp'],
        ['name' => 'phone', 'value' => '+31612345678']
    ]
);

// Contact uitschrijven
$sendfox->contacts()->unsubscribe('[email protected]');

// Uitgeschreven contacten ophalen
$unsubscribed = $sendfox->contacts()->unsubscribed();

// Zoeken in uitgeschreven contacten
$unsubscribedFiltered = $sendfox->contacts()->unsubscribed('john');

// Alle lijsten ophalen
$lists = $sendfox->lists()->all();

// Lijsten filteren
$filteredLists = $sendfox->lists()->all('newsletter');

// Specifieke lijst ophalen
$list = $sendfox->lists()->get(584322);

// Nieuwe lijst aanmaken
$newList = $sendfox->lists()->create('Nieuwe lijst naam');

// Contacten in lijst ophalen
$listContacts = $sendfox->lists()->contacts(584322);

// Contacten in lijst zoeken
$searchResults = $sendfox->lists()->contacts(584322, 'john');

// Contact uit lijst verwijderen
$sendfox->lists()->removeContact(584322, 125534483);

// Alle formulieren ophalen
$forms = $sendfox->forms()->all();

// Formulieren filteren
$filteredForms = $sendfox->forms()->all('signup');

// Specifiek formulier ophalen
$form = $sendfox->forms()->get(123);

// Alle automations ophalen
$automations = $sendfox->automations()->all();

// Specifieke automation ophalen
$automation = $sendfox->automations()->get(456);

// Huidige gebruiker informatie
$user = $sendfox->user()->me();

// Contact velden van gebruiker
$contactFields = $sendfox->user()->contactFields();

use Knops\SendfoxClient\Models\Campaign;
use Knops\SendfoxClient\Models\Contact;
use Knops\SendfoxClient\Models\ContactList;
use Knops\SendfoxClient\Models\Form;
use Knops\SendfoxClient\Models\Automation;
use Knops\SendfoxClient\Models\User;

// Creëer model van array data
$campaign = Campaign::fromArray($campaignData);
$contact = Contact::fromArray($contactData);
$list = ContactList::fromArray($listData);
$form = Form::fromArray($formData);
$automation = Automation::fromArray($automationData);
$user = User::fromArray($userData);

// Converteer model terug naar array
$array = $campaign->toArray();

$contact = Contact::fromArray($contactData);

// Toegang tot alle contact eigenschappen
echo $contact->id;              // Contact ID
echo $contact->email;           // Email address
echo $contact->first_name;      // Voornaam
echo $contact->last_name;       // Achternaam
echo $contact->ip_address;      // IP adres
echo $contact->unsubscribed_at; // Uitschrijf datum (null als niet uitgeschreven)
echo $contact->created_at;      // Aanmaak datum
echo $contact->updated_at;      // Update datum

// Controleer uitschrijf status
if ($contact->isUnsubscribed()) {
    echo "Contact is uitgeschreven op: " . $contact->unsubscribed_at;
} else {
    echo "Contact is actief";
}

$user = User::fromArray($userData);

// Toegang tot gebruiker eigenschappen
echo $user->name;            // Gebruikersnaam
echo $user->email;           // Email adres
echo $user->contacts_count;  // Aantal contacten
echo $user->contact_limit;   // Contact limiet

// Contact limiet monitoring
if ($user->isAtContactLimit()) {
    echo "Je hebt je contact limiet bereikt!";
}

// Contact gebruik percentage
$usage = $user->getContactUsagePercentage();
echo "Contact gebruik: {$usage}%";

$list = ContactList::fromArray($listData);

echo $list->name;                          // Lijst naam
echo $list->average_email_open_percent;    // Gemiddeld open percentage
echo $list->average_email_click_percent;   // Gemiddeld klik percentage

use Knops\SendfoxClient\Exceptions\SendfoxException;

try {
    $campaign = $sendfox->campaigns()->get(999999);
} catch (SendfoxException $e) {
    echo "API Error: " . $e->getMessage();
    echo "Status Code: " . $e->getCode();
}

// Symfony DI example
$container->register(SendfoxClient::class)
    ->setArguments([
        '$bearerToken' => '%sendfox.api_token%',
        '$httpClient' => new Reference(ClientInterface::class),
        '$requestFactory' => new Reference(RequestFactoryInterface::class),
        '$streamFactory' => new Reference(StreamFactoryInterface::class),
    ]);

use Psr\Http\Client\ClientInterface;
use PHPUnit\Framework\TestCase;

class SendfoxClientTest extends TestCase
{
    public function testCampaignsCall()
    {
        $httpClient = $this->createMock(ClientInterface::class);
        // Setup mock expectations...
        
        $sendfox = new SendfoxClient(
            'test-token',
            $httpClient,
            $requestFactory,
            $streamFactory
        );
        
        // Test your logic...
    }
}