验证Facebook页面URL

时间:2010-11-13 22:31:00

标签: php facebook

对于我正在开发的应用程序,我们允许用户添加指向其Facebook页面的链接(严格的页面,不是个人资料,不是群组,只有页面)。

我找不到任何关于验证我收到的URL(服务器端,PHP)是否实际上是有效页面URL的信息。

如果我能获得“真实的”URL信息,即减去#!如果有的话。我可以通过一些解析来管理这个,但我宁愿Facebook告诉我。

4 个答案:

答案 0 :(得分:4)

不幸的是,除非你可以只询问他们的id或用户名,否则无法解析网址。如果你不能这样做,他们会要求提供网址,解析用户名或网页ID。它可能是椰子油或122456743。 从那里使用图api:

在任何一种情况下,

http://graph.facebook.com/cocacolahttp://graph.facebook.com/122456743都会获得以下JSON:

{
   "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
}

接下来,要确定它是页面还是用户,只需检查是否存在“category”属性。每个页面都有一个类别。用户个人资料将永远不会有类别属性。

你也可以使用FQL,但这有点复杂,并不是真的需要。 http://developers.facebook.com/docs/reference/fql/page

答案 1 :(得分:2)

您可以使用图形api通过id访问对象,该ID可以是页面ID或页面名称,然后您将获得json对象或错误代码... ex https://graph.facebook.com/cocacola

答案 2 :(得分: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;    
}

答案 3 :(得分: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
    }
}