不知道从哪里开始使用REST API创建ServiceNow事件

时间:2018-11-21 11:10:04

标签: php rest api swagger servicenow

我需要更新一个用于使用SOAP API创建故障单的Web表单。现在它需要由ServiceNow与REST API一起使用,并且坦白地说,我不知道从哪里开始。

现有网络表单的工作方式如下: 填写表格并按下提交按钮后,将触发“ submit.js”。此功能将Web表单中的所有信息发布到“ soapclient.php”。此soapclient.php连接到SOAP客户端,并将所有数据传输到SOAP API。

“ soapclient.php”看起来如下:

<?php
    $wsdl =wsdl-server;
    $client = new SoapClient($wsdl, array('login' => "USER",
                                          'password' => "PW“,
                                          'trace'=> 1));

    $title = $_REQUEST['title'];
    $affuser = $_REQUEST['affuser'];
    $description = $_REQUEST['description'];
    $solutionstring = '';
    $solutioncode = '';
    $resolveimmediately = '';

 [[SEVERAL IF-ELSE STATEMENTS]]

    $request = array(
            'model'=>array(
                'keys'=>array(),
                'instance'=>array(
                    'registrationId' => ID,
                    'affectedUserId' => $affuser,
                    'serviceId' => $serviceId,
                    'affectedCiId' => '',
                    'priority' => '4',
                    'title' => $title,
                    'description' => "$description\n$timestring$errorstring$phonestring",
                    'resolveImmediately' => $resolveimmediately,
                    'solutionCode' => $solutioncode,
                    'solution' => $solutionstring
        )));

$response = $client->SubmitIntApiIncident($request);

我很确定我只需要将“ soapclient.php”更改为使用新的REST API而不是SOAP API。但这是我不知道从哪里开始的部分。 REST API使用一个API密钥+一个通用用户(我俩都有)-但我不知道在哪里使用它们。

我仅有的线索是可以下载的swagger.json文件。这种KINDA看起来像我正在寻找的结构,但是我不知道如何使用它。

“ swagger.json”看起来如下:

