Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

beromir/Servas

739 stars
2 code files
View beromir/Servas on GitHub

app/Helpers/WebpageData.php

Open in GitHub
use 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 GitHub
use 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']);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.