PHP code example of rajangdavis / osvc_php

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

    

rajangdavis / osvc_php example snippets



// Configuration is as simple as requiring the library
// and passing in an associative array

nterface credentials
	"password" => getenv("OSVC_PASSWORD"),			# => store these in environmental
	"interface" => getenv("OSVC_SITE"),			# => variables in your .bash_profile

	// Custom Domain for custom vhost
	// "custom_domain" => getenv("OSVC_VHOST"),

	// Session Authentication
	// "session" => <session ID>,

	// OAuth Token Authentication
	// "oauth" => <oauth token>,

	### optional configuration
	# Use 'rightnowdemo' namespace instead of 'custhelp'
	"demo_site" => true					# => Defaults to false. 

	# Sets the version of the REST API to use
	"version" => 'v1.4',					# => Defaults to 'v1.3'. 
	
	# Turns off SSL verification; don't use in production
	"no_ssl_verify" => true,				# => Defaults to false. 
	
	# Lets you supress business rules
	"suppress_rules" => true,				# => Defaults to false. 

	# Lets you supress external events
	"suppress_events" => true,				# => Defaults to false. 

	# Lets you supress external events and business rules
	"suppress_all" => true,				# => Defaults to false. 
	
	# Adds an access token to ensure quality of service
	"access_token" =>  "My access token" 		
));





$rn_client = new OSvCPHP\Client(array(
    "username" => getenv("OSVC_ADMIN"),	
    "password" => getenv("OSVC_PASSWORD"),
    "interface" => getenv("OSVC_SITE"),
    "suppress_rules" => true	
));

$options = array(
	
	// set the client for the request
	"client" => $rn_client,

	// Adds a custom header that adds an annotation (CCOM version must be set to "v1.4" or "latest"); limited to 40 characters
	"annotation" => "Custom annotation",

	// Prints request headers for debugging  
	"debug" => true,

	// Adds a custom header to excludes null from results; for use with GET requests only                 	 
	"exclude_null" => true,

	// Number of milliseconds before another HTTP request can be made; this is an anti-DDoS measure
	"next_request" => 500,

	// Sets 'Accept' header to 'application/schema+json'
	"schema" => true,

	// Adds a custom header to return results using Coordinated Universal Time (UTC) format for time (Supported on November 2016+
	"utc_time" => true              	 
);


//// OSvCPHP\Connect::post(options)
//// returns an object

// Here's how you could create a new ServiceProduct object
// using PHP variables and associative arrays (sort of like JSON)

ng data
// for creating
// a new product 

$new_product = array(
    'names' => array(
        array(
            'labelText' => 'NEW_PRODUCT',
            'language' => array('id' => 1)
        )
    ),
    'displayOrder' => 4,
    'adminVisibleInterfaces' => array(
        array(
            'id' => 1
        )
    ),
    'endUserVisibleInterfaces' => array(
        array(
            'id' => 1
        )
    ),
);


$options = array(
	"client" => $rn_client,
	"url" => "serviceProducts",
	"json" => $new_product,
	"debug" => true
);

$post_response = OSvCPHP\Connect::post($options);


//// OSvCPHP\Connect::get(options)
//// returns an object
// Here's how you could get an instance of ServiceProducts

	"interface" => getenv("OSVC_SITE"),
));


$options = array(
	"client" => $rn_client,
	"url" => "serviceProducts/56",
);

$post_response = OSvCPHP\Connect::get($options);

//// OSvCPHP\Connect::patch(options)
//// returns an object
// Here's how you could update a Service Product object
// using JSON objects
// to set field information
ed_product = array(
    'names' => array(
        array(
            'labelText' => 'UPDATED_PRODUCT',
            'language' => array('id' => 1)
        )
    ),
    'displayOrder' => 4,
    'adminVisibleInterfaces' => array(
        array(
            'id' => 1
        )
    ),
    'endUserVisibleInterfaces' => array(
        array(
            'id' => 1
        )
    ),
);


$options = array(
	"client" => $rn_client,
	"url" => "serviceProducts/268",
	"json" => $updated_product,
);

$patch_response = OSvCPHP\Connect::patch($options);

//// OSvCPHP\Connect::delete(options)
//// returns an object
// Here's how you could delete a serviceProduct object
,
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
	"client" => $rn_client,
	"url" => "serviceProducts/56",
);

$delete_response = OSvCPHP\Connect::delete($options);


//// OSvCPHP\Connect::options(options)
//// returns headers object or a raw Response object on error
// Here's how you can fetch options for incidents
"),
));

$options = array(
	"client" => $rn_client,
	"url" => "incidents",
);

$options_response = OSvCPHP\Connect::options($options);




$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
    "client" => $rn_client,
    "url" => "incidents",
    "json" =>  array(
        "primaryContact"=>  array(
            "id"=>  2
        ),
        "subject"=>  "FishPhone not working"
    ), "files" => array(
        "./test.php",
    )
);

$post_response = OSvCPHP\Connect::post($options);




$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
    "client" => $rn_client,
    "url" => '/incidents/25872/fileAttachments/417?download'
);

$get_response = OSvCPHP\Connect::get($options); // returns 1 on success




$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
    "client" => $rn_client,
    "url" => '/incidents/25872/fileAttachments?download'
);

$get_response = OSvCPHP\Connect::get($options); // returns 1 on success




$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
	"query" => "DESCRIBE CONTACTS",
	"client" => $rn_client
);

$q = new OSvCPHP\QueryResults;

$results = $q->query($options);



