带有og元标记的php重定向url(开放图形)

带有og元标记的php重定向url(开放图形),php,facebook,redirect,facebook-opengraph,Php,Facebook,Redirect,Facebook Opengraph,我在PHPredirect.PHP中有服务器端重定向页面。PHP此页面使用PHPheader()函数重定向用户: header( 'location: mypage.html?test' ); 有没有一种方法可以添加一些OG元标记(Open Graph),这样当有人在Facebook和类似网站上共享redirect.php页面时,就会应用这些属性 <meta property="og:title" content="Facebook should load this when share

我在PHP
redirect.PHP中有服务器端重定向页面。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"/>

由于
redirect.php
在任何时候被访问或执行时都会重定向,这是不可能的,但是通过一个简单的
if
语句,我们可以允许Facebook调试器读取页面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');
}