Facebook Open Graph,未提供类型为“string"的必需属性“og:title"

2022-01-06 00:00:00 php html facebook facebook-graph-api joomla

我有一个 Joomla 页面 (v3.2.4),我在其中使用 PHP 放入了一些动态 Open Graph 标签,如下所示:

I have a Joomla-page (v3.2.4) where I put in some dynamic Open Graph tags with PHP, like this:

标签前的 PHP:

$getcid = JRequest::getVar('id');

if(!isset($getcid)) {
    $title = "LIVA Kurser";
    $description = "Danmarks største udbyder af kurser til såvel offentlige og erhverv samt private.";
    $image = "http://www.livakursertestsite.dk/images/liva-logo.jpg";
    $type = "website";
} else {
    $db = JFactory::getDbo();
    $user    = JFactory::getUser();
    $query = $db->getQuery(true);
    $ogquery = "SELECT DISTINCT * FROM jos_managecourse WHERE state = '1' AND id = '".$getcid."' LIMIT 1";
    $db->setQuery($ogquery);
    $db->query();
    $getcourse = $db->loadObjectList();  
      
    $description = substr(strip_tags($getcourse[0]->details), 0, 247);
    $title = $getcourse[0]->course_name;
      
    $description = preg_replace( "/|
/", "", $description );
    $description = str_replace( " ", " ", $description );
      
    $image = JURI::root()."administrator/components/com_managecourse/images/".$getcourse[0]->image_url;
    $type = "article";
}

在我的标签中,我有这个:

And in my tag I have this:

  <!-- Facebook Open Graph -->
  <meta property="fb:app_id" content="502033806595590" />
  <meta property="og:site_name" content="LIVA Kurser" />
  <meta property="og:type" content="<?php echo $type; ?>" />
  <meta property="og:title" content="<?php echo $title; ?>" />
  <meta property="og:url" content="<?php echo JURI::current(); ?>" />
  <meta property="og:image" content="<?php echo $image; ?>" />
  <meta property="og:description" content="<?php echo $description; ?>..." />
  <!-- End Facebook Open Graph -->

当我通过 Facebook 对象调试器运行页面时,我得到以下两个错误:

When I run the page through the Facebook Object Debugger, I get the following two errors:

位于 URL 'http://www.livakursertestsite.dk/kurser/babytegn-2' 的对象类型 'website' 无效,因为必需的属性 'og:title'未提供类型字符串".

Object at URL 'http://www.livakursertestsite.dk/kurser/babytegn-2' of type 'website' is invalid because a required property 'og:title' of type 'string' was not provided.

第二个错误:

卷曲错误:处理内容时出现 BAD_CONTENT_ENCODING 错误取消编码:无效的块类型

Curl Error : BAD_CONTENT_ENCODING Error while processing content unencoding: invalid block type

我试过了:

  • 将代码移动到标题标签下方和上方
  • 将其移动到头部标签的最顶部和最底部,但是两者都不起作用.
  • 将 PHP 代码放在 head-tag 中,但仍然没有.

您可以在此处查看该站点:http://www.livakursertestsite.dk/kurser/babytegn-2

You can see the site here: http://www.livakursertestsite.dk/kurser/babytegn-2

我已经从标签中删除了 PHP 代码,所以它们不再是动态的,看看这是否是原因,但事实并非如此.现在代码看起来像这样:

I have removed the PHP-code from the tags, so they are no longer dynamic, to see if that was the cause, but it wasn't. Now the code looks like this:

<meta property="og:locale" content="da_DK" />
<meta property="og:type" content="website" />
<meta property="og:title" content="LIVA Kurser" />
<meta property="og:description" content="Beskrivelse..." />
<meta property="og:url" content="http://www.livakursertestsite.dk/kurser" />
<meta property="og:image" content="http://www.livakursertestsite.dk/images/liva-logo.jpg" />
<meta property="fb:app_id" content="502033806595590" />
<meta property="og:site_name" content="LIVA Kurser" />

我现在也试过了:

  • 在 Google 的 Rich Snippets 测试工具上测试网站,以及Google 获取所有数据.
  • 删除了所有非强制性的开放图谱标签,因此只存在四个强制性标签(类型、标题、网址和图片).
  • 删除所有导致 Firebug 错误的 javascript.
  • Testing the site on Googles Rich Snippets testing tool, and Google gets all the data.
  • Removing all not-obligatory Open Graph-tags, so only the four obligatory tags (type, title, url and image) was present.
  • Removing all javascript which caused errors in Firebug.

Facebook Debugger 仍然无法获取数据.

And the Facebook Debugger still can't get the data.

编辑 #2:

在@CBroe 发表评论后,我修复了页面,因此可以对其进行验证,没有错误,但仍然没有运气.

After the comment by @CBroe I fixed the page, so it could be validated, with no errors, but still no luck.

推荐答案

您可能想尝试禁用任何类型的压缩,例如 PHP 中的 GZIP 压缩.这帮助我解决了 Joomla 3.3 网站中的类似问题.

You might want to try to disable any sort of compression, for example GZIP compression in PHP. This helped me with a similar problem in a Joomla 3.3 website.

我启用了 GZIP 压缩,但 Facebook 无法抓取我的网站.禁用 GZIP 压缩解决了这个问题.

I had GZIP compression enabled and Facebook was unable to scrape my website. Disabling the GZIP compression solved that issue.

我还没有弄清楚是 Joomla 问题还是 GZIP/PHP 问题,还是 Facebook 抓取问题.

I have not figured out yet if it's a Joomla problem or a GZIP/PHP problem in general or a facebook scrape problem.

通过分享操作,Facebook 只会读取您页面的前 40k.开启GZIP后,只读取前40k会出现问题,无法解压部分内容.

With a share action, Facebook only reads the first 40k of your page. When GZIP is enabled, reading only the first 40k will give problems as it is unable to decompress the partial contents.

更好的解决方案是仅对 Facebook、LinkedIn 等网站禁用 GZIP.

A better solution would be to disable GZIP only for sites like Facebook, LinkedIn, etc.

可以使用名为gzip的小插件非常友好控制:http://extensions.joomla.org/extensions/core-enhancements/performance/site-performance/27725

You can use the small plugin called gzip very friendly Control: http://extensions.joomla.org/extensions/core-enhancements/performance/site-performance/27725

或者使用这个代码https://github.com/dgt41/(压缩xml和php文件您可以将其安装为插件)

Or use this codee https://github.com/dgt41/ (zip the xml and php file and you can install it as a plugin)

请注意:如果您的站点上安装了 JCH Optimize 模块,则需要禁用该插件提供的 gzip 压缩.所有其他选项都可以保持激活状态,没有任何问题,包括本机 Joomla gzip 压缩.

Please note: If you have JCH Optimize module installed on your site you will need to disable gzip compression offered by the plugin. All other options can remain activated without any problems, including a native Joomla gzip compression.

干杯,提乌斯

相关文章