PHP application to create Incident with attachment using SOAP web service

General Add comments
by:

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”]



Create an Incident with attachment in the demo instance of Service-Now.com, provided by 2e2 Consulting, The Netherlands

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.



Short Description: Detailed Description: Your Name: Ideally this should be a name already in the system.
You can specify any name but a user will not be created for it.
Instead the ticket will show a empty Caller name.
Attachment:







[/cc]

Put above code blocks into a single PHP file in the order specified. So PHP code first and than the HTML code.

Step 3: Test your work

If you’ve followed the instructions as they were mentioned above you can now create an attachment within an incident in Service-now.com.

If you would like to use the code against your own instance then you should modify the following lines:

$wsdl_url = ‘https:///incident.do?WSDL’;
$client = new SOAPClient($wsdl_url, array(‘login’ => ““, ‘password’ => ““));

Comments are closed.