承接 weebly/cloud-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

weebly/cloud-client

Composer 安装命令:

composer require weebly/cloud-client

包简介

Weebly Cloud API library

README 文档

README

Installation

###Composer

composer require weebly/cloud-client

###Other Download the latest release and include init.php in your code.

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

Setup and Authentication

API calls are authenticated through a public key and a hash of your request and secret key. You can create and manage your API keys in the settings section of the Weebly Cloud Admin.

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

You must set your public and secret key before making any calls to the API.

Examples

Typical use case: create a user and site, get a login link

// 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();
}

Printing the name of all pages in a site matching the query "help"

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

or

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

Errors

If a request fails, the CloudClient throws a CloudException. A list of error codes can be found in the API documentation. The exception can be caught as folllows:

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

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

Resources

The library provides classes that represent API resources. These resources can be mutable, meaning their properties can be changed, and deletable.

Common methods:

  • The method $resource->getProperty($property) will return a given property of the resource. If the property does not exist, it will return null.
  • The method $resource->setProperty($property, $value) will set the value of a given property of the resource. Changes will not be saved in the database until $resource->save() is called. If the resource is not mutable, calling this method will throw an exception. Not every property of a mutable resource can be changed; for more information, reference the Cloud API Documentation for the resource in question's PUT method.
  • The method $resource->save() saves the properties changed by setProperty() to the database. If the resource is not mutable, calling this method will throw an exception.
  • The method $resource->delete() deletes the resource from the database. If the resource is not deletable, calling this method will throw an exception.

Instantiating Resources

For example, to create an object representing a site with id $site_id and owned by $user_id:

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

All resource constructors have two optional parameters, initialize (true by default) and existing. If initialize is set to false, the properties of the object are not retrieved from the database when the object is instantiated. Instead, existing is used to set the object's properties. This can be used to reduce unecessary API calls when chaining calls.

Examples

Retrieve all the sites of a user without getting that user's information:

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

Retrieve information about a site's store:

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

Retrieve a list of products in the site's store:

$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");
}

Iterable Results

Methods beginning with list return a CloudList. Use the next function or a foreach loop to iterate through the list. For example:

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

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

This would list the titles of all sites belonging to a given user.

##Resource Types

In addition to this readme, each resource class has phpdoc documentation. To view that documentation, install phpDocumentor and run these commands in the cloud-client-php directory:

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

Account

API Documentation

A mutable representation of your Cloud Admin account, specified by your API keys. To construct:

$account = new WeeblyCloud\Account();
  • createUser($email, $data = []) Creates a new user in the database. Requires the user's email, and optionally takes an associative array, data, of additional properties. Returns the newly created User.
  • listPlans() Gets a CloudList of available plans.
  • getPlan($plan_id) Get a single plan by ID.

Blog

API Documentation

A respresentation of a blog. To construct:

$blog = new WeeblyCloud\Blog($user_id, $site_id, $blog_id);
  • listBlogPosts() Returns a CloudList of BlogPosts on this Blog.
  • getBlogPost($post_id) Returns the BlogPost with the given id.
  • makeBlogPost($post_body, data = [] ) Creates a new BlogPost on the blog. Requires the post's post_body and an optional associative array of additional parameters. Returns a BlogPost resource.

BlogPost

API Documentation

A mutable and deletable respresentation of a blog post. To construct:

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

There are no BlogPost specific methods.

Form

API Documentation

A respresentation of a form. To construct:

$form = new WeeblyCloud\Form($user_id, $site_id, $form_id);
  • listFormEntries($search_params = []) Returns a CloudList of FormEntry resources for a given form subject to the optional search parameters.
  • getFormEntry($entry_id) Return the FormEntry with the given id.

FormEntry

API Documentation

A respresentation of a form entry. To construct:

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

There are no FormEntry specific methods.

Group

API Documentation

A mutable and deletable respresentation of a group. To construct:

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

Page

API Documentation

A mutable respresentation of a page. To construct:

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

There are no Page specific methods.

Plan

API Documentation

A respresentation of a plan. To construct:

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

There are no Plan specific methods.

Member

API Documentation

A mutable and deletable respresentation of a member. To construct:

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

Product

API Documentation

A mutable and deletable respresentation of a site. To construct:

To construct:

$product = new WeeblyCloud\Product($user_id, $site_id, $product_id);
  • publish() Publishes the site.
  • unpublish() Unpublishes the site.

Site

API Documentation

A mutable and deletable respresentation of a site. To construct:

To construct:

