2017-05-29 56 views
-1

我想要做一個營銷活動,每次用戶分享特定頁面時,它會顯示不同的標題,圖片和說明。問題是Facebook將這些數據緩存了24小時。如何製作Facebook Open Graph更改圖片和每個分享的標題

我試圖使用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

+0

你願意指定爲什麼不合理? –

+0

這是一個經常被問到的問題,可能是爲什麼你被低估了。搜索,你會發現不少答案。繼承人之一,FB緩存基於唯一的URL,強制刷新,你可以添加一個隨機查詢字符串的URL。您可以編寫一些JavaScript,在網站上點擊共享按鈕時,將隨機字符串附加到網址中。沒有辦法做到這一點(即時知道),如果你的用戶是複製粘貼你自己的網址。乾杯! –

+0

@JonChurch,調試這個網站,例如:f-u-t-b-o-l.com –

回答

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; 
+0

嗨!你能解決這個問題嗎?我相信克朗的最短時間是1分鐘吧? –