How can PHP be used to implement a feature that displays website content when a link is posted in a post-box, similar to Facebook?
To implement a feature that displays website content when a link is posted in a post-box, similar to Facebook, you can use PHP to extract the URL from the post, fetch the content of the URL using cURL or file_get_contents, and then display the relevant information such as the title, description, and image.
<?php
function getWebsiteContent($url) {
$html = file_get_contents($url);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$title = $doc->getElementsByTagName('title')->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
$description = '';
$image = '';
foreach ($metas as $meta) {
if ($meta->getAttribute('property') == 'og:description') {
$description = $meta->getAttribute('content');
}
if ($meta->getAttribute('property') == 'og:image') {
$image = $meta->getAttribute('content');
}
}
return array(
'title' => $title,
'description' => $description,
'image' => $image
);
}
$url = "https://example.com";
$websiteContent = getWebsiteContent($url);
echo "Title: " . $websiteContent['title'] . "<br>";
echo "Description: " . $websiteContent['description'] . "<br>";
echo "Image: <img src='" . $websiteContent['image'] . "'><br>";
?>