$site = new WeeblyCloud\Site($user_id, $site_id);
  • publish() Publishes the site.
  • unpublish() Unpublishes the site.
  • loginLink() Generates a one-time log-in link that redirects users to the editor for this Site. Will throw an exception if the user whose ID was used to instantiate the Site object is disabled.
  • setPublishCredentials($data) Sets publish credentials for a given site. If a user's site will not be hosted by Weebly, publish credentials can be provided. Required properties of $data are publish_host, publish_username, publish_password, and publish_path.
  • restore($domain) When a site is restored the owner of the site is granted access to it in the exact state it was when it was deleted, including the Weebly plan assigned. Restoring a site does not issue an automatic publish
  • disable() Disables a site, preventing the user from accessing it through the editor.
  • enable() Enables a site, allowing it to be edited. Sites are enabled by default when created.
  • listPages($search_params = []) Returns a CloudList of Pages on this Site, subject to the search parameters.
  • listMembers($search_params = []) Returns a CloudList of Members on this Site, subject to the search parameters.
  • listGroups($search_params = []) Returns a CloudList of Members on this Site, subject to the search parameters.
  • listForms() Returns a CloudList of Forms on this Site.
  • listBlogs() Returns a CloudList of Blogs on this Site.
  • getPage($page_id) Return the Page with the given id.
  • getMember($member_id) Return the Member with the given id.
  • getGroup($group_id) Return the Group with the given id.
  • getForm($form_id) Return the Form with the given id.
  • getBlog($blog_id) Return the Blog with the given id.
  • getStore() Return the Store resource for the site.
  • getPlan() Returns the Plan resource for the site.
  • setPlan($plan_id, $term = 1) Assign a plan to the site with an optional term length.
  • setTheme($theme_id, $is_custom) Assign a theme to the site by ID. Requires a parameter is_custom, distinguishing whether the theme is a Weebly theme or a custom theme.
  • createMember($data) Creates a new Member of the site in the database. Returns the newly created Member.
  • createGroup($name) Creates a new Group of members of the site in the database. Returns the newly created Group.
  • listProducts($search_params) Retrieves a list of products for the store, subject to search parameters.
  • getProductCount() Returns the number of products in the store.
  • createProduct($product_name, $product_skus, $data = []) Creates a new Product in the Store in the database. Returns the newly created Product.
  • getProduct($product_id) Retrieves a specific Product from the Store by ID.

Store

API Documentation

A mutable respresentation of a site. To construct:

To construct:

$store = new WeeblyCloud\Store($user_id, $site_id);
  • updateStore($values) Update the store with provided values.

User

API Documentation

A mutable respresentation of a WeeblyCloud user. To construct:

$user = new WeeblyCloud\User($user_id);
  • enable() Enables a user account after an account has been disabled. Enabling a user account will allow users to log in and edit their sites. When a user is created, their account is automatically enabled. disable() Disables a user account, preventing them from logging in or editing their sites.
  • loginLink() Generates a one-time login link. Will return an error if the user has been disabled.
  • getAvailableThemes($search_params = []) Returns an array of themes available to this user, subject to optional search parameters. The themes in the array are NOT resource objects, but are constructed directly from the response JSON. For valid search parameters, see the API documentation.
  • listSites($search_params = []) Returns a CloudList of sites belonging to this user, subject to optional search parameters. getSite($site_id) Returns the Site with the given ID.
  • createCustomTheme($name, $zip_url) Adds a custom theme to a user. Requires the name of the theme and the url of a publicly accessible .zip file.
  • createSite($domain, $data = []) Creates a new Site belonging to this user in the database. Requires the site's domain and an optional associative array of properties, data. Returns the newly created Site.

Making Raw API Calls

Not every resource has a cooresponding resource class. It is possible to make a raw API call using a CloudClient object.

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

Using that client, call get, post, put, patch, or delete. All client request methods take a url as their first argument. post, patch, and put take an optional hash map of data that will be sent in the request body. get takes an optional hash map whose values will be used in the query string of the request.

The url must not include a leading slash.

Request examples

Get cloud admin account account
# Get client
$client = WeeblyCloud\Utils\CloudClient::getClient();

# Request the /account endpoint
$client->get("account");
Update a page title
# 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 all sites for a user (with search parameters)
# 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"]);

Handling Responses

All requests return a CloudResponse object or throw a CloudException. The JSON returned by the request can be accessed through the response's body property.

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

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

###Pagination example If the endpoint supports pagination, the next and previous pages of results can be retrieved with the getPreviousPage() and getNextPage() methods. If there is no next or previous page, those methods return 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();
}

Get all of a user's sites, 10 sites per page.

weebly/cloud-client 适用场景与选型建议

weebly/cloud-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.36k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2016 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 weebly/cloud-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 weebly/cloud-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 weebly/cloud-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 35.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 13
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-08-31