arslaniftikhar / shopify-client
Shopify API客户端,用于PHP
1.0
2019-05-18 11:44 UTC
Requires
- php: >=5.5.0
- arslaniftikhar/curl: >=1.0
This package is auto-updated.
Last update: 2024-09-19 00:24:24 UTC
README
一个简单的Shopify PHP SDK,用于私有应用与Shopify API轻松交互。
包具有以下功能
- 能够轻松地通过GET、PUT、POST和DELETE操作资源
设置/安装
使用 arslaniftikhar/curl。您需要通过以下命令包含此库
composer require arslaniftikhar/curl 1.0
公共应用
首先,您必须已设置公共应用。 查看文档。您需要一个授权URL。
$shopify = new Shopify($shop, $APP_API_KEY, $APP_SECRET); $client->installURL($permissions, $redirect_uri, $auto_redirect = true);
此时,用户将被带到他们的商店以授权应用程序使用他们的信息。
如果用户接受,他们将被带到重定向URL。
session_start(); $shopify = new Shopify($_GET['shop'], $APP_API_KEY, $APP_SECRET); if ($token = $client->getAccessToken()) { // You can save the access token to database to make the api call in future. $_SESSION['shopify_access_token'] = $token; $_SESSION['shopify_shop_domain'] = $_GET['shop']; // Redirect to app's dashboard URL header("Location: dashboard.php"); } else { die('Couldn't find the access token'); }
在这个时候,在 dashboard.php 中,您可以通过设置 access_token
开始进行API请求。
session_start(); $shopify = new Shopify(, $APP_API_KEY, $APP_SECRET); $shopify->setAccessToken($_SESSION['shopify_access_token']); // you can get the resource by just passing the resource name. $products = $shopify->get('products');
方法
GET
从API获取资源信息。
$shopify = new Shopify($SHOPIFY_SHOP_DOMAIN, $SHOPIFY_API_KEY, $SHOPIFY_SHARED_SECRET); $result = $shopify->get('shop');
$result
是一个JSON解码的 array
通过传递查询参数获取产品ID
$result = $shopify->get('products', ['query' => ['fields' => 'id']]); foreach($result->products as $product) { print $product->id; }
POST
通过POST请求创建新内容。
$data = ['product' => ['title' => 'my new product']]; $result = $shopify->post('products', $data);
PUT
使用给定ID更新现有内容。
$data = ['product' => ['title' => 'updated product name']]; $result = $shopify->put('products/' . $product_id, $data);
DELETE
通过给定ID轻松删除资源。
$shopify->delete('products/' . $product_id);