weebly / cloud-client

本包最新版本(1.0.2)没有提供许可证信息。

Weebly 云 API 库

1.0.2 2017-04-21 19:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:44 UTC


README

安装

###Composer

composer require weebly/cloud-client

###其他下载最新版本,并在您的代码中包含init.php。

require_once("path/to/cloud-client-php/src/init.php");

设置和认证

API 调用通过公钥以及您请求和密钥的哈希进行认证。您可以在 Weebly 云管理员设置的 API 密钥部分创建和管理您的 API 密钥。

WeeblyCloud\CloudClient::setKeys(YOUR_API_KEY, YOUR_API_SECRET);

您必须在调用 API 之前设置您的公钥和密钥。

示例

典型用例:创建用户和站点,获取登录链接

// Get the admin account
try {
	$account = new WeeblyCloud\Account();
} catch (WeeblyCloud\Utils\CloudException $e) {
	print("Unable to get account.\n");
	print("Error code {$e->getCode()}: {$e->getMessage()}.\n");
	exit();
}

//Create a new user
try {
	$user = $account->createUser("test@domain.com");
} catch (WeeblyCloud\Utils\CloudException $e) {
	print("Unable to create user.\n");
	print("Error code {$e->getCode()}: {$e->getMessage()}.\n");
	exit();
}

//Store the user's ID
$user_id = $user->getProperty("user_id");

//Create a site
try {
	$site = $user->createSite("domain.com", ["site_title"=>"My Website"]);
} catch (WeeblyCloud\Utils\CloudException $e) {
	print("Unable to create site.\n");
	print("Error code {$e->getCode()}: {$e->getMessage()}.\n");
	exit();
}

//Store the site's ID
$site_id = $site->getProperty("site_id");

//Get and print a login link
try {
	print($site->loginLink());
} catch (WeeblyCloud\Utils\CloudException $e) {
	print("Unable to generate login link.\n");
	print("Error code {$e->getCode()}: {$e->getMessage()}.\n");
	exit();
}

打印匹配查询 "帮助" 的站点中所有页面的名称

$pages = $site->listPages(["query"=>"help"]);
while ($page = $pages->next()) {
	print($page->getProperty("title")."\n");
}

foreach ($site->listPages(["query"=>"help"]) as $page) {
	print($page->getProperty("title")."\n");
}

错误

如果请求失败,CloudClient 将抛出 CloudException。错误代码列表可以在 API 文档 中找到。异常可以按以下方式捕获

# Create client
$client = WeeblyCloud\Utils\CloudClient::getClient();

try {
	$account = new WeeblyCloud\Account();
} catch (WeeblyCloud\Utils\CloudException $e) {
	print($e);
}

资源

该库提供代表 API 资源的类。这些资源可以是 可变的,这意味着它们的属性可以更改,并且是 可删除的

常见方法

  • $resource->getProperty($property) 方法将返回资源的给定属性。如果属性不存在,它将返回 null
  • $resource->setProperty($property, $value) 方法将设置资源的给定属性的值。更改将在调用 $resource->save() 之前保存到数据库中。如果资源不可变,调用此方法将抛出异常。不是可变资源的每个属性都可以更改;有关更多信息,请参阅有关特定资源的 云 API 文档 中的 PUT 方法。
  • $resource->save() 方法将 setProperty() 修改的属性保存到数据库中。如果资源不可变,调用此方法将抛出异常。
  • $resource->delete() 方法将从数据库中删除资源。如果资源不可删除,调用此方法将抛出异常。

资源实例化

例如,要创建一个表示 ID 为 $site_id、由 $user_id 拥有的站点的对象

$site = new WeeblyCloud\Site($user_id, $site_id);

所有资源构造函数都有两个可选参数,initialize(默认为 true)和 existing。如果 initialize 设置为 false,则在对象实例化时不会从数据库中检索对象的属性。相反,使用 existing 设置对象的属性。这可以在链式调用时减少不必要的 API 调用。

示例

检索用户的全部站点,而不获取该用户的信息

$sites = (new WeeblyCloud\User($user_id, false))->listSites();

检索站点商店的信息

$store = (new WeeblyCloud\Site($user_id, $site_id))->getStore();

检索站点商店中的产品列表

$site = new WeeblyCloud\Site($_ENV['USER_ID'], $_ENV['SITE_ID'], true);
$store = $site->getStore;
$products = $site->listProducts();
// Get quantity of products in the store
$numberOfProductsInStore = $site->getProductCount();
while ($product = $products->next()) {
	print($product->getProperty("name")."\n");
}

