How Facebook link preview happens?

后端 未结 4 1664
小鲜肉
小鲜肉 2021-02-01 17:47

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 som

4条回答
  •  故里飘歌
    2021-02-01 18:05

    Surprisingly there aren’t that many services out there doing this. As the previous commenter posted, your browser can’t do this without a server element.

    So, the workflow is really this:

    1. Parse the inserted text to test it’s a valid URL
    2. Whiz that URL value via AJAX to a bespoke service or roll your own.
    3. The service (mine is written in .Net) loads up the page in memory and parses it with an DOM parser. I use the HTML Agility pack you could use AngleSharp etc.
    4. You need to extract the Open graph meta elements with (typically) fall back to standard Meta Title/ Description et all.
    5. Pack up all that OG goodness and return this model via AJAX
    6. Put the returned values on screen as a preview.
    7. If accepted write the logic to insert the values into your DB

    If you are not up to creating the server object, there are third-party services that can do the retrieval bits:

    http://unfurl.oroboro.com/

    https://guteurls.de/

    best of luck.

提交回复
热议问题