Skip to content

Instantly share code, notes, and snippets.

@asimpkin
Last active November 6, 2019 00:49
Show Gist options
  • Save asimpkin/acf0857c49a29e52509279e4307fa38a to your computer and use it in GitHub Desktop.
Save asimpkin/acf0857c49a29e52509279e4307fa38a to your computer and use it in GitHub Desktop.
PHP function to query ServiceNow REST API to GET a list of records from a table.
<?php
date_default_timezone_set('America/Los_Angeles');
DEFINE("INSTANCE","instancename");
DEFINE("SITE", "http://".$_SERVER['SERVER_NAME']); // ENABLE THIS LINE WHEN PUSHED TO PROD
DEFINE("USER","username");
DEFINE("PASS","password");
function getXML($table,$query) {
$query=urlencode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.INSTANCE.'.service-now.com/'.$table.'_list.do?XML&useUnloadFormat=true&sysparm_query='.$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, USER .':'. PASS);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
foreach($xml->$table as $k=>$v) {
foreach($v as $ik => $iv) {
$t[(string)$ik]=(string)$iv;
}
$all[(string)$v->sys_id] = $t;
}
return $all;
} // end function
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment