jublonet/shapecode-php
Composer 安装命令:
composer require jublonet/shapecode-php
包简介
A Shapeways API library in PHP.
关键字:
README 文档
README
A Shapeways API library in PHP.
Copyright (C) 2014-2016 Jublo Solutions support@jublo.net
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Requirements
- PHP 5.5.0 or higher
- JSON extension
- OpenSSL extension
Authentication
To authenticate your API requests on behalf of a certain Shapeways user (following OAuth 1.0a), take a look at these steps:
require_once ('shapecode.php'); Shapecode::setConsumerKey('YOURKEY', 'YOURSECRET'); // static, see 'Using multiple Shapecode instances' $sc = Shapecode::getInstance();
You may either set the OAuth token and secret, if you already have them:
$sc->setToken('YOURTOKEN', 'YOURTOKENSECRET');
Or you authenticate, like this:
session_start(); if (! isset($_SESSION['oauth_token'])) { // get the request token $reply = $sc->oauth1_requestToken(array( 'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] )); // store the token $_SESSION['oauth_token'] = $reply->oauth_token; $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret; $_SESSION['oauth_verify'] = true; // redirect to auth website header('Location: ' . $reply->authentication_url); die(); } elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) { // verify the token $sc->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); unset($_SESSION['oauth_verify']); // get the access token $reply = $sc->oauth1_accessToken(array( 'oauth_verifier' => $_GET['oauth_verifier'] )); // store the token (which is different from the request token!) $_SESSION['oauth_token'] = $reply->oauth_token; $_SESSION['oauth_token_secret'] = $reply->oauth_token_secret; // send to same URL, without oauth GET parameters header('Location: ' . basename(__FILE__)); die(); } // assign access token on each page load $sc->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
Usage examples
When you have an access token, calling the API is simple:
$sc->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); // see above $reply = (array) $sc->api(); print_r($reply);
Adding a model to your cart is as easy as this:
$reply = $sc->orders_cart('modelId=480903');
For more complex parameters (see the Shapeways API documentation), giving all parameters in an array is supported, too:
$params = array( 'modelId' => '480903', 'materialId' => 61, 'quantity' => 3 ); $reply = $sc->orders_cart($params);
When uploading files to Shapeways, just give the file path:
$params = array( 'file' => 'in-some-folder/there-is/the-model.stl', 'fileName' => 'the-model.stl', 'hasRightsToModel' => 1, 'acceptTermsAndConditions' => 1, 'title' => 'My great model', 'description' => 'Lorem ipsum dolor sit amet', 'isPublic' => 1, 'isForSale' => 1, 'isDownloadable' => 0, 'tags' => array( 'ideas', 'miniatures', 'stuff' ) ); $reply = $sc->models($params); // required HTTP POST is auto-detected
Mapping API methods to Shapecode function calls
As you can see from the last example, there is a general way how the Shapeways API methods map to Shapecode function calls. The general rules are:
-
Omit the version info in the function name.
Example:
/v1is not part of the Shapecode function call. -
For each slash in a Shapeways API method, use an underscore in the Shapecode function.
Example:
orders/cart/v1maps toShapecode::orders_cart(). -
For each underscore in a Shapeways API method, use camelCase in the Shapecode function.
Example:
oauth1/request_token/v1maps toShapecode::oauth1_requestToken(). -
For each parameter template in method, use UPPERCASE in the Shapecode function. Also don’t forget to include the parameter in your parameter list.
Example:
materials/{materialId}/v1maps toShapecode::materials_MATERIALID('materialId=73').
HTTP methods (GET, POST, DELETE etc.)
Never care about which HTTP method (verb) to use when calling a Shapeways API. Shapecode is intelligent enough to find out on its own. For the automatic detection to work, be sure to use the correct required parameters, as outlined in the Shapeways API documentation.
The only exception to the above is the DELETE models/{modelId} method.
To call it, use the delete=1 parameter. It will trigger the DELETE method,
but is not sent to the API.
Response codes
The HTTP response code that the API gave is included in any return values.
You can find it within the return object’s httpstatus property.
To know whether your API call was successful, check the $reply->result string,
which either reads success or failure.
Return formats
The default return format for API calls is a PHP object. Upon your choice, you may also get PHP arrays directly:
$sc->setReturnFormat(SHAPECODE_RETURNFORMAT_ARRAY);
The Shapeways API natively responds to API calls in JSON (JS Object Notation). To get a JSON string, set the corresponding return format:
$sc->setReturnFormat(SHAPECODE_RETURNFORMAT_JSON);
Using multiple Shapecode instances
By default, Shapecode works with just one instance. This programming paradigma is called a singleton.
Getting the main Shapecode object is done like this:
$sc = Shapecode::getInstance();
If you need to run requests to the Shapeways API for multiple users at once, Shapecode supports this as well. Instead of getting the instance like shown above, create a new object:
$sc1 = new Shapecode; $sc2 = new Shapecode;
Please note that your OAuth consumer key and secret is shared within multiple Shapecode instances, while the OAuth request and access tokens with their secrets are not shared.
How Do I…?
…walk through paged results?
The Shapeways API utilizes a technique called ‘paging’ for large result sets. Pages separates results into pages of no more than 36 results at a time, and provides a means to move backwards and forwards through these pages.
Here is how you can walk through paged results with Shapecode.
- Get the first result set of a paged method:
$page = 1; $result1 = $sc->models();
- To navigate forth, increment the
$page:
$page++;
- If
$nextCursoris not 0, use this cursor to request the next result page:
$result2 = $sc->models("page=$page");
It might make sense to use the pages in a loop. Watch out, though, not to send more than the allowed number of requests per rate-limit timeframe, or else you will hit your rate-limit.
…know what cacert.pem is for?
Connections to the Shapeways API are done over a secured SSL connection. Shapecode-php checks if the Shapeways API server has a valid SSL certificate. Valid certificates have a correct signature-chain. The cacert.pem file contains a list of all public certificates for root certificate authorities. You can find more information about this file at http://curl.haxx.se/docs/caextract.html.
…set the timeout for requests to the Shapeways API?
For connecting to Shapeways, Shapecode uses the cURL library, if available. You can specify both the connection timeout and the request timeout, in milliseconds:
$sc->setConnectionTimeout(2000); $sc->setTimeout(5000);
If you don't specify the timeout, Shapecode uses these values:
- connection time = 5000 ms = 5 s
- timeout = 2000 ms = 2 s
…disable cURL?
Shapecode automatically detects whether you have the PHP cURL extension enabled.
If not, the library will try to connect to the Shapeways API via socket.
For this to work, the PHP setting allow_url_fopen must be enabled.
You may also manually disable cURL. Use the following call:
$sc->setUseCurl(false);
jublonet/shapecode-php 适用场景与选型建议
jublonet/shapecode-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2014 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「modeling」 「printing」 「shapeways」 「3d」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jublonet/shapecode-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jublonet/shapecode-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jublonet/shapecode-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Data validation library. Validate arrays, array objects, domain models etc using a simple API. Easily add your own validators on top of the already dozens built-in validation rules
Kata for domain models
Commons for domain models
Simple PHP Request Validator
Data validation library. Validate arrays, array objects, domain models etc using a simple API. Easily add your own validators on top of the already dozens built-in validation rules. This fork maintains PHP 5.3 compatibility.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2014-02-17