2012-07-05 33 views

回答

4

尝试与Facebook API(https://developers.facebook.com/docs/reference/php/)

$facebook->api('PAGEID or USERID here'); 

如果不存在,Facebook的回报false

样品的URL列表,请参阅https://developers.facebook.com/docs/reference/api/

+0

谢谢加布里埃尔,工作,但当页面,人员或组不存在时,我得到此错误:“致命错误:未捕获的OAuthException:(#803)您请求的某些别名不存在:hreherehere抛出第1106行的base_facebook.php“。我怎样才能压制这个,所以当我自己的错误显示$ facebook-> api('PAGEID或USERID');返回false? – RobertH

+0

@RobertH我不确定,你需要阅读文档。 –

+1

如何使用Try-Catch? – Jeff

0

您将需要facebook_id或用户/组的URL。 然后使用curl,你可以查找返回的状态码。 404意味着它不存在。

http://php.net/manual/en/book.curl.php

+0

我试过,但脸谱网有一个页面,显示“ 您找不到页面..”,并没有给出404代码。 – RobertH

2

目前,如果你访问一个不存在的Facebook页面,你得到这个消息:

The page you requested was not found. You may have clicked an expired link or mistyped the address. Some web addresses are case sensitive.

所以基本上你可以做:

$page = file_get_contents('http://www.facebook.com/no_real_page'); 
$pos = strrpos($page, 'The page you requested was not found'); 
if ($pos === true) { 
    // non existing page! 
} 

但是有所不同,消息可能会改变,然后您将不会得到该脚本的结果。所以,最好把它在一个恒定的地方,你可以很容易地在以后更改:

# config.php 
define (FACEBOOK_ERROR, 'The page you requested was not found'); 

# script.php 
$page = file_get_contents('http://www.facebook.com/no_real_page'); 
$pos = strrpos($page, FACEBOOK_ERROR); 
if ($pos === true) { 
    // non existing page! 
} 
+0

Facebook的错误页面是不是本地化? – Jeff

+0

您可以随时创建一个数组并使用foreach检查每个值。 – Peon

+0

不应该检查HTTP代码404吗?这是更可靠 –

2

的Facebook不支持file_get_contents或者cUrlDainis Abols说。

该代码对其他网站检查存在或不存在有用。但Facebook与其他网站有很大不同。

<?php 
$page = file_get_contents('http://www.facebook.com/pages/Studentmug/349363763205'); 
$pos = strrpos($page, 'The page you requested was not found'); 
echo "$page"; 
if ($pos === true) { 
    // non existing page! 
} 
?> 
+1

欢迎来到StackOverflow Radhakrishna Chowdary!很高兴看到你第一次帮助人们=) –

-2

好感谢加布里埃尔·桑托斯和杰夫,这是我结束了:再次

// A function to check if a facebook page exists 
// and if its a personal or published page 
function CheckFB($fbexist) { // $fbexist is the pageID or userID 
    require_once("include/facebook.php"); // Facebook php-sdk 

       // Install and Initialize 
    $config = array(); 
    $config['appId'] = 'APP_ID/KEY'; 
    $config['secret'] = 'APP_SECRET'; 

      // Create Facebook object 
    $facebook = new Facebook($config); 

    try { 
     $facebook->api($fbexist); 
     if (!$facebook->api($fbexist)) { // if this returns false 
      $personal = "No";  // it's not a personal page 
     } else {       // or group 
      $personal = "Yes"; 
     } 

    } catch(FacebookApiException $e) {   // If there is an exception 
     $checked = array(false, $personal);// the page doesn't exist 
     return $checked; 
    } 
      $checked = array(true, $personal); 
    return $checked; 
} 

感谢您的帮助球员:)

+1

为什么你不接受我的答案?我的回答正好说明你想要解决你的问题。 -1这种态度。 –

+0

好吧,我没有完全使用你的答案,我用它的一部分。所以不需要给我-1我现在接受了你的答案 – RobertH

相关问题