Web_Crawler_API/routes/api.php
2023-05-30 20:19:53 +03:00

45 lines
1.3 KiB
PHP

<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
use App\Http\Controllers\WebCrawlController;
use GuzzleHttp\Psr7\Response;
Route::get('/crawl', function (Request $request) {
// invode WebCrawlController index method in WebCrawlController
$url = $request->input('url');
// check if the url is valid URL
if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) {
return response()->json([
'error' => 'Missing required parameter `url`'
], 400);
}
$depth = $request->input('depth', 3); // default depth is 3 if not provided
$crawlerController = new WebCrawlController();
$isAlreadyDone = $crawlerController->getOne($url);
if(!$isAlreadyDone){
$crawlerController->crawlWebsite($url, $depth);
} else {
return response()->json([
'error' => 'This URL has already been crawled',
'data' => $isAlreadyDone
], 400);
}
});
// Route::post('/crawl/{id}', function (String $id, Request $request, Response $response) {
// $id = $request->input('id');
// $crawlerController = new WebCrawlController();
// if(!$crawlerController->destroy($id)) {
// return response()->json([
// 'error' => 'Url Not Found',
// ], 404);
// } else {
// return response()->json([
// 'success' => 'This URL has been deleted',
// ], 200);
// }
// });