2012-10-25 29 views
4

我正在创建一个在线HTML5/Javascript测验。我通过Facebook和Twitter分享测验的结果。消息传递是通过Javascript创建的,“我刚刚得到了X条Y正确的问题”定制Google +分享文字(片段)

我想对Google Plus做同样的事情。但是,看起来“片段”是由页面上的schema.org,Open Graph或Title/Description标记生成的。这里的文档:https://developers.google.com/+/plugins/snippet/

我想我可以通过PHP中的查询字符串参数动态生成这些标签,但它看起来像Google缓存页面。有任何想法吗?

PHP:

<?php 
$correct = strval($_GET['correct']); 
$title = "I took the Quiz and answered " . $correct . " of 9 questions correctly."; 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title><?php echo($title); ?></title> 
     <meta property="og:title" content="<?php echo($title); ?>" /> 
    </head> 
</html> 

我动态创建网址.../share.php正确= 4

但是,当我分享网页,我看到消息:

I took the Quiz and answered of 9 questions correctly. 

它似乎没有显示动态数据。我假设是因为Google缓存了该页面?

谢谢

回答

3

+1按钮和共享对话框确实使用缓存。片段获取器应该遵守缓存控制指令,所以你应该能够通过提供必要的头来实现你要做的事情,以防止缓存。

在PHP中,下面的代码将标题与必要的数据,以防止缓存:

<?php 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
?> 

别的东西,你可以考虑做太多,是插入一个随机生成的ID为查询字符串参数,以便共享URL每次对于提取器来说都是唯一的。

希望有所帮助。

3

为了我所知,加一按钮缓存页面。所以做fb like按钮。 你可能想试试这个

https://plus.google.com/share?url=http://example.com/share.php?correct=4 

它有窍门。希望这可以帮助