PHP code example of zammad / zammad-api-client-php

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

    

zammad / zammad-api-client-php example snippets




use ZammadAPIClient\Client;
$client = new Client([
    'url'           => 'https://myzammad.com', // URL to your Zammad installation
    'username'      => '[email protected]',  // Username to use for authentication
    'password'      => 'mypassword',           // Password to use for authentication
    // 'timeout'       => 15,                  // Sets timeout for requests, defaults to 5 seconds, 0: no timeout
    // 'debug'         => true,                // Enables debug output
    // 'verify'        => true,                // Enabled SSL verification. You can also give a path to a CA bundle file. Default is true.
]);

use ZammadAPIClient\ResourceType;
$ticket = $client->resource( ResourceType::TICKET )->get(34);

$ticket->setValue( 'title', 'My ticket title' );
$title = $ticket->getValue('title');
$all_values = $ticket->getValues();

$ticket_articles = $ticket->getTicketArticles();

$attachment_content = $ticket_article->getAttachmentContent(23);

$ticket->save();

if ( $ticket->isDirty() ) {...}

use ZammadAPIClient\ResourceType;

$ticket = $client->resource( ResourceType::TICKET );
$ticket->setValue( 'title', 'My new ticket' );
// ...
// Set additional values
// ...
$ticket->save(); // Will create a new ticket object in Zammad

use ZammadAPIClient\ResourceType;

// Fulltext search
$tickets = $client->resource( ResourceType::TICKET )->search('some text');

// Field specific search
$tickets = $client->resource( ResourceType::TICKET )->search('title:My Title');

// Field specific search with more than one field
$tickets = $client->resource( ResourceType::TICKET )->search('title:My Title AND priority_id:1');

// Pagination: Page 1, 25 entries per page
$tickets = $client->resource( ResourceType::TICKET )->search( 'some text', 1, 25 );

use ZammadAPIClient\ResourceType;

$tickets = $client->resource( ResourceType::TICKET )->search('some text');
if ( !is_array($tickets) ) {
    // Error handling
    print $tickets->getError();
}
else {
    // Do something with $tickets array
}

use ZammadAPIClient\ResourceType;

// Fetch all tickets (keep in mind the server-side limit, see 'Searching Resource objects')
$tickets = $client->resource( ResourceType::TICKET )->all();

// Fetch all tickets with pagination (keep in mind the server-side limit, see 'Searching Resource objects'), page 4, 50 entries per page
$tickets = $client->resource( ResourceType::TICKET )->all( 4, 50 );

use ZammadAPIClient\ResourceType;

$tickets = $client->resource( ResourceType::TICKET )->all( 4, 50 ); // pagination
if ( !is_array($tickets) ) {
    // Error handling
    print $tickets->getError();
}
else {
    // Do something with $tickets array
}

$ticket->delete();

use ZammadAPIClient\ResourceType;

// The third parameter 'Ticket' is the object type for which the ID will be given as first parameter.
$client->resource( ResourceType::TAG )->add( $ticket_id, 'tag 1', 'Ticket' );

use ZammadAPIClient\ResourceType;

$client->resource( ResourceType::TAG )->remove( $ticket_id, 'tag 1', 'Ticket' );

use ZammadAPIClient\ResourceType;

// The second parameter 'Ticket' is the object type for which the ID will be given as first parameter.
$tag = $client->resource( ResourceType::TAG )->get( $ticket_id, 'Ticket' );

// [ 'tag 1', 'tag 2' ]
$tags = $tag->getValue('tags')

use ZammadAPIClient\ResourceType;

$tags = $client->resource( ResourceType::TAG )->search('my tag');

use ZammadAPIClient\ResourceType;

$text_modules_csv_string = file_get_contents('text_modules.csv');

$client->resource( ResourceType::TEXT_MODULE )->import($text_modules_csv_string);


if ( $ticket->hasError() ) {
    print $ticket->getError();
}

$last_response = $client->getLastResponse();

$client->setOnBehalfOfUser('myuser');

$client->unsetOnBehalfOfUser();

use ZammadAPIClient\ResourceType;

$client->resource( ResourceType::TICKET );
json
"zammad/zammad-api-client-php": "^2.0"