The CreatorCon Call for Content is officially open! Get started here.

how to create incident with PHP/fsockopen

m_f1
Giga Contributor

Hello,

I am trying to add a feature to certain programs we have that will automatically generate incidents. I was looking at the documentation here

https://express.servicenow.com/support/documentation/r_RESTTableAPICreateRecord/

and trying to test generating a new incident. I am required to use PHP and fsockopen to send the request. The code looks like this:

<?php

$host = 'ourdomain.service-now.com';

$path = '/api/now/table/incident';

$fp = fsockopen($host, 443, $err_num, $err_str, 30);

print_r($fp);

if(!$fp){

echo $err_num . ' ' . $err_str . "\n";

}

else{

$eol = "\r\n";

$out = 'POST /api/now/table/incident HTTP/1.1' . $eol;

$out .= 'Host: www.ourdomain.com' . $eol;

$out .= 'Content-Type: application/json' . $eol;

$out .= 'Accept: application/json' . $eol;

$out .= 'Connection: Close' . $eol . $eol;

$d = array(

'short_description' => 'test from ss'

);

$out .= json_encode($d) . $eol . $eol;

echo $out;

fwrite($fp, $out);

$contents = ";

while(!feof($fp)){

$contents .= fgets($fp, 4096);

}

fclose($fp);

secho($contents);

}

?>

It generates a request that looks like this:

POST /api/now/table/incident HTTP/1.1

Host: http://www.ourdomain.com

Content-Type: application/json

Accept: application/json

Connection: Close

{"short_description":"test from ss"}

Can you tell me if that request is correct, or if there are additional fields that need to be sent?

Is the port correct (443)?

Anything else you see wrong?

I don't believe it's correct because I am receiving no response from your server.

Thank you!

1 ACCEPTED SOLUTION

I figured it out!



<?php


$auth = base64_encode('username:pwd');



$host = 'ourdomain.service-now.com';



$path = '/api/now/table/incident';


     


$eol = "\r\n";





//file_get_contents


      $d = array(


                      'short_description' => 'test from ss'


              );


             


              $out = json_encode($d);


             


      // Create a stream


      $opts = array(


              'http' => array(


                      'method' => 'POST',


                      'header' =>


                              'Content-Type: application/json' . $eol .


                              'Accept: application/json' . $eol .


                              'Authorization: Basic ' . $auth . $eol . $eol,


                      'content' => $out


              )


      );



      print_r($opts);


     


      $context = stream_context_create($opts);



      $file = file_get_contents('https://' . $host . $path, false, $context);



      echo($file);


?>


View solution in original post

6 REPLIES 6

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi MF,



-> Can you tell me if that request is correct, or if there are additional fields that need to be sent?


Maybe it will be helpful for me to post a curl command that does work - which I generated from the REST API Explorer.



curl "https://instancename.service-now.com/api/now/table/incident" --request POST --header "Accept:application/json" --header "Content-Type:application/json" --data "{'short_description':'test from xx'}" --user 'abel.tuter':'!qwertyuiop!'



-> Is the port correct (443)?


yes



-> Anything else you see wrong?


How are you defining the user name and password?



I have not used php and REST myself but this url has some guidance


Learn REST: A Tutorial: 12.5. Using REST in PHP



Best Regards



Tony


Hi MF,



Just had a additional thought - could you use PHP libcurl?


PHP: cURL - Manual



Best Regards



Tony


Hi MH,



1)   Your remote host is the ServiceNow instance that requires you to connect via https - so I expect this PHP guidance is relevant:


PHP: fsockopen - Manual


..


hostname


If OpenSSL support is installed, you may prefix the hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.


Does your PHP have OpenSSL support?


2) Here is an example of PHP libcurl, which makes it pretty easy to exercise the REST API, as the REST API Explorer creates curl commands for you.


Does your PHP have libcurl available?



<?php


$curl = curl_init();


curl_setopt($curl, CURLOPT_URL, "https://<instancename>.service-now.com/api/now/table/incident");


curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);


curl_setopt($curl,CURLOPT_USERPWD,"admin:1qwertyuiop!");


curl_setopt($curl, CURLOPT_POST, 1);


curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));


curl_setopt($curl, CURLOPT_POSTFIELDS, "{'short_description':'test from xx'}");


$result = curl_exec($curl);


$json = json_decode($result, true);


print_r($json);


curl_close($curl);


?>



output is:



Array


(


      [result] => Array


              (


                <snip>


                      [number] => INC0010006


                    <snip>


                      [short_description] => test from xx


                <snip>


                      [sys_id] => 588eb10c135712003c4ebdb12244b0d3


                  <snip>


              )


)




Best Regards



Tony


First, thank you for your replies! I have made some progress and am very close. I was able to create an incident and get a response from the server using this:



<?php


$auth = base64_encode('username:pwd');



$host = 'ourdomain.service-now.com';



$path = '/api/now/table/incident';


     


$eol = "\r\n";





//file_get_contents


      $d = array(


                      'short_description' => 'test from ss'


              );


             


              $out = json_encode($d) . $eol . $eol;


             


      // Create a stream


      $opts = array(


              'http' => array(


                      'method' => 'POST',


                      'header' =>


                              'Content-Type: application/json' . $eol .


                              'Accept: application/json' . $eol .


                              'Authorization: Basic ' . $auth . $eol . $eol .


                              'Connection: Close' . $eol . $eol .


                              $out


              )


      );



      $context = stream_context_create($opts);



      $file = file_get_contents('https://' . $host . $path, false, $context);



      echo($file);


?>



The only part that didn't work was it did not insert the short_description text in the incident. Do you see anything wrong with how I'm creating the contents of the JSON sent in the request?