Web_Crawler_API/app/Http/Controllers/WebCrawlController.php
2023-05-30 20:19:53 +03:00

154 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\WebCrawl;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class WebCrawlController extends Controller
{
protected $webCrawl;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$allCrawls = WebCrawl::all();
//Return the results in JSON format
return response()->json($allCrawls);
}
public function crawlWebsite($url, $depth) {
// // Use GuzzleHttp client to send HTTP requests
$client = new Client();
$response = $client->get($url);
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
$body = $response->getBody()->getContents();
// get:
// links from the page
// full content
// depth
// url
// visitedUrls
}
// // Check if the HTTP response is successful (status code 2xx)
// // Insert a page info the database if the HTTP response satus is successful
// $webCrawl = new WebCrawl();
// $webCrawl->url = $url;
// $webCrawl->content = $response->getBody()->getContents();
// $webCrawl->save();
// }
// Crawl the links on the page
echo 'Crawling completed!';
}
public function getOne($url)
{
$webCrawl = WebCrawl::where('url', $url)->first();
echo 'here!';die;
if ($webCrawl) {
return $webCrawl;
}
return false;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
private function create($response, $url, $depth, $visitedUrls, $links)
{
$webCrawl = new WebCrawl();
$webCrawl->url = $url;
$webCrawl->content = $response->getBody()->getContents();
$webCrawl->depth = $depth;
$webCrawl->visited_urls = $visitedUrls;
$webCrawl->status_code = $response->getStatusCode();
$webCrawl->status = $response->getReasonPhrase();
$webCrawl->created_at = $response->getHeader('Date')[0];
$webCrawl->updated_at = $response->getHeader('Last-Modified')[0];
$webCrawl->links = $links;
$webCrawl->save();
return $webCrawl;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\WebCrawl $webCrawl
* @return \Illuminate\Http\Response
*/
public function show(WebCrawl $webCrawl)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\WebCrawl $webCrawl
* @return \Illuminate\Http\Response
*/
public function edit(WebCrawl $webCrawl)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\WebCrawl $webCrawl
* @return \Illuminate\Http\Response
*/
public function update(Request $request, WebCrawl $webCrawl)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\WebCrawl $webCrawl
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$webCrawl = WebCrawl::where("_id", $id);
echo '<pre>';
echo 'fff';
print_r($webCrawl);die;
if ($webCrawl) {
$webCrawl->delete();
return true;
}
return false;
}
}