-
app/Helpers/WebpageData.php
Open in GitHubuse GuzzleHttp\Exception\RequestException; use Illuminate\Support\Facades\Http; class WebpageData { public static function getWebPageTitle(string $url): string { $filteredUrl = filter_var($url, FILTER_VALIDATE_URL); if ($filteredUrl === false) return ''; try { $html = Http::timeout(30)->get($filteredUrl)->body(); preg_match_all('@<head.*>.*<title.*>(.*)</title>.*</head>@sU', $html, $matches); if (empty($matches[1])) return ''; return html_entity_decode($matches[1][0]); } catch (RequestException) { return ''; } } }
-
app/Http/Controllers/ApiControllers/LinkController.php
Open in GitHubuse App\Helpers\WebpageData; use App\Http\Controllers\Controller; use App\Http\Requests\StoreLinkApiRequest; use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; class LinkController extends Controller { public function store(StoreLinkApiRequest $request): Response { $validated = $request->validated(); $link = Link::make(); $link->link = $validated['link']; $link->title = $validated['title']; $link->user_id = Auth::id(); if (empty($link->title)) { $link->title = WebpageData::getWebPageTitle($link->link); } $link->save(); return response('The link was added.', headers: ['Content-Type' => 'text/plain']); } }