// Pass in each query into a hash
// set query: to the query you want to execute
// set key: to the value you want the results to of the query to be referenced to
eries = array(
	array(
		"query" => "DESCRIBE ANSWERS",
		"key" => "answerSchema"
	),
 	array(
 		"query" => "SELECT * FROM ANSWERS LIMIT 1",
 		"key" => "answers"
 	),
 	array(
 		"query" => "DESCRIBE SERVICECATEGORIES",
 		"key" => "categoriesSchema"
 	),
 	array(
 		"query" => "SELECT * FROM SERVICECATEGORIES",
 		"key" =>"categories"
 	),
 	array(
 		"query" => "DESCRIBE SERVICEPRODUCTS",
 		"key" => "productsSchema"
 	),
 	array(
 		"query" => "SELECT * FROM SERVICEPRODUCTS",
 		"key" =>"products"
 	)
);


$options = array(
    "client" => $rn_client,
    "queries" => $queries
);

$mq = new OSvCPHP\QueryResultsSet;

$results = $mq->query_set($options);


//  Results for "DESCRIBE ANSWERS"
// 
//  [
//   {
//     "Name": "id",
//     "Type": "Integer",
//     "Path": ""
//   },
//   {
//     "Name": "lookupName",
//     "Type": "String",
//     "Path": ""
//   },
//   {
//     "Name": "createdTime",
//     "Type": "String",
//     "Path": ""
//   }
//   ... everything else including customfields and objects...
// ]

//  Results for "SELECT * FROM ANSWERS LIMIT 1"
// 
//  [
//   {
//     "id": 1,
//     "lookupName": 1,
//     "createdTime": "2016-03-04T18:25:50Z",
//     "updatedTime": "2016-09-12T17:12:14Z",
//     "accessLevels": 1,
//     "adminLastAccessTime": "2016-03-04T18:25:50Z",
//     "answerType": 1,
//     "expiresDate": null,
//     "guidedAssistance": null,
//     "keywords": null,
//     "language": 1,
//     "lastAccessTime": "2016-03-04T18:25:50Z",
//     "lastNotificationTime": null,
//     "name": 1,
//     "nextNotificationTime": null,
//     "originalReferenceNumber": null,
//     "positionInList": 1,
//     "publishOnDate": null,
//     "question": null,
//     "solution": "<HTML SOLUTION WITH INLINE CSS>",
//     "summary": "SPRING IS ALMOST HERE!",
//     "updatedByAccount": 16,
//     "uRL": null
//   }
// ]

//  Results for "DESCRIBE SERVICECATEGORIES"
//  
// [
// ... skipping the first few ... 
//  {
//     "Name": "adminVisibleInterfaces",
//     "Type": "SubTable",
//     "Path": "serviceCategories.adminVisibleInterfaces"
//   },
//   {
//     "Name": "descriptions",
//     "Type": "SubTable",
//     "Path": "serviceCategories.descriptions"
//   },
//   {
//     "Name": "displayOrder",
//     "Type": "Integer",
//     "Path": ""
//   },
//   {
//     "Name": "endUserVisibleInterfaces",
//     "Type": "SubTable",
//     "Path": "serviceCategories.endUserVisibleInterfaces"
//   },
//   ... everything else 



$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
	"client" => $rn_client,
	"json" => array(
		"filters" => array(
			array(
				"name" => "search_ex",
				"values" => array("returns")
			)
    	),
    	"limit" => 2,
    	"id" => 176
	)
);

$arr = new OSvCPHP\AnalyticsReportResults;

$arrResults = $arr->run($options);




$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
	"version" => "latest"
));

$options = array(
	"client" => $rn_client,
	"query" => "DELETE FROM INCIDENTS LIMIT 10",
	"annotation" => "Delete example"
);

$q = new OSvCPHP\QueryResults;

$results = $q->query($options);



$rn_client = new OSvCPHP\Client(array(
	"username" => getenv("OSVC_ADMIN"),
	"password" => getenv("OSVC_PASSWORD"),
	"interface" => getenv("OSVC_SITE"),
));

$queries = array(
    array(
        "query" => "DESCRIBE INCIDENTS",
        "key" => "incidents"
    ),
    array(
        "query" => "DESCRIBE SERVICEPRODUCTS",
        "key" => "serviceProducts"
    ),
);

$options = array(
    "client" => $rn_client,
    "queries" => $queries,
    "concurrent" => true
);

$mq = new OSvCPHP\QueryResultsSet;

$results = $mq->query_set($options);



// Find our position in the file tree
if (!defined('DOCROOT')) {
$docroot = get_cfg_var('doc_root');
define('DOCROOT', $docroot);
}
 
/************* Agent Authentication ***************/
 
// Set up and call the AgentAuthenticator
enticator::authenticateCredentials($username,$password));




// initialize a curl request
$ch = curl_init(); 
// set the base of the url
$url = "https://". getenv('OSVC_SITE') .".custhelp.com/cgi-bin/"
// add the location of the above file
$url .= getenv('OSVC_CONFIG') .".cfg/php/custom/login_test.php"
// add the credentials for getting a session ID
$url .= "?username=". getenv('OSVC_ADMIN') ."&password=". getenv('OSVC_PASSWORD');
// set the URL
curl_setopt($ch, CURLOPT_URL, "$url"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
// execute
$output = curl_exec($ch); 
// close curl
curl_close($ch);      

$session_id = json_decode($output)->session_id;

$rn_client = new OSvCPHP\Client(array(
    "session" => $session_id,
	"interface" => getenv("OSVC_SITE"),
));

$options = array(
    "client" => $rn_client,
    "query" => "SELECT * FROM INCIDENTS LIMIT 10"
);

$mq = new OSvCPHP\QueryResults();

$results = $mq->query($options);

echo json_encode($results, JSON_PRETTY_PRINT);