문제

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,

도움이 되었습니까?

해결책

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

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top