Question

I'm developing a social media platform using a LAMP build. So far my users can upload pictures and videos and comment and vote on them. I want users to be able to post a url link to an article, and have the title, image and description automatically pop up like it does on Facebook. I'm guessing that most web pages containing articles include some sort of meta data that would allow a developer like me to systematically access the title, description fields etc. If this is the case, then how specifically do I access this metadata. Otherwise, how does Facebook do it?

Thanks,

Was it helpful?

Solution

You can use a PHP HTML parsing library that allows you to input a URL, and break out meta information at your choosing.

This answer on StackOverflow has an excellent list of available HTML parsing options for PHP: https://stackoverflow.com/a/3577662/1332068

OTHER TIPS

This scrapes all of the images off of whatever valid url you input:

<?php
if(isset($_POST['link'])){
    $link = $_POST['link'];
    $scrapings = "";
    $article = new DOMDocument;
    $article ->loadHTMLFile($link);
    $titles = $article->getElementsByTagName("title");
    foreach($titles as $title){
        echo $title->nodeValue, PHP_EOL;
    }
    $images = $article->getElementsByTagName("img");
    foreach($images as $image){
        $source = $image->getAttribute("src");
        $scrapings .= '<img src="'.$source.'" alt="default">';
    }
}
?>
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <form method="POST" action="article_system.php">
            <input type="text" name="link">
            <input type="submit" value="submit">
        </form>
        <?php echo $scrapings; ?>
    </body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top