原文提供的 taoBaoJiaGe() 使用 file_get_contents + 正則提取價格,存在嚴重問題:
robots.txt、User-Agent 聲明、頻率限制<?php
declare(strict_types=1);
class TaobaoPriceFetcher
{
private int $timeout = 10;
private string $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
private string $cookieFile; // 支援 Cookie 持久化
public function __construct(string $cookieFile = '')
{
$this->cookieFile = $cookieFile ?: sys_get_temp_dir() . '/tb_cookies.txt';
}
/**
* 獲取商品價格資訊
* @return array ['price' => '199.00', 'original_price' => '299.00', 'currency' => 'CNY', 'sku_prices' => [...]] 或拋出異常
*/
public function fetch(string $itemUrl): array
{
$html = $this->request($itemUrl);
return $this->parsePrice($html, $itemUrl);
}
private function request(string $url): string
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CONNECTTIMEOUT => $this->timeout,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_ENCODING => 'gzip, deflate, br', // 支援壓縮
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control: no-cache',
'Pragma: no-cache',
],
]);
$html = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($errno) throw new RuntimeException("cURL Error [$errno]: $error");
if ($code !== 200) throw new RuntimeException("HTTP $code: " . ($code === 403 ? '疑似反爬攔截' : '請求失敗'));
if (empty($html)) throw new RuntimeException('Empty response');
// 簡易反爬檢測:檢查是否為驗證頁/登入頁/錯誤頁
if (strpos($html, 'nc_') !== false || strpos($html, 'alibaba.com') !== false && strpos($html, 'slide') !== false) {
throw new RuntimeException('觸發滑塊驗證/反爬頁面,需人工介入或使用無頭瀏覽器');
}
return $html;
}
private function parsePrice(string $html, string $url): array
{
// 策略 1: 現代淘寶/天貓頁面價格通常在 JSON-LD 結構化數據或 window.__INIT_DATA__ 中
// 這比正則匹配 HTML 標籤穩健得多
if (preg_match('/window\.__INIT_DATA__\s*=\s*(\{.+?\});/s', $html, $m)) {
$json = json_decode($m[1], true);
if ($json) {
$price = $this->extractPriceFromInitData($json);
if ($price) return $price;
}
}
// 策略 2: JSON-LD 結構化數據 (SEO 標準)
if (preg_match_all('/