2016-09-09 43 views
10

我有一個iOS應用程序用於與Facebook共享鏈接的公共庫。這些鏈接指向一個域,其中包含一個相對簡單的PHP腳本,該腳本根據鏈接的內容(編目項目,日曆事件和用戶生成的列表)重定向到三個不同的目標域。我有這樣設置,因爲我使用的是iOS通用鏈接,並且我無法控制所有鏈接目標,所以我需要一個蘋果應用程序站點關聯文件的中央位置。Facebook有時無法獲取Open Graph標籤

在這個PHP腳本中,我試圖根據共享內容的類型動態地設置OG標籤。這裏的腳本:

<?php 

$shareType = $_GET['t']; 
$contentId = $_GET['id']; 

$base_catalog_url='XXXXXXXXXXXX'; 
$base_list_url='XXXXXXXXXXXXX'; 
$base_event_url='XXXXXXXXXXXXXX'; 

if($shareType=='0'){ 
    $oclc; 
    if(strlen($contentId)==8){ 
     $oclc = 'ocm'.$contentId; 
    } 

    if(strlen($contentId)==9){ 
     $oclc = 'ocn'.$contentId; 
    } 

    $url = $base_catalog_url.'searchCatalog?'.http_build_query(array('clientID' =>'sdIPhoneApp','term1'=>$oclc)); 
    $resp = simplexml_load_file($url); 

    $pageTitle = $resp->HitlistTitleInfo->title; 
    $isbn = $resp->HitlistTitleInfo->ISBN; 
    $imageURL = 'http://www.syndetics.com/index.aspx?isbn='.$isbn.'/lc.gif&client=XXXXXXX'; 
    $redirectURL = 'XXXXXXXXXXXX'.$contentId; 
    error_log($redirectURL); 
    echo '<html> 
     <head> 
      <meta property="og:image" content="'.$imageURL.'" /> 
      <meta property="og:title" content="'.$pageTitle.'" /> 
      <meta name="twitter:card" content="summary" /> 
      <meta name="twitter:site" content="@acpl" /> 
      <meta name="twitter:title" content="'.$pageTitle.'" /> 
      <meta name="twitter:description" content="Allen County Public Library" /> 
      <meta name="twitter:image" content="'.$imageURL.'" /> 
      <meta http-equiv="refresh" content="0;URL='.$redirectURL.'"> 
     </head> 
    </html>'; 
} 

if($shareType=='1'){ 
    $url = $base_event_url.http_build_query(array('eventid' =>$contentId)); 
     $response = file_get_contents($url); 
    $json = json_decode($response); 
    $event = $json[0]; 
    $imageURL = $event->Image; 
    $pageTitle = $event->Title; 
    $description = $event->Description; 

    if(strlen($imageURL)<5){ 
     $imageURL = 'https://XXXXXXXXX/appIcon200.png'; 
    } 

    $redirectURL = 'XXXXXXXXXXX'.$contentId; 

    echo '<html> 
     <head> 
      <meta property="og:image" content="'.$imageURL.'" /> 
      <meta property="og:title" content="'.$pageTitle.'" /> 
      <meta property="og:description" content="'.$description.'" /> 
      <meta name="twitter:card" content="summary" /> 
      <meta name="twitter:site" content="@acpl" /> 
      <meta name="twitter:title" content="'.$pageTitle.'" /> 
      <meta name="twitter:description" content="'.$description.'" /> 
      <meta name="twitter:text:description" content="'.$description.'" /> 
      <meta name="twitter:image" content="'.$imageURL.'" /> 
      <meta http-equiv="refresh" content="0;URL='.$redirectURL.'"> 
     </head> 
    </html>'; 

} 

if($shareType=='2'){ 

    $url = $base_list_url.http_build_query(array('listId' =>$contentId,'userKey'=>0)); 
    $response = file_get_contents($url); 
    $json = json_decode($response); 
    $imageURL = $json->coverImageURL; 
    $pageTitle = $json->listName; 
    $pageTitle = ucwords(strtolower($pageTitle)); 

    $redirectURL = "XXXXXXXXXXXX"; 

    echo '<html> 
     <head> 
      <meta property="og:image" content="'.$imageURL.'" /> 
      <meta property="og:title" content="'.$pageTitle.'" /> 
      <meta name="twitter:card" content="summary" /> 
      <meta name="twitter:site" content="@acpl" /> 
      <meta name="twitter:title" content="'.$pageTitle.'" /> 
      <meta name="twitter:description" content="Allen County Public Library" /> 
      <meta name="twitter:image" content="'.$imageURL.'" /> 
      <meta http-equiv="refresh" content="0;URL='.$redirectURL.'"> 
     </head> 
    </html>'; 

} 

