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