2010-11-13 83 views
0

對於我正在開發的應用程序,我們允許用戶添加一個鏈接到他們的Facebook頁面(嚴格來說,不是簡介,而不是組,只有一頁)。驗證Facebook頁面的URL

我找不到任何關於驗證我收到的URL(服務器端,PHP)是否實際上是一個有效的頁面URL。

更好的是,如果我能得到「真實」的URL信息,即減去#!如果有的話。我可以用一些解析來解決這個問題,但我寧願Facebook告訴我。

+1

你是什麼意思的「頁面」不來的文本字段不屬「個人資料」? – 2010-11-13 22:35:38

+1

我使用頁面活動小部件,它只適用於頁面。按個人資料我是指個人資料,即人。一個頁面更公開,默認情況下爲所有人打開。 – StrangeElement 2010-11-14 03:41:00

回答

1

我也有類似的任務,首先我想問的Facebook的規範化URL(不含#,等等!):

require_once('facebook.php'); 
function getNormalized($anylink){ 
$facebook = new Facebook(array(
    'appId' => 'yourapplicationID', 
    'secret' => 'yoursecretcode', 
    'cookie' => true, 
)); 
$res = $facebook->api(array(
    'method' => 'fql.query', 
    'query' => 'select url, normalized_url from link_stat where url = "'.$anylink.'" ;' 
)); 
     if (!isset($res[0]['normalized_url'])) 
     { 
     //If not isset -> set with dumy value, example 
     echo '<br>You have not entered Facebook Page URL or its not accessible. Please go back and try again.'; 
     exit; 
     } 

return $res;  
} 
+0

不錯!我只需驗證規範化的URL是一個頁面(即包含/ pages /,因爲它也可以與組一起工作),並且它完美地工作! – StrangeElement 2011-07-22 18:55:34

2

可以使用圖形API通過ID其中要麼是THA頁面ID頁面名稱來訪問對象,那麼你得到一個JSON對象或錯誤代碼......恩https://graph.facebook.com/cocacola

+0

問題是我可以以多種形式獲取URL。它可以有一個永久的URL(facebook.com/cocacola),它可以是一個帶有附加ID的名稱的網址友好版本(Facebook/Coca-Cola/122456743),它可以使用散列參數(Facebook。 COM/home.php?/#!可口可樂/ 123457407)。我可以解析所有這些情況,但我寧願讓它通過Facebook進行驗證,確實它是一個頁面,而不用自己解析。最糟糕的情況是我會解析,但我更喜歡Facebook可以確認的方式。 – StrangeElement 2010-11-14 03:44:26

+0

theres沒有這樣的方式。 – 2010-11-14 18:02:33

+0

然而,你可以請求網址,並看看返回的文件,但你將不得不按照JavaScript重定向的東西。 – 2010-11-14 18:03:27

4

不幸的是,沒有辦法解析url,除非你可以問他們只是爲了id或用戶名。如果你不能這樣做,他們要求的網址,解析出用戶名或頁面ID。這要麼是可口可樂或122456743. 從那裏使用圖形API:在任何情況下,你會得到下面的JSON回來

http://graph.facebook.com/cocacolahttp://graph.facebook.com/122456743

{ 
    "id": "40796308305", 
    "name": "Coca-Cola", 
    "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs236.ash2/50516_40796308305_7651_s.jpg", 
    "link": "http://www.facebook.com/coca-cola", 
    "category": "Consumer_products", 
    "website": "http://www.coca-cola.com", 
    "username": "coca-cola", 
    "products": "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known product in the world.\n\nCreated in Atlanta, Georgia, by Dr. John S. Pemberton, Coca-Cola was first offered as a fountain beverage by mixing Coca-Cola syrup with carbonated water. Coca-Cola was introduced in 1886, patented in 1887, registered as a trademark in 1893 and by 1895 it was being sold in every state and territory in the United States. In 1899, The Coca-Cola Company began franchised bottling operations in the United States.\n\nCoca-Cola might owe its origins to the United States, but its popularity has made it truly universal. Today, you can find Coca-Cola in virtually every part of the world.", 
    "fan_count": 17367199 
} 

接下來,以確定它是否是一個頁面或者用戶只是檢查「類別」屬性的存在。每一頁都會有一個類別。用戶配置文件永遠不會擁有類別屬性。

你也可以使用FQL,但這有點複雜,並不是真的需要。 http://developers.facebook.com/docs/reference/fql/page

+0

對於某些頁面,您的登錄狀態似乎會影響結果。例如http://graph.facebook.com/150583234999168給我一個鏈接的不同結果,如果我登錄到Facebook與未登錄。 – Bemmu 2011-06-18 15:19:59

+0

使用graph.facebook.com/id給出此錯誤http:///prntscr.com/d1s70m – 2016-11-01 17:50:15

0

我需要的東西,檢查,看看它是否是一個有效的頁面或組。我的用戶提交其通過$_POST$_POST['mu_facebook']

if(class_exists('Facebook')){ 
     $facebook = new Facebook(array(
     'appId' => 'yourapplicationID', 
     'secret' => 'yoursecretcode', 
     'cookie' => true, 
     )); 

     //break up the saved URL to see what we're dealing with 
    $parts = explode('/', $_POST['mu_facebook']); 
    if($key = array_search('groups', $parts)) 
     $query = $parts[$key+1]; //if this is a group URL, the ID will be in the next key after /group/ 
    else 
     $query = $_POST['mu_facebook']; 

    try { 
     $fb_data = $facebook->api($query); 
    } catch (FacebookApiException $e){ 
     //insert your own error here - Facebook did not recognize this object (group id failures will happen here) 
    } 
    //If FB doesn't recognize the URL, the API simply shows the requested URL as the id or returns an object wth type = 'website' (Page URL failures will happen here) 
    if($fb_data["id"] && $fb_data["id"] !== $_POST['mu_facebook'] && $fb_data["type"] !== "website"){ 
     //We have a valid URL 
     update_as_site_option('mu_facebook'); 
    } else { 
     //insert your own error here - Facebook did not recognize this object 
    } 
}