Validate a Facebook page URL
For an application I am developing, we allow users to add a link to their Facebook Page (strictly page, not Profile, not Group, only a Page).
I cannot find anything about validating whether the URL I received (server-side, PHP) is actually a valid Page URL.
Even better if I can get the "true" URL information, i.e. minus the #! if there is one. I can manage this with some parsing but I'd rather Face开发者_Python百科book tell me.
Unfortunately, there is no way around parsing the url unless you can ask them just for the id or username. If you cannot do that, they ask for the url, parse out either the username or page id. It would either be cocacola or 122456743. From there use the graph api:
http://graph.facebook.com/cocacola or http://graph.facebook.com/122456743 in either case you will get the following JSON back:
{
"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
}
Next, to determine if it is a page or a user just check for the presence of the "category" property. Every page will have a category. User profiles will never have the category property.
You could also use FQL, but that is a bit more complicated and not really needed. http://developers.facebook.com/docs/reference/fql/page
you can use the graph api to access the object by id which an either be tha page id or the page name, then you get a json object or an error code ... ex https://graph.facebook.com/cocacola
I have the similar task, first I ask Facebook for normalized URL (without !#, etc):
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;
}
I needed something that checked to see if it was a valid Page or Group. My user submits a text field which comes via $_POST
as $_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
}
}
精彩评论