有没有模仿Facebook的&QUOT库;链路的检测"? [英] Is there a library that emulates facebook's "Link Detect"?

查看:116
本文介绍了有没有模仿Facebook的&QUOT库;链路的检测"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待写道:分析,当你发布链接Facebook这样的信息做了图书馆。然而,由于我不想推倒重来没有人知道一个库或和精力来写,做一个图书馆这个了吗?

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.

推荐答案

还没有看到任何库,但看起来pretty简单的事情。我已经记下一个快速的功能,它可以帮助你。我一直简单,你可能要使用卷曲来获取内容,把一些错误处理,等等。

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/");

这篇关于有没有模仿Facebook的&QUOT库;链路的检测&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