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

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);


?>


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi M F,



Excellent!!


Thanks for posting and figuring it out!



Best Regards



Tony