第一步:获取 REST API 密钥
进入 WordPress 后台:WooCommerce > 设置 > 高级 > REST API。添加密钥,权限务必选择 “读/写” (Read/Write)。
第二步:关闭库存管理 (极其重要)
注意: 本系统上架时默认关闭产品库存追踪。请前往 WooCommerce 设置 > 产品 > 库存,取消勾选“启用库存管理”。
第三步:写入代理脚本 (proxy.php)
在您的网站根目录下新建 proxy.php,点击下方按钮复制代码并存入。这能保障图片正常显示:
<?php
/**
* WooCommerce 端的图片中转代理
* 用于绕过 1688 防盗链并显示图片
*/
$url = isset($_GET['url']) ? $_GET['url'] : '';
if (empty($url)) {
header("HTTP/1.1 404 Not Found");
exit('URL Missing');
}
// 设置请求头,伪造 Referer 为 1688
$options = [
'http' => [
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\r\n" .
"Referer: https://www.1688.com/\r\n",
'method' => 'GET',
'timeout' => 20
],
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
]
];
$context = stream_context_create($options);
$imgData = @file_get_contents($url, false, $context);
if ($imgData !== false) {
// 识别图片类型
$ext = strtolower(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION));
$mime = 'image/jpeg'; // 默认
if ($ext == 'png') $mime = 'image/png';
if ($ext == 'gif') $mime = 'image/gif';
if ($ext == 'webp') $mime = 'image/webp';
header("Content-Type: $mime");
header("Cache-Control: public, max-age=86400"); // 浏览器缓存1天
echo $imgData;
} else {
header("HTTP/1.1 404 Not Found");
exit('Image Fetch Failed');
}
?>