可迭代结果

list 开头的方法返回一个 CloudList。使用 next 函数或 foreach 循环来遍历列表。例如

$sites = (new WeeblyCloud\User($user_id))->listSites();

while ($site = $sites->next()) {
	print($site->getProperty("site_title")."\n");
}

这将列出属于给定用户的所有站点的标题。

##资源类型

除了本说明文档外,每个资源类都有 phpdoc 文档。要查看该文档,请在 cloud-client-php 目录中安装 phpDocumentor 并运行以下命令

phpdoc -d ./src/WeeblyCloud
open output/namespaces/WeeblyCloud.html

账户

API 文档

A 可变的 Cloud Admin 账户表示,由您的 API 密钥指定。

$account = new WeeblyCloud\Account();
  • createUser($email, $data = []) 在数据库中创建新用户。需要用户的 邮箱,并且可以可选地接受一个包含附加属性的关联数组 data。返回新创建的用户。
  • listPlans() 获取可用计划列表。
  • getPlan($plan_id) 通过ID获取单个计划。

博客

API 文档

博客的表示。为了构建

$blog = new WeeblyCloud\Blog($user_id, $site_id, $blog_id);
  • listBlogPosts() 返回该博客上的博客帖子列表。
  • getBlogPost($post_id) 返回具有给定ID的 BlogPost
  • makeBlogPost($post_body, data = [] ) 在博客上创建新的博客帖子。需要帖子的 post_body 和一个可选的包含附加参数的关联数组。返回一个 BlogPost 资源。

博客帖子

API 文档

博客帖子的可变和可删除表示。为了构建

$blog_post = new WeeblyCloud\BlogPost($user_id, $site_id, $blog_id, $post_id);

没有 BlogPost 特定方法。

表单

API 文档

表单的表示。为了构建

$form = new WeeblyCloud\Form($user_id, $site_id, $form_id);
  • listFormEntries($search_params = []) 返回给定表单的 FormEntry 资源列表,受可选搜索参数的限制。
  • getFormEntry($entry_id) 返回具有给定ID的 FormEntry

表单条目

API 文档

表单条目的表示。为了构建

$form_entry = new WeeblyCloud\FormEntry($user_id, $site_id, $form_id, $entry_id);

没有 FormEntry 特定方法。

API 文档

组的一个可变和可删除表示。为了构建

$group = new WeeblyCloud\Group($user_id, $site_id, $group_id);

页面

API 文档

页面的一个可变表示。为了构建

$page = new WeeblyCloud\Page($user_id, $site_id, $page_id);

没有 Page 特定方法。

计划

API 文档

计划的一个表示。为了构建

$plan = new WeeblyCloud\Plan($plan_id);

没有 Plan 特定方法。

成员

API 文档

成员的一个可变和可删除表示。为了构建

$member = new WeeblyCloud\Member($user_id, $site_id, $member_id);

产品

API 文档

站点的可变和可删除表示。为了构建

为了构建

$product = new WeeblyCloud\Product($user_id, $site_id, $product_id);
  • publish() 发布站点。
  • unpublish() 取消发布站点。

站点

API 文档

站点的可变和可删除表示。为了构建

为了构建

$site = new WeeblyCloud\Site($user_id, $site_id);
  • publish() 发布站点。
  • unpublish() 取消发布站点。
  • loginLink() 生成一个一次性登录链接,将用户重定向到该站点的编辑器。如果用于实例化站点对象的用户ID被禁用,将抛出异常。
  • setPublishCredentials($data) 为给定站点设置发布凭据。如果用户的站点不由Weebly托管,则可以提供发布凭据。$data 的必需属性是 publish_host、publish_username、publish_password 和 publish_path。
  • restore($domain) 当站点被恢复时,站点的所有者可以以删除时精确的状态访问它,包括分配的Weebly计划。恢复站点不会自动发布。
  • disable() 禁用站点,阻止用户通过编辑器访问它。
  • enable() 启用站点,允许对其进行编辑。站点在创建时默认启用。
  • listPages($search_params = []) 返回该 Site 上的 Pages 列表,受搜索参数的限制。
  • listMembers($search_params = []) 返回该 Site 上的 Members 列表,受搜索参数的限制。
  • listGroups($search_params = []) 返回该 Site 上的 Members 列表,受搜索参数的限制。
  • listForms() 返回该 Site 上的 Forms 列表。
  • listBlogs() 返回该 Site 上的 Blogs 列表。
  • getPage($page_id) 返回具有给定ID的 Page
  • getMember($member_id) 返回具有给定ID的 Member
  • getGroup($group_id) 返回具有给定ID的 Group
  • getForm($form_id) 返回具有给定ID的 Form
  • getBlog($blog_id) 返回具有给定ID的 Blog
  • getStore() 返回网站的 Store 资源。
  • getPlan() 返回网站的 Plan 资源。
  • setPlan($plan_id, $term = 1) 为网站分配一个计划,可选的期限长度。
  • setTheme($theme_id, $is_custom) 通过ID分配一个主题给网站。需要一个参数 is_custom,用于区分主题是Weebly主题还是自定义主题。
  • createMember($data) 在数据库中创建一个新的 Member。返回新创建的 Member
  • createGroup($name) 在数据库中创建一个新的 Group,其中包含网站的成员。返回新创建的 Group
  • listProducts($search_params) 根据搜索参数检索商店的产品列表。
  • getProductCount() 返回商店中的产品数量。
  • createProduct($product_name, $product_skus, $data = []) 在数据库中的 Store 中创建一个新的 Product。返回新创建的 Product
  • getProduct($product_id) 通过ID从 Store 中检索一个特定的 Product

