PHP code example of zendesk / zendesk_api_client_php

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

    

zendesk / zendesk_api_client_php example snippets


$iterator = $client->tickets()->iterator();

foreach ($iterator as $ticket) {
    echo($ticket->id . " ");
}

$params = ['my' => 'param1', 'extra' => 'param2'];
$iterator = $client->tickets()->iterator($params);

foreach ($iterator as $ticket) {
    echo($ticket->id . " ");
}

$strategy = new CbpStrategy( // Or ObpStrategy or SinglePageStrategy
    "resources_key", // The root key with resources in the response, usually plural and in underscore
    [], // Extra params for your call
);
$iterator = PaginationIterator($client->tickets(), $strategy);
foreach ($ticketsIterator as $ticket) {
    // Use as normal
}

$iterator = $client->automations()->iterator($params, 'findActive');

use Zendesk\API\Traits\Utility\Pagination\PaginationIterator;
use Zendesk\API\Traits\Utility\Pagination\CbpStrategy;
$strategy = new CbpStrategy('automations', $params);
$iterator = new PaginationIterator(
    $client->automations(),
    $strategy,
    'findActive'
);

try {
    foreach ($iterator as $ticket) {
        // your code
    }
} catch (ApiResponseException $e) {
    $errorMessage = $e->getMessage();
    $errorDetails = $e=>getErrorDetails();
}

$pageSize = 100;
$pageNumber = 1;
do {
    // OBP: /path?per_page=100&page=2
    $response = $client->tickets()->findAll(['per_page' => $pageSize, 'page' => $pageNumber]);
    process($response->tickets); // Your implementation
    $pageNumber++;
} while (count($response->tickets) == $pageSize);

$client = new ZendeskAPI($subdomain);
$client->log_api_calls = true;

$client->getDebug();
 php
// load Composer
pClient as ZendeskAPI;

$subdomain = "subdomain";
$username  = "[email protected]"; // replace this with your registered email
$token     = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv"; // replace this with your token

$client = new ZendeskAPI($subdomain);
$client->setAuth('basic', ['username' => $username, 'token' => $token]);
 php
$ticket = $client->tickets()->create([
    'subject' => 'The quick brown fox jumps over the lazy dog',
    'comment' => [
        'body' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' .
                  'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
        'uploads'   => [$attachment->upload->token]
    ]
]);
 php
$tickets = $client->tickets()->sideload(['users', 'groups'])->findAll();
 php
// CBP: /path?page[size]=100
$response = $client->tickets()->findAll(['page[size]' => 100]);
process($response->tickets); // Your implementation
do {
    if ($response->meta->has_more) {
        // CBP: /path?page[after]=cursor
        $response = $client->tickets()->findAll(['page[after]' => $response->meta->after_cursor]);
        process($response->tickets);
    }
} while ($response->meta->has_more);