Created
October 20, 2013 18:43
-
-
Save supercleanse/7073570 to your computer and use it in GitHub Desktop.
Facebook app connection code for PHP / WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| define('FBAUTH_FB_OAUTH_URL', 'https://www.facebook.com/dialog/oauth'); | |
| define('FBAUTH_FB_OAUTH_TOKEN_URL', 'https://graph.facebook.com/oauth/access_token'); | |
| define('FBAUTH_FB_GRAPH_DEBUG_TOKEN_URL', 'https://graph.facebook.com/debug_token'); | |
| define('FBAUTH_FB_GRAPH_PROFILE_URL', 'https://graph.facebook.com/me'); | |
| define('FBAUTH_FB_APP_ID', 'xxxx'); | |
| define('FBAUTH_FB_APP_SECRET', 'xxxx'); | |
| function fbauth_get_fb_token($code) { | |
| global $post; | |
| $url = get_permalink($post->ID); | |
| $res = wp_remote_get( FBAUTH_FB_OAUTH_TOKEN_URL . | |
| '?client_id=' . FBAUTH_FB_APP_ID . | |
| '&redirect_uri=' . urlencode($url) . | |
| '&client_secret=' . FBAUTH_FB_APP_SECRET . | |
| '&code=' . $code ); | |
| if( strtoupper($res['response']['message']) != 'OK' ) | |
| throw new Exception( __('Error 100: There was a problem communicating with Facebook. Try again later.') ); | |
| parse_str($res['body']); | |
| if( !isset($access_token) ) | |
| throw new Exception( __('Error 200: There was a problem communicating with Facebook. Try again later.') ); | |
| setcookie('fbauth_fb_token',$access_token,(time()+(60*60*24*30))); | |
| $_COOKIE['fbauth_fb_token'] = $access_token; | |
| return $access_token; | |
| } | |
| function fbauth_get_fb_user($access_token) { | |
| $res = wp_remote_get( FBAUTH_FB_GRAPH_DEBUG_TOKEN_URL . | |
| "?input_token={$access_token}" . | |
| '&access_token='.FBAUTH_FB_APP_ID.'|'.FBAUTH_FB_APP_SECRET ); | |
| $dbg = json_decode($res['body']); | |
| if( !isset($dbg->data->is_valid) or !isset($dbg->data->user_id) or $dbg->data->is_valid==0 ) | |
| throw new Exception( __('Error 300: There was a problem communicating with Facebook. Try again later.') ); | |
| return $dbg->data->user_id; | |
| } | |
| function fbauth_get_fb_profile( $tkn ) { | |
| $res = wp_remote_get( FBAUTH_FB_GRAPH_PROFILE_URL . "?access_token={$tkn}" ); | |
| $prf = json_decode($res['body']); | |
| return $prf; | |
| } | |
| function fbauth_post_to_fb_feed( $user_id, $message ) { | |
| $fb_user_id = get_user_meta( $user_id, 'fbauth_fb_user_id', true ); | |
| //$fb_user_tkn = get_user_meta( $user_id, 'fbauth_fb_token', true ); | |
| $fb_user_tkn = FBAUTH_FB_APP_ID.'|'.FBAUTH_FB_APP_SECRET; | |
| if( $fb_user_id !== false and $fb_user_tkn !== false ) { | |
| $url = "https://graph.facebook.com/{$fb_user_id}/feed"; | |
| $args = array( 'body' => array( 'access_token' => $fb_user_tkn, | |
| 'message' => strip_tags($message) ) ); | |
| $res = wp_remote_post( $url, $args ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment