phpfacebookfacebook-graph-api

graph.facebook.com for Facebook Pages


I am currently doing an internship at an internet company, and they would like me to create a 'brand checker', so that visitors can check if their brandname is taken on popular websites like Facebook, Twitter, LinkedIn, and the likes.

I have found that by going to http://graph.facebook.com/, you can see if a person/account exists on Facebook. I am currently trying to find a way to check if a page exists on Facebook. I don't need details like posts or whatever, I just need to find out if it exists or not.

So far I've tried the code below (both the commented code and the uncommented code), and it doesn't work at all.

<?php
/*$z = file_get_contents("http://www.facebook.com/search/results/?q=example");
preg_match("#LifeStamp#s", $z, $matches);
echo $matches[1];
if($matches == 1){
    echo "In there!";
    } elseif($matches == 0) {
    echo "No";
    } elseif($matches == "false") {
    echo "Nope";
    } else {
    echo "Nopenopenope";
    };*/
$ch = curl_init(); 
$timeout = 0; 
curl_setopt ($ch, CURLOPT_URL, 'http://www.facebook.com/search/results/?q=example'); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
$file_contents = curl_exec($ch); 
curl_close($ch); 

var_dump($file_contents);
echo $file_contents[1];
?>

The file_get_contents() gives an error (so does url_get_contents() by the way). The curl-part just returns a blank page.

TL;DR: Is there a (preferrably easy) way for me to check if a Facebook page exists or not? And if you guys happen to know, same for Twitter and LinkedIn.

Thanks in advance!

-Sean


Solution

  • You can use the endpoint

    https://graph.facebook.com/?id={url}
    

    as shown in https://developers.facebook.com/docs/graph-api/reference/v2.2/url/ to check whether there's an object_id for that URL. An URL can also be a Facebook Page link, for example

    https://graph.facebook.com/?id=https://www.facebook.com/fgshdioghsdlfghsldfgkl
    

    The response for non-taken URLs (-> Facebook Pages) is

    {
       "id": "https://www.facebook.com/fgshdioghsdlfghsldfgkl"
    }
    

    in contrary to Facebook Pages which are taken:

    https://graph.facebook.com/?id=https://www.facebook.com/cocacola
    

    whcih returns

    {
       "id": "40796308305",
       "about": "The Coca-Cola Facebook Page is a collection of your stories showing how people from around the world have helped make Coke into what it is today.",
       "can_post": false,
       "category": "Food/beverages",
       "checkins": 13624,
       "cover": {
          "cover_id": "10152297032458306",
          "offset_x": 0,
          "offset_y": 0,
          "source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-prn2/v/t1.0-9/s720x720/625442_10152297032458306_574021701_n.jpg?oh=4bcbc7e195383a2c41c99f2d9a76a41b&oe=54FFAEE9&__gda__=1426811392_edb8ee0782ce0143ba26abb724e7bc82",
          "id": "10152297032458306"
       },
       "description": "Created in 1886 in Atlanta, Georgia, by Dr. John S. Pemberton, Coca-Cola was first offered as a fountain beverage at Jacob's Pharmacy by mixing Coca-Cola syrup with carbonated water. \n\nCoca-Cola was 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.\n\nCoca-Cola Page House Rules: http://CokeURL.com/q28a",
       "founded": "1886",
       "has_added_app": false,
       "is_community_page": false,
       "is_published": true,
       "likes": 91176186,
       "link": "https://www.facebook.com/coca-cola",
       "name": "Coca-Cola",
       "parking": {
          "lot": 0,
          "street": 0,
          "valet": 0
       },
       "talking_about_count": 1306671,
       "username": "coca-cola",
       "website": "http://www.coca-cola.com",
       "were_here_count": 0
    }