matthewpoer / php-docusign-wrapper
PHP用于RESTful DocuSign v2 API的包装器
Requires
- php: >5.6
- educoder/pest: dev-master
This package is auto-updated.
Last update: 2024-09-05 06:06:44 UTC
README
DocuSign提供了一种可靠且RESTful(尽管有些令人困惑的架构)的v2 API。然而,我发现官方PHP SDK并不容易直接使用。尝试过几个不同版本的PHP,也尝试过几个主机。存在命名空间和/或文件/类结构的问题。或者是什么。但是REST API足够简单,因此编写一个新的包装器比调试他们要少做很多工作。
此代码库将实现基本的身份验证、信封、收件人、表单和相关数据的交互。我的商业目标是组织通过此类表单返回的数据。超出此目标的方法可能不会实现,但如果您想增强并分享您的工作,请随时提交拉取请求。
使用Pest进行REST
Pest是RESTful Web服务的PHP客户端库。我发现它对PhpDocuSignWrapper是一个最小和有效的后端。Pest依赖关系由composer处理。
感谢@Educoder!
PHP 5.6和Vagrant以及DevBox
我选择支持5.6,因为它仍然是许多行业在生产环境中有效且广泛使用的选项。如果您运行的是最新的PHP 7,5.6代码仍然可以100%运行。
我发现Damian Lewis的DevBox项目是一个非常适合此类PHP开发的优秀开发环境。一个漂亮的Vagrantfile
可能包含
Vagrant.configure("2") do |config|
config.vm.box = "damianlewis/ubuntu-16.04-lamp"
config.vm.provision "shell",
inline: 'update-alternatives --set php "/usr/bin/php5.6" > /dev/null'
end
感谢,@Damian!
Composer安装
composer require matthewpoer/php-docusign-wrapper:dev-master
身份验证
虽然DocuSign API支持OAuth,但这里没有实现。相反,我们使用Legacy Header Authentication登录方法。对此表示歉意。
API密码
DocuSign确实提供了一种解决此处固有的明文密码问题的解决方案,那就是提供所谓的"apiPassword",
一个可以用于API调用中的身份验证而不是使用用户名和密码的令牌
好吧,太好了。这里包含一个Bash脚本来从DocuSign获取此API密码,就像这样
$ ./GetApiPassword.sh
Host? enter a subdomain, i.e. 'demo' or 'www'
www
Username:
user@host.tld
Password:
Integrator Key:
my-integrator-key
requesting API Password...
API Password Found:
"apiPassword": "Your Cool API Password"
示例代码
<?php
// maybe you define your connection info. in a config file?
require_once('config.php');
try {
// invocation and auth.
echo "Accessing DocuSign..." . PHP_EOL;
$ds = new PhpDocuSignWrapper(
DOCUSIGN_HOST,
DOCUSIGN_EMAIL,
DOCUSIGN_PASSWORD,
DOCUSIGN_INTEGRATOR_KEY,
DOCUSIGN_ACCOUNT_ID
);
// list all users
echo "Preparing All User Retrieval..." . PHP_EOL;
$users = $ds->get_users();
$user_count = count($users);
echo "Found {$user_count} Users:" . PHP_EOL;
foreach($users as $userId => $userName) {
echo "\t{$userName}" . PHP_EOL;
}
// list only the active users, also show their group associations
echo "Preparing Active-Only User Retrieval..." . PHP_EOL;
$users = $ds->get_users(TRUE);
$user_count = count($users);
echo "Found {$user_count} Users:" . PHP_EOL;
foreach($users as $userId => $userName) {
echo "\t{$userName} has the following groups:" . PHP_EOL;
foreach($ds->get_user_groups($userId) as $group_name) {
echo "\t\t-- {$group_name}" . PHP_EOL;
}
}
// list all seen folders and their envelopes
echo "Preparing folder list..." . PHP_EOL;
$folders = $ds->get_folders();
$folders_count = count($folders);
echo "Found {$folders_count} Folders:" . PHP_EOL;
foreach($folders as $folderId => $folderName) {
echo "Contents of $folderName:" . PHP_EOL;
$contents = $ds->get_folder_contents($folderId);
if(empty($contents)) {
echo "\t-- folder is empty --" . PHP_EOL;
}
foreach($contents as $envelopeId => $envelopeName) {
echo "\t{$envelopeName}" . PHP_EOL;
}
}
} catch(\Exception $e) {
$message = $e->getMessage();
echo 'Error working with DocuSign. Exception: '
. PHP_EOL
. $message
. PHP_EOL;
}
关于文件夹的说明
默认情况下,DocuSign Folders:list API端点只会返回“常规”(信封)文件夹列表,例如收件箱、已发送项、已删除项等。要获取“模板”文件夹的列表,我们指定一个GET参数template
。文档告诉我们template
...
指定返回的项目。有效值有
- include - 文件夹列表将返回常规文件夹加模板文件夹。
- only - 仅返回模板文件夹列表。
我发现 only
的值并不符合这些说明,我无法确定 only
给我的文件夹的具体位置。要真正获取模板文件夹,我建议使用 include
获取所有正常和模板文件夹,然后删除任何没有指定 template
参数的文件夹(即信封模板)。
以下示例代码将基于文件夹名称找到文件夹中存放的一组模板
$desired_folder_name = 'My Cool Folder Name';
$desired_folder_guid = NULL;
$all_folders = $ds->get_folders('include');
foreach($all_folders as $folder_id => $folder_name) {
if($folder_name == $desired_folder_name) {
$desired_folder_guid = $folder_id;
}
}
$approved_templates = $templates = $ds->get_templates_in_folder($desired_folder_guid);