matthewpoer / php-cvent-wrapper
PHP 对一些常见的 Cvent SOAP API 函数的包装
Requires
- php: >5.6
- ext-soap: *
This package is auto-updated.
Last update: 2024-09-25 07:25:40 UTC
README
Cvent SOAP API 提供读取事件数据以及针对许多以人命名的对象执行 CRUD 操作的能力。此包装器应简化系统只读访问的登录、搜索和检索操作。
Composer 安装
composer require matthewpoer/php-cvent-wrapper:dev-master
沙盒或生产环境?
默认情况下,此包装器将访问生产环境的 Cvent API,但在调用类时设置 $sandbox
参数将使您的调用发送到沙盒 API,例如:
// Production
$wrapper = new php_cvent_wrapper();
// Sandbox
$wrapper = new php_cvent_wrapper(TRUE);
身份验证说明
访问 Cvent API(无论是沙盒还是生产账户),都需要 API 账号的账号号、用户名和密码,这些在 Cvent 中与典型用户账户的处理方式不同。还必须将 Cvent 配置为将传入 API 请求的 IP 地址列入白名单,作为额外的安全措施。请参阅 Cvent 关于 登录基本步骤 的说明。
鸣谢
此代码的部分灵感来自 php-cvent。
示例代码
授权
<?php
require_once('config.php');
require_once('vendor/autoload.php');
// Authorization
$php_cvent_wrapper = new php_cvent_wrapper(TRUE);
try {
$result = $php_cvent_wrapper->login(
CVENT_ACCOUNT_NUMBER,
CVENT_USERNAME,
CVENT_PASSWORD
);
if(!$result) {
die('Cvent authentication failed for an unknown reason' . PHP_EOL);
}
} catch (\CventAuthorizationFailureException | \CventAuthorizationLockoutException $e) {
echo 'Cvent Auth Error: ' . $e->getMessage();
die();
} catch (\Exception $e) {
echo 'Failed to authenticate with Cvent' . PHP_EOL;
echo $e->getMessage();
die();
}
echo "Authentication was successful." . PHP_EOL;
获取对象的字段列表
try {
$fields = $php_cvent_wrapper->describe_object_fields('Registration');
} catch (\Exception $e) {
echo 'Failed to get fields list' . PHP_EOL;
echo $e->getMessage();
die();
}
过滤搜索
try {
$users = $php_cvent_wrapper->search(
'User',
array(
(object)array(
'Field' => 'UserRole',
'Operator' => 'Equals',
'Value' => 'Administrators',
)
)
);
echo "Here's a list of all of the User IDs for Administrators:" . PHP_EOL;
foreach ($users as $user_id) {
echo 'User ID is ' . $user_id . PHP_EOL;
}
} catch (\Exception $e) {
echo 'Failed to search for a list of Administrators' . PHP_EOL;
echo $e->getMessage();
die();
}
无过滤条件搜索
相同的搜索方法也可以不使用过滤条件,例如获取所有已知演讲者的列表。
try {
$speakers = $php_cvent_wrapper->search('Speaker');
echo "Here's a list of all of the Speaker IDs:" . PHP_EOL;
foreach ($speakers as $speaker_id) {
echo 'Speaker ID is ' . $speaker_id . PHP_EOL;
}
} catch (\Exception $e) {
echo 'Failed to search for a list of Speakers' . PHP_EOL;
echo $e->getMessage();
die();
}
检索
try {
$user_data = $php_cvent_wrapper->retrieve(
'User',
$users,
array(
'Email',
'Id',
'UserType',
'UserRole',
)
);
foreach($user_data as $user_info) {
echo 'The email address for user ' . $user_info['Id'] . ' is ' . $user_info['Email'] . PHP_EOL;
}
} catch (\Exception $e) {
echo 'Failed to retrieve user data for our list of Administrators' . PHP_EOL;
echo $e->getMessage();
die();
}
单次调用中的搜索和检索
try {
$events = $php_cvent_wrapper->search_and_retrieve(
'Event',
array(
(object)array(
'Field' => 'EventStartDate',
'Operator' => 'Greater than',
'Value' => date('Y-m-d\TH:m:s'),
)
),
array(
'EventCode',
'EventStartDate',
'EventTitle',
'Id',
)
);
foreach($events as $event) {
echo 'Event ' . $event['EventTitle'] . ' will begin on ' . $event['EventStartDate'] . PHP_EOL;
}
} catch (\Exception $e) {
echo 'Failed to search and retrieve info. for future events' . PHP_EOL;
echo $e->getMessage();
die();
}
使用包含(即多个值)进行搜索
以下示例将找到一周内已修改的活跃和已完成活动的列表。请注意,过滤器使用 ValueArray
而不是仅使用 Value
。
try {
$events = $php_cvent_wrapper->search_and_retrieve(
'Event',
array(
(object)array(
'Field' => 'LastModifiedDate',
'Operator' => 'Greater than',
'Value' => date('Y-m-d\TH:m:s', strtotime('-1 week')),
),
(object)array(
'Field' => 'EventStatus',
'Operator' => 'Includes',
'ValueArray' => array(
'Active',
'Completed',
)
),
),
array(
'EventCode',
'EventStartDate',
'EventTitle',
'Id',
)
);
foreach($events as $event) {
echo 'Event ' . $event['EventTitle'] . ' will begin on ' . $event['EventStartDate'] . PHP_EOL;
}
} catch (\Exception $e) {
echo 'Failed to search and retrieve info. for recently modified active and completed events' . PHP_EOL;
echo $e->getMessage();
die();
}
注册问题
注册问题存储在注册对象内部的对象中,因此这些可以“展开”为单个字段和/或转换为数组以进行更精细的处理。请注意,在 search_and_retrieve()
中的第 5 个参数设置了 search()
中的第 4 个参数,即 $always_flat
标志。它默认为 TRUE(假设大多数情况下您不会在平坦的记录数据中想要数组),但当设置为 FALSE 时,注册数据将作为数组返回。
// `$always_flat` is the 4th param in retrieve
$registrations = $php_cvent_wrapper->retrieve(
'Registration',
$Ids,
$Fields,
FALSE
);
// `$always_flat` is the 5th param in search_and_retrieve
$registrations = $php_cvent_wrapper->search_and_retrieve(
'Registration',
$Filters,
$Fields,
'AndSearch'
FALSE
);
返回的数据将包括文本形式的答案,无论 $always_flat
是否为真或假,因此像下面那样转储 $registration['Answer']
将给出以下输出:
$registration = current(registrations);
var_dump($registration['Answer']);
/*
string(204) "Question:
Here is an example event question (001)
Response:
Here is a response to the question (001)
Question:
Here is an another example event question (002)
Response:
And here is another response (002)"
*/
但若将 $always_flat
设置为 FALSE,您可以转储一个额外的数组,这有助于更容易地区分问题。
$registration = current(registrations);
var_dump($registration['Answer Array']);
/*
array(2) {
["Here is an example event question (001)"]=>
string(40) "Here is a response to the question (001)"
["Here is an another example event question (002)"]=>
string(47) "Here is an another example event question (002)"
}
*/