Validate a Facebook page URL

后端 未结 4 2080
广开言路
广开言路 2020-12-22 02:36

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

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 03:00

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

提交回复
热议问题