-
-
Save bajpaik/eaa2d0c8c71ca5220cce015c05127e8f 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('America/Los_Angeles'); | |
DEFINE("INSTANCE","central1"); | |
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