以下是使用 PHP 调用 Shopify API 的完整指南,涵盖认证、基础请求和常见操作示例。
your-store
)2024-01
,查看版本列表)推荐使用 Guzzle(PHP 的流行 HTTP 客户端):
composer require guzzlehttp/guzzle
Shopify API 使用 OAuth 2.0(自定义应用)或 Basic Auth(私有应用)。 这里以 私有应用 的 Basic Auth 为例(更简单)。
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$shopName = 'your-store'; // 替换为你的店铺子域名
$apiKey = 'your-api-key'; // 替换为你的 API Key
$password = 'your-password'; // 替换为你的 API Password
$apiVersion = '2024-01'; // 替换为你的 API 版本
// 初始化 Guzzle 客户端
$client = new Client([
'base_uri' => "https://{$apiKey}:{$password}@{$shopName}.myshopify.com/admin/api/{$apiVersion}/",
'headers' => [
'Content-Type' => 'application/json',
],
]);
try {
$response = $client->get('products.json');
$products = json_decode($response->getBody(), true);
print_r($products['products']); // 输出产品列表
} catch (\GuzzleHttp\Exception\RequestException $e) {
echo "Error: " . $e->getMessage();
}
try {
$data = [
'product' => [
'title' => 'Test Product',
'body_html' => '<p>This is a test product.</p>',
'vendor' => 'Test Vendor',
'product_type' => 'Test Type',
],
];
$response = $client->post('products.json', [
'json' => $data,
]);
$newProduct = json_decode($response->getBody(), true);
echo "Created Product ID: " . $newProduct['product']['id'];
} catch (\GuzzleHttp\Exception\RequestException $e) {
echo "Error: " . $e->getMessage();
}
try {
$productId = 123456789; // 替换为实际产品 ID
$data = [
'product' => [
'title' => 'Updated Product Title',
],
];
$response = $client->put("products/{$productId}.json", [
'json' => $data,
]);
$updatedProduct = json_decode($response->getBody(), true);
echo "Updated Product Title: " . $updatedProduct['product']['title'];
} catch (\GuzzleHttp\Exception\RequestException $e) {
echo "Error: " . $e->getMessage();
}
try {
$productId = 123456789; // 替换为实际产品 ID
$response = $client->delete("products/{$productId}.json");
if ($response->getStatusCode() === 200) {
echo "Product deleted successfully.";
}
} catch (\GuzzleHttp\Exception\RequestException $e) {
echo "Error: " . $e->getMessage();
}
如果使用 自定义应用(需用户授权),流程更复杂,需以下步骤:
$shopName = 'your-store';
$clientId = 'your-client-id';
$redirectUri = 'https://your-app.com/callback'; // 替换为你的回调地址
$scopes = 'read_products,write_products'; // 所需权限
$authUrl = "https://{$shopName}.myshopify.com/admin/oauth/authorize?client_id={$clientId}&scope={$scopes}&redirect_uri={$redirectUri}";
header("Location: {$authUrl}"); // 重定向用户到授权页面
exit;
用户授权后,Shopify 会重定向到你的 redirect_uri
并附带 code
参数:
$shopName = $_GET['shop']; // 从回调 URL 获取店铺名
$code = $_GET['code']; // 授权码
$clientId = 'your-client-id';
$clientSecret = 'your-client-secret';
// 请求 Access Token
$response = (new Client())->post("https://{$shopName}.myshopify.com/admin/oauth/access_token", [
'form_params' => [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'code' => $code,
],
]);
$accessToken = json_decode($response->getBody(), true)['access_token'];
echo "Access Token: " . $accessToken;
$client = new Client([
'base_uri' => "https://{$shopName}.myshopify.com/admin/api/{$apiVersion}/",
'headers' => [
'Content-Type' => 'application/json',
'X-Shopify-Access-Token' => $accessToken,
],
]);
// 示例:获取产品
$response = $client->get('products.json');
$products = json_decode($response->getBody(), true);
print_r($products['products']);
products.json
不是 product.json
)。Shopify API 限制:
可通过响应头 X-Shopify-Shop-Api-Call-Limit
查看剩余配额:
$headers = $response->getHeaders();
$limit = $headers['X-Shopify-Shop-Api-Call-Limit'][0];
echo "API Calls: {$limit}"; // 格式如 "39/40"
没有搜到相关的文章