Facebook链接预览如何发生? [英] How Facebook link preview happens?

查看:368
本文介绍了Facebook链接预览如何发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在Facebook中键入URL时,它会自动显示发布链接功能。对于大多数网站,它会自动加载从页面某处拉出的一系列图像,以与链接和描述一起使用。有没有人知道如何为我们自己的网站实现相同的功能,没有任何FB API等..只是javascript或JQuery?

When you type a URL into facebook, it automatically comes up with the "post a link" feature. With most websites, it automatically loads a selection of images it pulls from somewhere in the page to go alongside the link and description. does anyone have an idea of how to implement that same feature for our own site without any FB API etc.. just javascript or JQuery ?

推荐答案

由于安全限制,JavaScript无法从任何服务器加载任何页面。 Facebook实际上要求 Facebook服务器获取有关该页面的信息,然后服务器依次查询页面以提取信息。您也需要服务器端实现此功能。

Due to security restrictions, JavaScript cannot load any page from any server. Facebook actually asks the Facebook servers for information about that page, and the servers in turn query the page to extract the information. You, too, will need a server-side implementation of this.

除此之外,它非常简单:使用HTTP客户端库为您选择的语言连接到提供的URL,然后使用HTML解析库提取 title 标记,识别正文中的重要文本标记,并提取似乎最合适的 img 标记源。

Aside from that, it's fairly simple: use an HTTP client library for your language of choice to connect to the provided URL, then use an HTML parsing library to extract the title tag, identify a significant piece of text in the body tag, and extract the img tag sources that seem the most appropriate.

一旦你的服务器能够提取有关页面的信息,通过AJAX调用它是一个相当简单的练习。

Once your server is able to extract information about a page, it's a fairly simple exercise to call it through AJAX.

这篇关于Facebook链接预览如何发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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