views:

55

answers:

1

Hello, I am trying to write a function to just get the users profile id or username from Facebook. They enter there url into a form then I'm trying to figure out if it's a Facebook profile page or other page. The problem is that if they enter an app page or other page that has a subdomain I would like to ignore that request.

Right now I have:

    $author_url = http://facebook.com/profile?id=12345;
            if(preg_match("/facebook/i",$author_url)){
            $parse_author_url = (parse_url($author_url));
            $parse_author_url_q = $parse_author_url['query'];
                if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
                    $fb_id = "/".$match[1];}
                else{ $fb_id = $parse_author_url['path'];
                }
            $grav_url= "http://graph.facebook.com".$fb_id."/picture?type=square";
}
echo $gav_url;

This works if $author_url has "id=" then use that as the profile id if not then it must be a user name or page name so use that instead. I need to run one more check that if the url contains facebook but is a subdomain ignore it. I belive I can do that in the first preg_match preg_match("/facebook/i",$author_url)

Thanks!

+1  A: 

To ignore facebook subdomains you can ensure that

$parse_author_url['host']

is facebook.com.

If its anything else like login.facebook.com or apps.facebook.com you need not proceed.

Alternatively you can also ensure that the URL begins with http://facebook.com as:

if(preg_match("@(?:http://)?facebook@i",$author_url)){
codaddict
Humm... I tried `if(strtolower($parse_author_url['host']) == "facebook.com")` but it online liked "facebook.com" not "www.facebook.com" I also tried the preg_match but it accepted the subdomains as valid. Thanks for the help.
BandonRandon
You can make the `www.` part optional as: `if(preg_match("@(?:http://)?(?:www\.)?facebook@i",$author_url)){`
codaddict
unfortunately that still allows "apps.facebook.com"
BandonRandon
Try using start anchor: `if(preg_match("@^(?:http://)?(?:www\.)?facebook@i",trim($author_url))){`
codaddict
I got it working by using `if(strtolower($parse_author_url['host']) == "facebook.com" || strtolower($parse_author_url['host'])== "www.facebook.com" ) {` I know that's not the quickest way but it works.
BandonRandon
Looks like start anchor and trim did the trick! Thanks!
BandonRandon