rajangdavis / osvc_php
Oracle Service Cloud (前身为RightNow Technologies) REST API的非官方PHP包装器
Requires (Dev)
- phpunit/phpunit: ^5.0|^7.0
README
一个(正在开发中)的PHP库,用于使用Oracle Service Cloud REST API,受ConnectPHP API影响
安装PHP(针对Windows)
这里有一个Windows 10的安装说明视频。强烈推荐安装PHP 7。
如果你遇到SSL错误(你可能会),请遵循此链接,了解如何解决我对SSL一无所知的问题。
安装
使用composer安装
$ composer require rajangdavis/osvc_php --dev
兼容性
此PHP库在PHP 7.2.1上针对Oracle Service Cloud 2017年5月进行了测试。
它在Travis CI上针对版本7.2.1和5.6.2进行了测试。
从2015年5月起的所有HTTP方法都应该在Oracle Service Cloud的任何版本上工作;然而,在2016年5月之前的任何版本中查询项目可能会有问题。这是因为ROQL查询直到2016年5月才通过REST API公开。
基本用法
迄今为止工作的功能如下
- HTTP方法
- 为了创建对象和上传一个或多个文件附件,使用OSvCPHP\Connect Object进行POST请求
- 为了读取对象和下载一个或多个文件附件,使用OSvCPHP\Connect Object进行GET请求
- 为了更新对象,使用OSvCPHP\Connect Object进行PATCH请求
- 为了删除对象,使用OSvCPHP\Connect Object进行DELETE请求
- 为了查找给定URL的选项,使用OSvCPHP\Connect Object进行OPTIONS请求
- 运行ROQL查询一次一个或一组多个
- 运行报告
- 可选设置
以下是更高级(更复杂的)功能
身份验证
OSvCPHP\Client类让库知道用于与Oracle Service Cloud REST API交互的凭据和接口。如果您需要与多个接口交互或为不同的对象设置不同的头信息,这将非常有用。
// Configuration is as simple as requiring the library // and passing in an associative array require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), # => These are interface credentials "password" => getenv("OSVC_PASSWORD"), # => store these in environmental "interface" => getenv("OSVC_SITE"), # => variables in your .bash_profile // Custom Domain for custom vhost // "custom_domain" => getenv("OSVC_VHOST"), // Session Authentication // "session" => <session ID>, // OAuth Token Authentication // "oauth" => <oauth token>, ### optional configuration # Use 'rightnowdemo' namespace instead of 'custhelp' "demo_site" => true # => Defaults to false. # Sets the version of the REST API to use "version" => 'v1.4', # => Defaults to 'v1.3'. # Turns off SSL verification; don't use in production "no_ssl_verify" => true, # => Defaults to false. # Lets you supress business rules "suppress_rules" => true, # => Defaults to false. # Lets you supress external events "suppress_events" => true, # => Defaults to false. # Lets you supress external events and business rules "suppress_all" => true, # => Defaults to false. # Adds an access token to ensure quality of service "access_token" => "My access token" ));
可选设置
除了指定要使用的凭据、接口和CCOM版本的客户端之外,您还需要创建一个选项对象,将客户端传递进去,并指定您可能希望使用的任何其他参数。
以下是使用上一节中创建的客户端对象的一个示例
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), "suppress_rules" => true )); $options = array( // set the client for the request "client" => $rn_client, // Adds a custom header that adds an annotation (CCOM version must be set to "v1.4" or "latest"); limited to 40 characters "annotation" => "Custom annotation", // Prints request headers for debugging "debug" => true, // Adds a custom header to excludes null from results; for use with GET requests only "exclude_null" => true, // Number of milliseconds before another HTTP request can be made; this is an anti-DDoS measure "next_request" => 500, // Sets 'Accept' header to 'application/schema+json' "schema" => true, // Adds a custom header to return results using Coordinated Universal Time (UTC) format for time (Supported on November 2016+ "utc_time" => true );
HTTP方法
要使用各种HTTP方法返回原始响应对象,请使用“Connect”对象
POST
//// OSvCPHP\Connect::post(options) //// returns an object // Here's how you could create a new ServiceProduct object // using PHP variables and associative arrays (sort of like JSON) require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); // JSON object // containing data // for creating // a new product $new_product = array( 'names' => array( array( 'labelText' => 'NEW_PRODUCT', 'language' => array('id' => 1) ) ), 'displayOrder' => 4, 'adminVisibleInterfaces' => array( array( 'id' => 1 ) ), 'endUserVisibleInterfaces' => array( array( 'id' => 1 ) ), ); $options = array( "client" => $rn_client, "url" => "serviceProducts", "json" => $new_product, "debug" => true ); $post_response = OSvCPHP\Connect::post($options);
GET
//// OSvCPHP\Connect::get(options) //// returns an object // Here's how you could get an instance of ServiceProducts require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "url" => "serviceProducts/56", ); $post_response = OSvCPHP\Connect::get($options);
PATCH
//// OSvCPHP\Connect::patch(options) //// returns an object // Here's how you could update a Service Product object // using JSON objects // to set field information require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $updated_product = array( 'names' => array( array( 'labelText' => 'UPDATED_PRODUCT', 'language' => array('id' => 1) ) ), 'displayOrder' => 4, 'adminVisibleInterfaces' => array( array( 'id' => 1 ) ), 'endUserVisibleInterfaces' => array( array( 'id' => 1 ) ), ); $options = array( "client" => $rn_client, "url" => "serviceProducts/268", "json" => $updated_product, ); $patch_response = OSvCPHP\Connect::patch($options);
DELETE
//// OSvCPHP\Connect::delete(options) //// returns an object // Here's how you could delete a serviceProduct object require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "url" => "serviceProducts/56", ); $delete_response = OSvCPHP\Connect::delete($options);
OPTIONS
//// OSvCPHP\Connect::options(options) //// returns headers object or a raw Response object on error // Here's how you can fetch options for incidents require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "url" => "incidents", ); $options_response = OSvCPHP\Connect::options($options);
上传文件附件
为了上传文件附件,请将一个包含数组作为值的 "files" 属性添加到您的选项对象中。在该数组中,输入您希望上传的文件的路径,相对于脚本运行的位置。
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "url" => "incidents", "json" => array( "primaryContact"=> array( "id"=> 2 ), "subject"=> "FishPhone not working" ), "files" => array( "./test.php", ) ); $post_response = OSvCPHP\Connect::post($options);
下载文件附件
为了下载文件附件,请将 "?download" 查询参数添加到文件附件的 URL 中,并使用 OSvCPHP\Connect.get 方法发送 GET 请求。文件将被下载到脚本运行的位置。
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "url" => '/incidents/25872/fileAttachments/417?download' ); $get_response = OSvCPHP\Connect::get($options); // returns 1 on success
为了下载特定对象的多个附件,请将 "?download" 查询参数添加到文件附件的 URL 中,并使用 OSvCPHP\Connect.get 方法发送 GET 请求。
所有指定对象的文件将被下载并存档到一个 .tgz 文件中。
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "url" => '/incidents/25872/fileAttachments?download' ); $get_response = OSvCPHP\Connect::get($options); // returns 1 on success
您可以使用 tar 提取文件。
$ tar -xvzf ./downloadedAttachment.tgz
OSvCPHP\QueryResults 示例
这是用于运行一个 ROQL 查询。任何 REST API 允许的(限制和排序)都可以使用这个库。
OSvCPHP\QueryResults 只有一个函数:"query",它接受一个 OSvCPHP\Client 对象和字符串查询(示例见下文)。
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "query" => "DESCRIBE CONTACTS", "client" => $rn_client ); $q = new OSvCPHP\QueryResults; $results = $q->query($options);
OSvCPHP\QueryResultsSet 示例
这是用于运行多个查询并将每个查询的结果分配给键以进行进一步处理的。
OSvCPHP\QueryResultsSet 只有一个函数:"query_set",它接受一个 OSvCPHP\Client 对象和多个查询哈希(示例见下文)。
// Pass in each query into a hash // set query: to the query you want to execute // set key: to the value you want the results to of the query to be referenced to require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $queries = array( array( "query" => "DESCRIBE ANSWERS", "key" => "answerSchema" ), array( "query" => "SELECT * FROM ANSWERS LIMIT 1", "key" => "answers" ), array( "query" => "DESCRIBE SERVICECATEGORIES", "key" => "categoriesSchema" ), array( "query" => "SELECT * FROM SERVICECATEGORIES", "key" =>"categories" ), array( "query" => "DESCRIBE SERVICEPRODUCTS", "key" => "productsSchema" ), array( "query" => "SELECT * FROM SERVICEPRODUCTS", "key" =>"products" ) ); $options = array( "client" => $rn_client, "queries" => $queries ); $mq = new OSvCPHP\QueryResultsSet; $results = $mq->query_set($options); // Results for "DESCRIBE ANSWERS" // // [ // { // "Name": "id", // "Type": "Integer", // "Path": "" // }, // { // "Name": "lookupName", // "Type": "String", // "Path": "" // }, // { // "Name": "createdTime", // "Type": "String", // "Path": "" // } // ... everything else including customfields and objects... // ] // Results for "SELECT * FROM ANSWERS LIMIT 1" // // [ // { // "id": 1, // "lookupName": 1, // "createdTime": "2016-03-04T18:25:50Z", // "updatedTime": "2016-09-12T17:12:14Z", // "accessLevels": 1, // "adminLastAccessTime": "2016-03-04T18:25:50Z", // "answerType": 1, // "expiresDate": null, // "guidedAssistance": null, // "keywords": null, // "language": 1, // "lastAccessTime": "2016-03-04T18:25:50Z", // "lastNotificationTime": null, // "name": 1, // "nextNotificationTime": null, // "originalReferenceNumber": null, // "positionInList": 1, // "publishOnDate": null, // "question": null, // "solution": "<HTML SOLUTION WITH INLINE CSS>", // "summary": "SPRING IS ALMOST HERE!", // "updatedByAccount": 16, // "uRL": null // } // ] // Results for "DESCRIBE SERVICECATEGORIES" // // [ // ... skipping the first few ... // { // "Name": "adminVisibleInterfaces", // "Type": "SubTable", // "Path": "serviceCategories.adminVisibleInterfaces" // }, // { // "Name": "descriptions", // "Type": "SubTable", // "Path": "serviceCategories.descriptions" // }, // { // "Name": "displayOrder", // "Type": "Integer", // "Path": "" // }, // { // "Name": "endUserVisibleInterfaces", // "Type": "SubTable", // "Path": "serviceCategories.endUserVisibleInterfaces" // }, // ... everything else include parents and children ... // ] // Results for "SELECT * FROM SERVICECATEGORIES" // // [ // { // "id": 3, // "lookupName": "Manuals", // "createdTime": null, // "updatedTime": null, // "displayOrder": 3, // "name": "Manuals", // "parent": 60 // }, // { // "id": 4, // "lookupName": "Installations", // "createdTime": null, // "updatedTime": null, // "displayOrder": 4, // "name": "Installations", // "parent": 60 // }, // { // "id": 5, // "lookupName": "Downloads", // "createdTime": null, // "updatedTime": null, // "displayOrder": 2, // "name": "Downloads", // "parent": 60 // }, // ... you should get the idea by now ... // ]
OSvCPHP\AnalyticsReportsResults
您可以通过报告的 'id' 或 'lookupName' 创建一个新实例。
OSvCPHP\AnalyticsReportsResults 只有一个函数:"run",它接受一个 OSvCPHP\Client 对象。
在选项数据对象中传递 'id'、'lookupName' 和 'filters' 来设置报告和任何过滤器。
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "json" => array( "filters" => array( array( "name" => "search_ex", "values" => array("returns") ) ), "limit" => 2, "id" => 176 ) ); $arr = new OSvCPHP\AnalyticsReportResults; $arrResults = $arr->run($options);
批量删除
这个库使得使用最新版本 REST API 中的批量删除功能变得容易。
您可以使用 QueryResults 或 QueryResultsSet 对象来运行批量删除查询。
在您可以使用此功能之前,请确保您已为您的配置文件设置了 正确的权限。
以下是如何使用批量删除功能的示例
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), "version" => "latest" )); $options = array( "client" => $rn_client, "query" => "DELETE FROM INCIDENTS LIMIT 10", "annotation" => "Delete example" ); $q = new OSvCPHP\QueryResults; $results = $q->query($options);
并发运行多个ROQL查询
您不需要运行多个查询,而是可以通过添加一个 "concurrent" 属性到选项对象中,通过多个 GET 请求运行多个查询并将结果组合。
require __DIR__ . '/vendor/autoload.php'; $rn_client = new OSvCPHP\Client(array( "username" => getenv("OSVC_ADMIN"), "password" => getenv("OSVC_PASSWORD"), "interface" => getenv("OSVC_SITE"), )); $queries = array( array( "query" => "DESCRIBE INCIDENTS", "key" => "incidents" ), array( "query" => "DESCRIBE SERVICEPRODUCTS", "key" => "serviceProducts" ), ); $options = array( "client" => $rn_client, "queries" => $queries, "concurrent" => true ); $mq = new OSvCPHP\QueryResultsSet; $results = $mq->query_set($options);
执行会话身份验证
- 创建一个包含以下代码的自定义脚本,并将其放置在文件管理器中的 "自定义脚本" 文件夹
<?php // Find our position in the file tree if (!defined('DOCROOT')) { $docroot = get_cfg_var('doc_root'); define('DOCROOT', $docroot); } /************* Agent Authentication ***************/ // Set up and call the AgentAuthenticator require_once (DOCROOT . '/include/services/AgentAuthenticator.phph'); // get username and password $username = $_GET['username']; $password = $_GET['password']; // On failure, this includes the Access Denied page and then exits, // preventing the rest of the page from running. echo json_encode(AgentAuthenticator::authenticateCredentials($username,$password));
- 创建一个类似于以下内容的 PHP 脚本,它应该可以连接
require __DIR__ . '/vendor/autoload.php'; // initialize a curl request $ch = curl_init(); // set the base of the url $url = "https://". getenv('OSVC_SITE') .".custhelp.com/cgi-bin/" // add the location of the above file $url .= getenv('OSVC_CONFIG') .".cfg/php/custom/login_test.php" // add the credentials for getting a session ID $url .= "?username=". getenv('OSVC_ADMIN') ."&password=". getenv('OSVC_PASSWORD'); // set the URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // execute $output = curl_exec($ch); // close curl curl_close($ch); $session_id = json_decode($output)->session_id; $rn_client = new OSvCPHP\Client(array( "session" => $session_id, "interface" => getenv("OSVC_SITE"), )); $options = array( "client" => $rn_client, "query" => "SELECT * FROM INCIDENTS LIMIT 10" ); $mq = new OSvCPHP\QueryResults(); $results = $mq->query($options); echo json_encode($results, JSON_PRETTY_PRINT);