Store

API 文档

网站的 可变 表示。要构造

为了构建

$store = new WeeblyCloud\Store($user_id, $site_id);
  • updateStore($values) 使用提供的值更新商店。

User

API 文档

一个代表WeeblyCloud用户的 可变 表示。要构造

$user = new WeeblyCloud\User($user_id);
  • enable() 在禁用账户后启用用户账户。启用用户账户将允许用户登录并编辑他们的网站。当创建用户时,他们的账户将自动启用。disable() 禁用用户账户,阻止他们登录或编辑他们的网站。
  • loginLink() 生成一个一次性登录链接。如果用户已被禁用,将返回错误。
  • getAvailableThemes($search_params = []) 返回一个数组,其中包含此用户可用的主题,受可选搜索参数的限制。数组中的主题不是资源对象,而是直接从响应JSON中构造的。有关有效的搜索参数,请参阅API文档。
  • listSites($search_params = []) 返回此用户所属的CloudList网站,受可选搜索参数的限制。getSite($site_id) 返回具有给定ID的 Site
  • createCustomTheme($name, $zip_url) 向用户添加一个自定义主题。需要主题的名称和公开可访问的.zip文件的URL。
  • createSite($domain, $data = []) 在数据库中创建一个新的属于此用户 Site。需要网站的 domain 和可选的属性关联数组 data。返回新创建的 Site

执行原始API调用

并非每个资源都有一个对应的资源类。可以使用 CloudClient 对象执行原始API调用。

$client = WeeblyCloud\Utils\CloudClient::getClient();

使用该客户端,调用 getpostputpatchdelete。所有客户端请求方法都接受一个url作为第一个参数。postpatchput 可选接受一个包含将发送到请求体的数据的数据哈希表。get 可选接受一个哈希表,其值将用于请求的查询字符串。

url 必须 不包含前导斜杠。

请求示例

获取云管理员账户
# Get client
$client = WeeblyCloud\Utils\CloudClient::getClient();

# Request the /account endpoint
$client->get("account");
更新页面标题
# Get client
$client = WeeblyCloud\Utils\CloudClient::getClient();

# Build endpoint with IDs
$endpoint = "user/{$user_id}/site/{$site_id}/page/{$page_id}";

# Make the request
$client->patch($endpoint, ["title"=>"New Title"]);
获取用户的全部网站(带有搜索参数)
# Get client
$client = WeeblyCloud\Utils\CloudClient::getClient();

# Build endpoint with IDs
$endpoint = "user/{$user_id}/site";

# Make the request (get all sites this user owns)
$client->get($endpoint, ["role"=>"owner"]);

处理响应

所有请求都返回一个 CloudResponse 对象或抛出 CloudException。请求返回的JSON可以通过响应的 body 属性访问。

# Make a request
$response = $client->get("account");

# Print JSON body of response
print($response->body);

### 分页示例 如果端点支持分页,可以使用 getPreviousPage()getNextPage() 方法检索结果的下一页和前一页。如果没有下一页或前一页,则这些方法返回null。

# Create client
$client = WeeblyCloud\Utils\CloudClient::getClient();
$response = $client->get("user/{$user_id}/site",["limit"=>10]);

while($response){
	print($response->body . "\n");
	$response =  $response->getNextPage();
}

获取用户所有站点,每页10个站点。