PHP code example of onbalt / servicedeskplus-api

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

    

onbalt / servicedeskplus-api example snippets


Onbalt\ServicedeskplusApi\ServicedeskplusApiServiceProvider::class,

'ServicedeskplusApi' => Onbalt\ServicedeskplusApi\Facades\ServicedeskplusApi::class,

return [
	'api_base_url' => env('SDPAPI_BASE_URL', 'http://helpdesk.local/api/v3/'),
	'technician_key' => env('SDPAPI_TECHNICIAN_KEY', 'key'),
	'api_version' => env('SDPAPI_VERSION', '3'),
	'api_v1_format' => env('SDPAPI_V1_FORMAT', 'json'),
	'timeout' => 60,
];

use Onbalt\ServicedeskplusApi\Facades\ServicedeskplusApi;

// View Request
$response = ServicedeskplusApi::get('requests/111');
var_dump($response->request);



use Onbalt\ServicedeskplusApi\ServicedeskplusApi;

$config = [
	'api_base_url' => 'http://helpdesk.local/api/v3/',
	'technician_key' => 'YOUR_TECHNICIAN_KEY',
	'api_version' => '3',
	'api_v1_format' => 'json',
	'timeout' => 60,
];

$sdpApi = new ServicedeskplusApi($config);

// View Request
$response = $sdpApi->get('requests/111');
var_dump($response->request);

$response = ServicedeskplusApi::get('requests/111');
var_dump($response);

object(stdClass)# (2) {
  ["request"]=>
  object(stdClass)# (50) {
    //<all request properties in V3 format>
    //@see: http://ui.servicedeskplus.com/APIDocs3/index.html#request8
  }
  ["response_status"]=>
  object(stdClass)# (2) {
    ["status_code"]=>
    int(2000)
    ["status"]=>
    string(7) "success"
  }
}

$inputData = ServicedeskplusApi::prepareJsonInputData([
	'list_info' => [ //@see: http://ui.servicedeskplus.com/APIDocs3/index.html#list-info
		'row_count' => 10, //max 100
		'start_index' => 1,
		'sort_field' => 'id',
		'sort_order' => 'asc',
		'get_total_count' => true,
		'search_criteria' => [
			[
				'field' => 'site.name',
				'condition' => 'eq',
				'value' => 'MyCompany',
			],
		],
	]
]);
$response = ServicedeskplusApi::get('requests', $inputData);
var_dump($response);

object(stdClass)# (3) {
  ["requests"]=>
  array(10) {
    [0]=>
    object(stdClass)# (15) {
      //<base request properties in V3 format>
      //@see: http://ui.servicedeskplus.com/APIDocs3/index.html#view-all-requests
    }
    [1]=>
    //<...>
  }
  ["response_status"]=>
  object(stdClass)# (2) {
    ["status_code"]=>
    int(2000)
    ["status"]=>
    string(7) "success"
  },
  ["list_info"]=>
  object(stdClass)# (8) {
    ["has_more_rows"]=>
    bool(true)
    ["get_total_count"]=>
    string(7) "true"
    ["total_count"]=>
    int(111)
    //<other source properties from $inputData's 'list_info' array>
  }
}

$inputData = ServicedeskplusApi::prepareJsonInputData([
	'request' => [
		'subject' => 'Subject of the request',
		'description' => 'HTML description of the request. Contains formatted text.',
		'requester' => [
			'name' => 'Dummy Requester',
			'email_id' => '[email protected]',
		],
		'site' => [
			'name' => 'Requester Company',
		],
		'impact' => [
			'name' => 'High',
		],
		'urgency' => [
			'name' => 'High',
		],
		'level' => [
			'name' => 'Tier 1',
		],
		'mode' => [
			'name' => 'Web Form',
		],
	]
]);
$workorder = ServicedeskplusApi::post('requests', null, $inputData);
var_dump($workorder->request->id);
//int(112)

$inputData = ServicedeskplusApi::prepareJsonInputData([
	'attachment' => [
		'request' => [
			'id' => 112,
		],
	]
]);
$filesData['multipart'] = [[
	'name' => 'file0',
	'contents' => fopen('/full/path/to/file.ext', 'r'),
	'filename' => 'file.ext',
]];
$response = ServicedeskplusApi::post('attachments', $inputData, $filesData);
var_dump($response);

object(stdClass)# (2) {
  ["response_status"]=>
  object(stdClass)# (2) {
    ["status_code"]=>
    int(2000)
    ["status"]=>
    string(7) "success"
  }
  ["attachment"]=>
  object(stdClass)# (4) {
    ["id"]=>
    int(101)
    ["file_name"]=>
    string(7) "file.ext"
    ["content_url"]=>
    string(7) "/api/v3/attachments/101"
    ["size"]=>
    string(7) "2.42KB"
  }
}

php artisan vendor:publish --tag=servicedeskplus-api-config