lngo/shopify-sdk-php
Composer 安装命令:
composer create-project lngo/shopify-sdk-php
包简介
Shopify SDK PHP had cloned from https://github.com/cmcdonaldca/ohShopify.php.git.
README 文档
README
Lightweight multi-paradigm PHP (JSON) client for the Shopify API.
Requirements
- PHP 5.4 with cURL support.
- Only compatible OAuth Shopify Apps. For Legacy Authentication: Use an old version of ohShopify.php
Getting Started
Basic needs for authorization and redirecting
<?php This libary it working in yii2 framework at currently. You allow create new branch to change code base or change namespace anyway make your app running. :D require 'path/to/shopify.php'; /* Define your APP`s key and secret*/ define('SHOPIFY_API_KEY',''); define('SHOPIFY_SECRET',''); /* Define requested scope (access rights) - checkout https://docs.shopify.com/api/authentication/oauth#scopes */ define('SHOPIFY_SCOPE',''); //eg: define('SHOPIFY_SCOPE','read_orders,write_orders'); if (isset($_GET['code'])) { // if the code param has been sent to this page... we are in Step 2 // Step 2: do a form POST to get the access token $shopifyClient = new ShopifyClient($_GET['shop'], "", SHOPIFY_API_KEY, SHOPIFY_SECRET); session_unset(); // Now, request the token and store it in your session. $_SESSION['token'] = $shopifyClient->getAccessToken($_GET['code']); if ($_SESSION['token'] != '') $_SESSION['shop'] = $_GET['shop']; header("Location: index.php"); exit; } // if they posted the form with the shop name else if (isset($_POST['shop'])) { // Step 1: get the shopname from the user and redirect the user to the // shopify authorization page where they can choose to authorize this app $shop = isset($_POST['shop']) ? $_POST['shop'] : $_GET['shop']; $shopifyClient = new ShopifyClient($shop, "", SHOPIFY_API_KEY, SHOPIFY_SECRET); // get the URL to the current page $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["SCRIPT_NAME"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"]; } // redirect to authorize url header("Location: " . $shopifyClient->getAuthorizeUrl(SHOPIFY_SCOPE, $pageURL)); exit; } // first time to the page, show the form below ?> <p>Install this app in a shop to get access to its private admin data.</p> <p style="padding-bottom: 1em;"> <span class="hint">Don’t have a shop to install your app in handy? <a href="https://app.shopify.com/services/partners/api_clients/test_shops">Create a test shop.</a></span> </p> <form action="" method="post"> <label for='shop'><strong>The URL of the Shop</strong> <span class="hint">(enter it exactly like this: myshop.myshopify.com)</span> </label> <p> <input id="shop" name="shop" size="45" type="text" value="" /> <input name="commit" type="submit" value="Install" /> </p> </form>
Once you have authorized and stored the token in the session, you can make API calls
Making API calls:
<?php require 'path/to/shopify.php'; $sc = new ShopifyClient($_SESSION['shop'], $_SESSION['token'], $api_key, $secret); try { // Get all products $products = $sc->call('GET', '/admin/products.json', array('published_status'=>'published')); // Create a new recurring charge $charge = array ( "recurring_application_charge"=>array ( "price"=>10.0, "name"=>"Super Duper Plan", "return_url"=>"http://super-duper.shopifyapps.com", "test"=>true ) ); try { $recurring_application_charge = $sc->call('POST', '/admin/recurring_application_charges.json', $charge); // API call limit helpers echo $sc->callsMade(); // 2 echo $sc->callsLeft(); // 498 echo $sc->callLimit(); // 500 } catch (ShopifyApiException $e) { // If you're here, either HTTP status code was >= 400 or response contained the key 'errors' } } catch (ShopifyApiException $e) { /* $e->getMethod() -> http method (GET, POST, PUT, DELETE) $e->getPath() -> path of failing request $e->getResponseHeaders() -> actually response headers from failing request $e->getResponse() -> curl response object $e->getParams() -> optional data that may have been passed that caused the failure */ } catch (ShopifyCurlException $e) { // $e->getMessage() returns value of curl_errno() and $e->getCode() returns value of curl_ error() } ?>
When receiving requests from the Shopify API, validate the signature value:
<?php $sc = new ShopifyClient($_GET['shop'], '', SHOPIFY_API_KEY, SHOPIFY_SECRET); if(!$sc->validateSignature($_GET)) die('Error: invalid signature.'); ?>
lngo/shopify-sdk-php 适用场景与选型建议
lngo/shopify-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「api」 「composer」 「sdk」 「shopify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lngo/shopify-sdk-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lngo/shopify-sdk-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lngo/shopify-sdk-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
Simple ASCII output of array data
Generate a Composer repository from installed WordPress plugins and themes.
统计信息
- 总下载量: 44
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2017-09-09