如何使Facebook Open Graph在每个共享上更改图像和标题

时间:2017-05-29 19:22:55

标签: facebook facebook-graph-api

我想做一个营销活动,每当用户分享特定页面时,它就会显示不同的标题,图像和描述。问题是Facebook将此数据缓存24小时。

我尝试使用Facebook API将Curl发送到Facebook调试器以强制它在加载HTML之前刷新,但没有成功。 https://gist.github.com/FrostyX/81d58222d1e835e24013

<?php
  require_once('FacebookDebugger.php');
  $fb = new FacebookDebugger();
  $fb->reload('http://example.com/'); // this page
?>
<html>
<head>
<?php
  $var = array( 
  "first" => array(
              "title" => "First test",
              "image" => "https://example.com/1.jpg"
              ), 
  "second" => array(
              "title" => "Second test",
              "image" => "https://example.com/2.jpg"
              ), 
  "third" => array(
              "title" => "Third test",
              "image" => "https://example.com/3.jpg"
              ), 
  );

  $section = array_rand($var); // choose a random item on the array above
?>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<meta name="twitter:card" value="summary"> 
<meta property="og:title" content="<?php echo $var[$section]['title']; ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.com" />
<meta property="og:image" content="<?php echo $var[$section]['image']; ?>"  />

我已经看过几个网页。我怎样才能做到这一点?

示例:

https://developers.facebook.com/tools/debug/sharing/?q=marvelfly.com https://developers.facebook.com/tools/debug/sharing/?q=f-u-t-b-o-l.com

1 个答案:

答案 0 :(得分:0)

我是用cron做的。

HTML:

<?php
  $var = array( 
  "um" => array(
              "titulo" => "Produto 1",
              "image" => "http://tudosobrecachorros.com.br/wp-content/uploads/cachorros-pequenos.jpg"
              ), 
  "dois" => array(
              "titulo" => "Produto 2",
              "image" => "http://tudosobrecachorros.com.br/wp-content/uploads/precos-de-cachorro.png"
              ), 
  "tres" => array(
              "titulo" => "Produto 3",
              "image" => "http://tudosobrecachorros.com.br/wp-content/uploads/2013/09/nomes-para-cachorros.jpeg"
              ), 
  );

  $section = array_rand($var); //here yoy get random first of array(green or red or yellow)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<meta name="twitter:card" value="summary">
<meta property="og:title" content="<?php echo $var[$section]['titulo']; ?>" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://YOUR-URL-HERE.COM/" />
<meta property="og:image" content="<?php echo $var[$section]['image']; ?>"  />
<meta property="og:description" content="Somente um teste"/>
<script src="https://code.jquery.com/jquery-1.7.1.min.js" integrity="sha256-iBcUE/x23aI6syuqF7EeT/+JFBxjPs5zeFJEXxumwb0=" crossorigin="anonymous"></script>
<title>Somente um teste</title>
</head>
<body>
</body>
</html>

Refresh.php(应该每隔几秒触发一次cron)

<?php
    $url = 'https://graph.facebook.com';
    $fields = array(
                'id'=>urlencode('http://YOUR-URL-HERE.COM'),
                'scrape'=>urlencode(true)
            );

    //url-ify the data for the POST
    $fields_string = '';
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    $fields_string = rtrim($fields_string,'&');

    //echo $fields_string;exit;

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    //execute post
    $result = curl_exec($ch);
    print $result;
?>

crontab -e

* * * * * for i in {0..59}; do curl http://YOUR-URL-HERE.com/refresh.php && sleep 10; done;