【问题标题】:php redirect url with og metatag (open graph)带有 og 元标记的 php 重定向 url(打开图)
【发布时间】:2014-04-01 08:13:22
【问题描述】:

我在 PHP redirect.php 中有服务器端重定向页面,此页面使用 PHP header() 函数重定向用户:

header( 'location: mypage.html?test' );

有没有办法添加一些 OG 元标签(Open Graph),当有人在 Facebook 和类似网站上分享 redirect.php 页面时,这些属性会被应用?

<meta property="og:title" content="Facebook should load this when shared redirect.php"/>

【问题讨论】:

    标签: php facebook redirect facebook-opengraph


    【解决方案1】:

    由于redirect.php 每次访问都会重定向,所以这是不可能的。但是,我们可以让 Facebook Debugger 读取页面 OG 元标记,如下所示

    PHP

    <?php
    if (in_array($_SERVER['HTTP_USER_AGENT'], array(
      'facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php)',
      'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
    ))) {
      header("HTTP/1.1 200 OK");
      print '<html prefix="og: http://ogp.me/ns#">
                   <head>
                      <title>{title}</title>
                      <meta property="og:title" content="{OG Title}" />
                      <meta property="og:type" content="website" />
                      <meta property="og:url" content="http://example.com" />
                      <meta property="og:image" content="http://example.com/og_img.jpg" />
                  </head>
            </html>';
    }
    else {
      // You're not Facebook agent '__' 
      header('Location: mypage.html?test');
    }
    

    参考

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 2017-01-26
      • 1970-01-01
      • 2015-07-16
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多