Is there a library that emulates facebook’s “Link Detect”?

I’m looking to write a library that “parses” information like facebook does when you post a link. However as I don’t want to reinvent the wheel does anyone know of a library or and effort to write an Library that does this already?

I have included an example so that you can get a grasp of what I mean if you don’t use face book. http://lh4.ggpht.com/_zbED-KN_ZAI/Sx6LuDmZkVI/AAAAAAAADLs/mN7eFnzL1gE/s144/example.png

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Haven’t seen any library for that, but looks a pretty simple thing. I’ve jot down a quick function which can help you out. I have kept it simple, you might want to use cURL to fetch the content, put some error handling, etc.

Anyway, here is my two cents:

<?php

function getLinkInfo($url)
{
    // Get target link html
    $html = file_get_contents($url);

    // Prepare the DOM document
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $dom->preserveWhiteSpace = false; 

    // Get page title
    $titles = $dom->getElementsByTagname('title');
    foreach ($titles as $title) {
        $linkTitle = $title->nodeValue;
    }

    // Get META tags
    $metas = $dom->getElementsByTagname('meta'); 

    // We only need description
    foreach ($metas as $meta) {
        if ($meta->getAttribute("name") == "description") {
            $linkDesc = $meta->getAttribute("content");
        }
    }

    // Get all images
    $imgs = $dom->getElementsByTagname('img'); 

    // Again, we need the first one only
    foreach ($imgs as $img) {
        $firstImage = $img->getAttribute("src");

        if (strpos("http://", $firstImage) === false) {
            $firstImage = $url . $firstImage; 
        }

        break;
    }

    $output = <<<HTML

    <div class="info">

        <div class="image"><img src="{$firstImage}" alt="{$linkTitle}" /></div>
        <div class="desc">
            <div class="title">{$linkTitle}</div>
            <div class="subtitle">{$url}</div>
            <div class="summary">{$linkDesc}</div>
        </div>

    </div>

HTML;

    return $output;
}

echo getLinkInfo("http://www.phpfour.com/");

Method 2

John Gruber has a regex pattern that might help:

A common programming problem:
identify the URLs in an arbitrary
string of text, where by “arbitrary”
let’s agree we mean something
unstructured such as an email message
or a tweet.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x