quentn / php-sdk
Quentn Api 的官方 PHP SDK
Requires
- php: >=7.1
- guzzlehttp/guzzle: 6 - 7
README
官方 Quentn PHP API 客户端
这是一个为 Quentn API 提供简单接口的官方 PHP 库。它易于使用,并得到 Quentn.com GmbH 的全面支持。
安装
您需要安装 Composer 来管理依赖项。
运行以下 Composer 命令以安装最新稳定的 Quentn PHP SDK 版本。
composer require quentn/php-sdk
示例
我们需要在每个文件的顶部添加 autoload.php
require __DIR__ . './vendor/autoload.php';
响应包括三个主要元素,数据、状态和速率限制
数据:数据取决于您的请求,可以是联系信息,如姓名、电子邮件等,也可以是请求成功状态,即 true/false。
状态:HTTP 状态码
速率限制:Web API 中所有调用在刷新周期内分配了特定的请求数量。
每个 API 响应都包含限制数字、剩余次数和重置时间。
联系 API 示例
require __DIR__ . './vendor/autoload.php';
$quentn = new Quentn\Quentn([
'api_key' => 'API_KEY',
'base_url' => 'BASE_URL',
]);
//add additional headers
$headers = [
'X-sender-source' => 'mybusinessid',
'X-sender-source-key' => 'cdTHikGOQQuiIqo2VcCmkyIqIPq82oUm7juC9wqnxY',
];
$quentn->setHeaders($headers);
if (!$quentn->test()) {
echo "key doesn't seem to work";
exit;
}
//create contact
$data = [
"first_name" => "Johnn",
"family_name" => "Doe",
"mail" => "johndoe@example.com",
];
try {
$get_response = $quentn->contacts()->createContact($data);
//get id of newly created contact
$contact_id = $get_response['data']['id'];
} catch (Exception $e) {
echo $e->getMessage();
}
// Get all terms of a contact
try {
$get_response = $quentn->contacts()->getContactTerms($contactId);
$terms = $get_response['data'];
foreach ($terms as $term) {
echo $term['name']."\n";
}
} catch (Exception $e) {
echo $e->getMessage();
}
通过联系 API,您可以执行以下功能
通过 ID 获取联系人
用户可以通过 ID 查找联系人
findContactById((int) $contactId, (string)$fields = NULL);
通过邮件获取联系人
用户可以通过电子邮件查找联系人
findContactByMail((string) $mail, (string)$fields = NULL);
创建联系人
用户可以创建联系人
createContact((array)$data);
创建多个联系人
用户可以在一次调用中创建多个联系人
createContacts((array)$data);
更新联系人
用户可以更新联系人
updateContact((int)$contactId, (array)$data);
删除联系人
用户可以删除联系人
deleteContact((int)$contactId);
获取联系人条款
用户可以获取联系人的所有条款
getContactTerms((int)$contactId)
设置联系人条款 (已弃用)
用户可以 覆盖 所有联系人条款。通过使用此 POST 方法,您将覆盖整个条款字段。
注意:这将删除您现有的所有条款。如果您想添加条款,请使用 addContactTerms。
setContactTerms((int)$contactId, (array)$terms);
添加联系人条款
用户可以向联系人添加条款
addContactTerms((int) $id, (array)$terms);
删除联系人条款
用户可以删除联系人的条款
deleteContactTerms((int) $id, (array)$terms);
点击此处 查看联系 API 的完整使用示例
条款 API 示例
require __DIR__ . './vendor/autoload.php';
$quentn = new Quentn\Quentn([
'api_key' => 'API_KEY',
'base_url' => 'BASE_URL',
]);
/*
* TEST API CREDENTIALS.
*/
if (!$quentn->test()) {
echo "key doesn't seem to work";
exit;
}
/*
* get list of all terms
*/
try {
$get_response = $quentn->terms()->getTerms();
$terms = $get_response['data'];
foreach ($terms as $term) {
echo $term['name']."\n";
}
} catch (Exception $e) {
echo $e->getMessage();
}
//get term by id
try {
$get_response = $quentn->terms()->findTermById($termId);
echo $get_response['data']['name']."\n";
echo $get_response['data']['description'];
} catch (Exception $e) {
echo $e->getMessage();
}
通过条款 API,您可以执行以下功能
获取条款
用户可以查找所有条款的列表
getTerms((int)$offset = 0, (int)$limit = 500);
通过 ID 获取条款
用户可以通过 ID 查找条款
findTermById((int)$termId);
通过名称获取条款
用户可以通过名称查找条款
findTermByName((string)$termName);
创建条款
用户可以创建条款
createTerm((array)$data);
更新条款
用户可以更新条款
updateTerm((int)$id, (array)$data);
删除条款
用户可以删除条款
deleteTerm((int)$termId);
点击此处 查看条款 API 的完整使用示例
自定义字段 API 示例
require __DIR__ . './vendor/autoload.php';
$quentn = new Quentn\Quentn([
'api_key' => 'API_KEY',
'base_url' => 'BASE_URL',
]);
/*
* TEST API CREDENTIALS.
*/
if (!$quentn->test()) {
echo "key doesn't seem to work";
exit;
}
/*
* get list of all custom fields
*/
try {
$get_response = $quentn->custom_fields()->getCustomFields();
$custom_fields = $get_response['data'];
foreach ($custom_fields as $custom_field) {
echo $custom_field['field_name']."\n";
}
} catch (Exception $e) {
echo $e->getMessage();
}
//find custom field by ID
try {
$get_response = $quentn->custom_fields()->findCustomFieldById($customFieldId);
echo $get_response['data']['field_name']."\n";
echo $get_response['data']['label']."\n";
echo $get_response['data']['description']."\n";
echo $get_response['data']['type']."\n";
echo $get_response['data']['required']."\n";
} catch (Exception $e) {
echo $e->getMessage();
}
通过联系字段 API,您可以执行以下功能
获取自定义字段
用户可以查找所有自定义字段的列表
getCustomFields();
通过 ID 获取自定义字段
用户可以通过 ID 查找自定义字段
findCustomFieldById((int)$customFieldId);
通过名称获取自定义字段
用户可以通过名称查找自定义字段
findCustomFieldByName((string)$cutomFieldName);
创建自定义字段
用户可以创建自定义字段
createCustomField((array)$data);
更新自定义字段
用户可以更新自定义字段
updateCustomField((string)$cutomFieldName, (array)$data);
删除自定义字段
用户可以删除自定义字段
deleteCustomField((string)$cutomFieldName);
点击此处 查看自定义字段 API 的完整使用示例
OAuth
开始您的OAuth流程,您需要将您的应用注册到Quentn。注册后,您将获得客户端ID和客户端密钥。
设置OAuth配置
一旦您获得了客户端ID和客户端密钥,您需要使用以下变量调用setApp函数
client_id: 当您在Quentn创建应用时获得的客户端ID
client_secret: 当您在Quentn创建应用时获得的客户端密钥
redirect_url: 指示授权后返回用户的URI。域名必须是您已经在Quentn上注册的域名之一。例如,如果您注册了example.com,则可以使用example.com/my/redirect/url
setApp([
'client_id' => 'CLIENT_ID',
'client_secret' => 'CLIENT_SECRET',
'redirect_uri' => 'REDIRECT_URL',
]);
获取授权URL
要获取用户授权链接,您需要调用getAuthorizationUrl()
getAuthorizationUrl();
作为回报,您将获得授权URL,即
https://my.quentn.com/public/api/v1/oauth/?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=code&scope=all&state=4a4c2ZD
client_id: 如上所述,您在Quentn创建应用时获得的客户端ID
redirect_uri: 如上所述,它指示授权后返回用户的URI。域名必须是您已经在Quentn上注册的域名之一。例如,如果您注册了example.com,则可以使用example.com/my/redirect/url
scope: 它指示您想要访问用户账户的哪些部分,默认为'全部'
response_type: 它指示您的服务器期望接收授权码,默认为code
state: 由您的应用程序生成的一个随机字符串,稍后将用于验证
检查用户是否成功授权
要检查用户是否成功授权,您可以使用以下函数。
authorize()
OAuth示例
require __DIR__ . './vendor/autoload.php';
$quentn = new Quentn\Quentn();
$quentn->oauth()->setApp([
'client_id' => 'CLIENT_ID',
'client_secret' => 'CLIENT_SECRET',
'redirect_uri' => 'REDIRECT_URL',
]);
if($quentn->oauth()->authorize()) {
/*
do you stuff here
You can access your App key and base url here
echo $quentn->getApiKey()."\n";
echo $quentn->getBaseUrl()."\n";
*/
try {
$get_response = $quentn->contacts()->findContactById($contactId, 'first_name, mail');
} catch (Exception $e) {
echo $e->getMessage();
}
}
else {
//to get the Authorization URL you can use getAuthorizationUrl() function
echo '<a href="' . $quentn->oauth()->getAuthorizationUrl() . '">Click here to get authorize</a>';
}