From 650f9430374d82a4f322d5e827b3bcd30648f979 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 21 Jan 2026 14:12:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E7=AB=AFurl=E6=8A=93?= =?UTF-8?q?=E5=8F=96=E6=87=82=E8=BD=A6=E5=B8=9D=E8=BD=A6=E8=BE=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/controller/UsedCarController.php | 19 +++++++++ .../crawl/service/UsedCarCrawlSingle.php | 42 ++++++++++++++++++- config/route.php | 3 +- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/common/controller/UsedCarController.php b/app/common/controller/UsedCarController.php index b2f2a94..2b1365b 100644 --- a/app/common/controller/UsedCarController.php +++ b/app/common/controller/UsedCarController.php @@ -128,6 +128,25 @@ class UsedCarController extends BaseController return $data; } + /** + * 移动端链接抓取 + * @param Request $request + */ + public function crawlMobileCar(Request $request) + { + try { + $urlShare = $request->get('url'); + + $finalUrl = (new UsedCarCrawlSingle())->getFinaUrl($urlShare); + + $request->setGet('url', $finalUrl); + $data = $this->crawlCar($request); + return $data; + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + public function crawlCar(Request $request) { try { diff --git a/app/common/utils/crawl/service/UsedCarCrawlSingle.php b/app/common/utils/crawl/service/UsedCarCrawlSingle.php index c04cd10..0d6bf26 100644 --- a/app/common/utils/crawl/service/UsedCarCrawlSingle.php +++ b/app/common/utils/crawl/service/UsedCarCrawlSingle.php @@ -36,10 +36,50 @@ class UsedCarCrawlSingle 'headers' => self::DEFAULT_HEADERS, 'timeout' => 10, ]); - return (string)$response->getBody(); } + /** + * 获取移动端最终url + * + * @param string $url + * @return string + * @throws GuzzleException + */ + public function getFinaUrl(string $url): string + { + $client = new Client([ + 'verify' => false, + ]); + $response = $client->get($url, [ + 'allow_redirects' => ['track_redirects' => true], + ]); + + $history = $response->getHeader('X-Guzzle-Redirect-History'); + $finalUrl = end($history); + + $sku_id = $this->getSkuIdFromUrl($finalUrl); + + return 'https://www.dongchedi.com/usedcar/'.$sku_id; + } + + /** + * 从url获取sku_id + * @param string $url + * @return string|null + */ + public function getSkuIdFromUrl(string $url): ?string + { + $query = parse_url($url, PHP_URL_QUERY); + if (!$query) { + return null; + } + + parse_str($query, $params); + + return $params['sku_id'] ?? null; + } + public function crawlCar(string $url){ $html = $this->fetchHtml($url); if ($html === '' || str_contains($html, '404 Not Found')) { diff --git a/config/route.php b/config/route.php index f6b0373..b9337f4 100644 --- a/config/route.php +++ b/config/route.php @@ -68,12 +68,11 @@ Route::group('/common', function () { Route::any('/destroy', [app\common\controller\UsedCarController::class, 'destroy']); Route::any('/crawl', [app\common\controller\UsedCarController::class, 'crawlKeyword']); Route::any('/url_crawl', [app\common\controller\UsedCarController::class, 'crawlCar']); + Route::any('/mobile/url_crawl', [app\common\controller\UsedCarController::class, 'crawlMobileCar']); }); Route::group('/user', function () { Route::any('/info', [app\common\controller\UserController::class, 'show']); Route::any('/edit', [app\common\controller\UserController::class, 'edit']); }); - - }); \ No newline at end of file