【发布时间】:2018-11-21 11:10:04
【问题描述】:
我需要更新一个用于使用 SOAP API 创建事件工单的网络表单。现在它需要通过 ServiceNow 与 REST API 一起使用,坦率地说,我不知道从哪里开始。
现有网络表单的工作方式如下: 填写表格并按下提交按钮后,“submit.js”被触发。该函数将网络表单中的所有信息发布到“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 文件。这个有点像我正在寻找的结构,但我不知道如何使用它。
“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,我将不胜感激。
最好的 蒂姆
【问题讨论】:
-
看起来您的 swagger.json 中的 send-dev.servicenow.com 不可用 - 所以第一个问题 - 使用该 API 的工作服务在哪里?
-
要“运行” swagger.json(可视化它并发送请求)使用 swagger-ui swagger.io/tools/swagger-ui(还有 dockerized 版本:hub.docker.com/r/swaggerapi/swagger-ui(使用 docker 轻松运行))
-
您好,Kamil,感谢您的快速回答。我不想透露我在哪家公司工作,这是我在发布之前编辑源代码的原因。
-
我可能不明白你的问题。你可以去editor.swagger.io把你的json(它有2个错误——提示:删除“”)放在左边——然后在右边查看请求。使用该 API,您只能发出一个请求 POST send-dev.servicenow.com/api/ServiceNow/devb/incident/v2.5/…
-
所以你的 swagger.json 只描述了一个 API 请求。您需要询问 API 提供商如何“登录”该 API
标签: php rest api swagger servicenow