39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
// 获取传入参数
|
|
$goods = $_GET['goods'] ?? null;
|
|
|
|
// 检查参数是否提供
|
|
if ($goods === null) {
|
|
echo json_encode(["error" => "Missing required parameter: goods"]);
|
|
exit;
|
|
}
|
|
|
|
// 定义目标地址
|
|
$target_url = "http://localhost:8022/goods/flash_sale_1.php";
|
|
|
|
// 使用 cURL 抓取页面内容
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $target_url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置超时时间
|
|
$response = curl_exec($ch);
|
|
|
|
if (curl_errno($ch)) {
|
|
echo json_encode(["error" => "Failed to fetch the target page"]);
|
|
curl_close($ch);
|
|
exit;
|
|
}
|
|
curl_close($ch);
|
|
|
|
// 使用正则表达式匹配指定商品的价格
|
|
// 根据提供的网页结构,价格在 <span id="商品ID">价格</span> 中
|
|
$pattern = '/<span id="' . preg_quote($goods, '/') . '">(\d+)元<\/span>/';
|
|
if (preg_match($pattern, $response, $matches)) {
|
|
// 提取并输出价格
|
|
echo json_encode([(int)$matches[1]]);
|
|
} else {
|
|
// 如果没有匹配到价格,返回提示信息
|
|
echo json_encode(["error" => "Price not found for the specified goods"]);
|
|
}
|
|
?>
|