{
  "swagger" : "2.0",
  "host" : "send-dev.servicenow.com",
  "basePath" : "/api/ServiceNow/devb/incident/v2.5",
  "schemes" : [ "https" ],
  "paths" : {
    "/incident/createIncidentMethod" : {
      "post" : {
        "description" : "Create incident\n",
        "operationId" : "POST /incident/createIncidentMethod",
        "parameters" : [ {
          "description" : "Create Incident",
          "required" : false,
          "in" : "body",
          "name" : "body",
          "schema" : {
            "properties" : {
              "header" : {
                "properties" : {
                  "transactionid" : {
                    "type" : "string"
                  },
                  "sourcesystemid" : ""{
                    "type" : "string"
                  },
                  "targetsystemid" : " "{
                    "type" : "string"
                  }
                },
                "type" : "object"
              },
              "content" : {
                "properties" : {
                  "caller_id" : {
                    "type" : "string"
                  },
                  "category" : {
                    "type" : "string"
                  },
                  "subcategory" : {
                    "type" : "string"
                  },
                  "business_service" : {
                    "type" : "string"
                  },
                  "ci_name" : {
                    "type" : "string"
                  },
                  "impact" : {
                    "type" : "string"
                  },
                  "urgency" : {
                    "type" : "string"
                  },
                  "assignment_group" : {
                    "type" : "string"
                  },
                  "assigned_to" : {
                    "type" : "string"
                  },
                  "short_description" : {
                    "type" : "string"
                  },
                  "state" : {
                    "type" : "string"
                  },
                  "close_code" : {
                    "type" : "string"
                  },
                  "close_notes" : {
                    "type" : "string"
                  },
                  "service_offering" : {
                    "type" : "string"
                  },
                  "affected_user" : {
                    "type" : "string"
                  },
                  "description" : {
                    "type" : "string"
                  },
                  "correlation_id" : {
                    "type" : "string"
                  },
                  "ci_sysid" : {
                    "type" : "string"
                  }
                },
                "type" : "object"
              },
              "attachment" : {
                "properties" : {
                  "file_name" : {
                    "type" : "string"
                  },
                  "mime_type" : {
                    "type" : "string"
                  },
                  "base64string" : {
                    "type" : "string"
                  }
                },
                "type" : "object"
              }
            },
            "type" : "object"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "Success",
            "schema" : {
              "type" : "object"
            }
          },
          "401" : {
            "description" : "Not authorized",
            "schema" : {
              "type" : "object"
            }
          },
          "610" : {
            "description" : "User doesn't exist.",
            "schema" : {
              "type" : "object"
            }
          },
          "611" : {
            "description" : "Group doesn't exist.",
            "schema" : {
              "type" : "object"
            }
          },
          "612" : {
            "description" : "User is not member of the group.",
            "schema" : {
              "type" : "object"
            }
          },
          "613" : {
            "description" : "Incident State is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "405" : {
            "description" : "Not supported. Invalid parameters.",
            "schema" : {
              "type" : "object"
            }
          },
          "603" : {
            "description" : "Missing mandatory information.",
            "schema" : {
              "type" : "object"
            }
          },
          "614" : {
            "description" : "Incident Category is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "615" : {
            "description" : "Incident Subcategory is invalid.",
            "schema" : {
              "type" : "object"
            }
          },
          "616" : {
            "description" : "Only one input is allowed. Configuration item Name or SysId.",
            "schema" : {
              "type" : "object"
            }
          },
          "606" : {
            "description" : "Record not found.",
            "schema" : {
              "type" : "object"
            }
          },
          "618" : {
            "description" : "CI name is not unique."
          },
          "608" : {
            "description" : "Impact is invalid. Impact must be 1, 2 or 3.",
            "schema" : {
              "type" : "object"
            }
          },
          "609" : {
            "description" : "Urgency is invalid. Urgency must be 1, 2 or 3.",
            "schema" : {
              "type" : "object"
            }
          }
        }
      }
    }
  },
  "info" : {
    "title" : "ServiceNow Incident v2.5",
    "description" : "This REST API provides methods to create/update/retrieve ITSM incident module data from ServiceNow. This API uses Basic authentication (plus API Key). If there is a need to consume from outside the network, there is another version of this API that is configured for two factor authentication.",
    "version" : "1.0.0",
    "x-summary" : "SNOW Incident API"
  }
}

如果有人可以给我提示如何使用新API或如何更改soapclient.php以使其与新API配合使用,我将非常感激。

最佳 蒂姆

1 个答案:

答案 0 :(得分:0)

您的swagger.json不是有效的JSON(但是,它具有易于删除的小错误-在第23和26行上,请在括号前删除" ")。您可以在https://editor.swagger.io/中查看该文件。该文件仅描述了一个请求:

POST http://send-dev.servicenow.com/api/ServiceNow/devb/incident/v2.5/incident/createIncidentMethod

要将请求(主体中带有正确的json的POST)发送到该API形式的php,您可以使用GUZZLE库而不是SoapClient(枪口有很好的文档)。

在swagger.json中,您只有以下信息:

  

此REST API提供了创建/更新/检索ITSM事件的方法   来自ServiceNow的模块数据。此API使用基本身份验证(加上   API密钥)。如果需要从网络外部进行消费,   该API的另一个版本针对两个因素进行了配置   身份验证。

但没有详细信息-但是使用了关键词"Basic Authentication"。因此,您可以尝试将以下标头添加到POST请求中:

Authorization: Basic username:password_base64

其中username:password_base64是base64的凭据代码-以用户demo为例,并通过p@55w0rd,您需要对base64字符串“ demo:p @ 55w0rd”进行编码并添加标头:

Authorization: Basic ZGVtbzpwQDU1dzByZA== 

(它应该与http / ssl一起使用,因为base64易于解码)。如果这不起作用,那么您需要询问您的API提供者:如何“登录”到该API(某些API仅需要其他路径参数,例如“?key = abc ... xyz”)。