首页 > 解决方案 > 如何从 url 获取信息

问题描述

有没有办法通过链接(标题、副标题、详细信息、图片)获取链接信息?

就像电报或 LinkedIn 中的电报和 LinkedIn 一样,当您将一些链接用于发布或发送到频道或群组时,您可以看到该链接的一些详细信息。

我该如何为 php 代码执行此操作?

标签: phplinkedintelegram

解决方案


获取有关该链接的信息的唯一方法是爬取页面,然后从<meta property="og:..."标题中的特殊元标记 ( ) 中提取数据,然后相应地显示它。也许你需要一个现成的包可以做到这一点,或者即使使用 jQuery 你也应该能够解析它。以下是当前页面生成的标签示例:

    <meta property="og:type" content="website">
    <meta property="og:url" content="https://stackoverflow.com/questions/58146161/ability-to-get-article-link-information/58146431">
    <meta property="og:site_name" content="Stack Overflow">
    <meta property="og:image" itemprop="image primaryImageOfPage" content="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:domain" content="stackoverflow.com">
    <meta name="twitter:title" property="og:title" itemprop="name" content="Ability to get article link information">
    <meta name="twitter:description" property="og:description" itemprop="description" content="The only way to get information about that link is to crawl the page and then extract the data from special meta tags (&amp;lt;meta property=&quot;og:...&quot;) in the header, then display it accordingly.  Perha...">

    <meta name="twitter:app:country" content="US">
    <meta name="twitter:app:name:iphone" content="Stack Exchange iOS">
    <meta name="twitter:app:id:iphone" content="871299723">
    <meta name="twitter:app:url:iphone" content="se-zaphod://stackoverflow.com/questions/58146161/ability-to-get-article-link-information/58146431">
    <meta name="twitter:app:name:ipad" content="Stack Exchange iOS">
    <meta name="twitter:app:id:ipad" content="871299723">
    <meta name="twitter:app:url:ipad" content="se-zaphod://stackoverflow.com/questions/58146161/ability-to-get-article-link-information/58146431">
    <meta name="twitter:app:name:googleplay" content="Stack Exchange Android">
    <meta name="twitter:app:url:googleplay" content="http://stackoverflow.com/questions/58146161/ability-to-get-article-link-information/58146431">
    <meta name="twitter:app:id:googleplay" content="com.stackexchange.marvin">

推荐阅读