PHP application to create Incident with attachment using SOAP web service
General Add commentsby: Bert Stapel
Jeffry Meijer published earlier an article about Windows application to create Incident with attachment using SOAP web service.
In this article I will talk about achieving similar functionality using PHP.
In order to communicate with ServiceNow you have to establish a connection through web services.
These phases are explained on the SNC wiki step by step.
Normally you can upload an attachment by specifying the folder path in the programming code.
For security reasons this does not work in ServiceNow since foreign programming code is not allowed on the ServiceNow servers.
But there is a workaround called Base64.
This function encodes the attachment file on the client side and through a directional SOAP message is able to communicate with ServiceNow.
Step 1: Create PHP code to create the incident and attachment in Service-Now
[cc lang=”php”]
“admin”, ‘password’ => “admin”));
$params = array(
‘comments’ => $_POST[‘mDescription’],
‘short_description’ => $_POST[‘sShortDescription’],
‘caller_id’ => $_POST[‘sCaller’]
);
$return = $client->insert($params);
$return_inc = array();
foreach ($return as $key => $value) {
$return_inc[$key] = $value;
}
$incident_sysid = “incident:” . $return_inc[sys_id];
$attachmentspecs = $_FILES[‘fAttachment’][‘name’] . “:” . $_FILES[‘fAttachment’][‘type’];
$fd = fopen ($_FILES[‘fAttachment’][‘tmp_name’], ‘rb’);
$size=filesize ($_FILES[‘fAttachment’][‘tmp_name’]);
$imgbinary = fread ($fd, $size);
fclose ($fd);
$imgbase64 = base64_encode($imgbinary);
//HERE WE UPLOAD THE ATTACHMENT TO THE TICKET IN SNC IF ANY SPECIFIED
if (isset($_FILES[‘fAttachment’][‘name’])){
try {
$wsdl_url = ‘https://demo.service-now.com/ecc_queue.do?WSDL’;
$clientattachment = new SOAPClient($wsdl_url, array(‘login’ => “admin”, ‘password’ => “admin”));
$params = array(
‘agent’ => “AttachmentCreator”,
‘name’ => $attachmentspecs,
‘payload’ => $imgbase64,
‘source’ => $incident_sysid,
‘topic’ => “AttachmentCreator”,
);
$return = $clientattachment->insert($params);
} catch (Exception $e) {
echo “Exception occured: ” . $e;
}
}
} catch (Exception $e) {
echo “Exception occured: ” . $e;
}
?>
[/cc]
Step 2: Create a form which holds the input fields
[cc lang=”html”]

Below you can specify your name, short description, description and also choose an attachment. With this data
a ticket will be created in the demo instance of service-now.com.
Recent Comments