?> 

因此,基於對被分享的內容的類型,我取一個頁面標題和圖像中的OG標籤來提供。不管Facebook是否拉入標籤,重定向總是有效的,但標籤只用了大約一半的時間。你可以在iOS應用中看到這個。標籤拉到成功:

enter image description here

標籤不拉:

enter image description here

這似乎是隨機的標籤是否顯示某個商品。在我的服務器上的訪問日誌,當成功地顯示了標籤,我看到這樣一行:

66.220.158.119 - - [09/Sep/2016:09:54:50 -0400] "GET /share.php?t=1&id=76137 HTTP/1.1" 206 3771 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" 

然而,當不顯示標籤,有沒有在訪問日誌或錯誤日誌。這表明Facebook(或iOS中的Facebook組件)在這些情況下甚至沒有試圖讀取標籤。這是否意味着Facebook錯誤地認爲它已經緩存了這些數據?

另一個有趣的花絮是當我嘗試在Facebook共享調試器(https://developers.facebook.com/tools/debug/)上調試這些失敗的URL之一時發生的情況。我會收到一條錯誤消息:

The 'og:image' property should be explicitly provided, even if a value can be inferred from other tags. 

而當我點擊「查看我們的刮板看到您的網址是什麼」。我得到的迴應是「文檔沒有返回數據」。

有趣的是,當我點擊「Scrape again」時,通常會在前幾次出現相同的錯誤,然後在3次或4次嘗試後突然出現並顯示標籤。我首先想到的是,這與我如何動態獲取標籤的內容有關,但正如我上面提到的,在沒有顯示標籤的情況下,訪問日誌顯示Facebook甚至不是從我的服務器請求任何東西

感謝您的幫助;這讓我拉我的頭髮!

UPDATE:這裏,如果你想嘗試一下在Facebook的調試器,如果你想一個例子網址:https://amshare.acpl.lib.in.us/0_930144011

下劃線後的數字是本書的OCLC數,所以你可以在那裏插入其他值。正如我所提到的,經過幾次擦傷後,它通常開始工作,然後再次無法再次工作等。

+0

是基本URL(不含查詢參數)總是'share.php'?我不確定在確定要緩存什麼時,Facebook是否考慮附加參數,這可能會導致這種奇怪。 –

+0

是的,它總是share.php。我想我可以爲每個共享類型(shareItem.php,shareEvent.php)使用不同的腳本,但它似乎會遭受同樣的基本問題。 –

+0

我想你可能需要爲每個鏈接使用唯一的URL。如果您查看網址縮短符(Bit.ly,Goo.gl等)和其他依賴於URL跟蹤或動態路由(HubSpot,HootSuite)的服務,他們通常會這樣做。在[Branch.io](https://branch.io),我們幾乎完全按照您上面所描述的內容進行操作,並且每個鏈接始終帶有唯一的網址 –

回答

1

我可能有可能facebook緩存share.php文件並忽略GET Vars。

您可以嘗試將網址重寫爲「漂亮的固定鏈接」。把這個在您的htaccess文件(如果你有阿帕奇):

Options +FollowSymLinks 
RewriteEngine On 

RewriteRule ^share/(.*)/(.*)$ share.php?t=$1&id=$2 [L,NC] 

這使得出的http://your-url.com/share/4/yeah這樣的:http://your-url.com/?t=4&id=yeah

的$ _GET變種看起來是這樣的:

Array ([t] => 4 [id] => yeah) 

有了這個,你可以解決這個問題(如果它是真正的緩存)。過去我在Facebook刮板上遇到了很多問題。有時它會忽略瓦爾和它像地獄般的緩存...

+0

進行此更改後,我看到相同的行爲。另外,我會說這幾乎肯定是某種緩存問題。正如我上面提到的,在標籤不起作用的情況下,訪問日誌中絕對沒有任何東西。因此,在這些情況下,Facebook不會向我的服務器請求任何內容。 –

+0

您是否嘗試過使用相同的sharer.php網址添加og:url meta標記? (或重寫,如果你插入我的代碼段)?它告訴FB刮在它的URL - 可能它可以解決你的緩存問題? –

+0

我試過這個;它會導致相同的行爲。當我告訴它在調試器上「再次刮擦」時,它看起來完全是隨機的,不管它是否工作。 –

0

嘗試添加一些標題到您的響應,以防止緩存。

緩存控制:無緩存,無店鋪,必重新驗證 雜注:無緩存過期 :0

相關問題