[
  {
    "path": "LICENSE.md",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 Sohaib Ilyas (Roomi)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "This repo has been archived, visit https://github.com/sohaibilyas/facebook-php-sdk for latest Facebook PHP SDK with examples.\n"
  },
  {
    "path": "add-page-tab.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://sohaibilyas.com/APP_DIR/');\n\n$permissions = ['manage_pages']; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t$pages = $fb->get('/me/accounts');\n\t$pages = $pages->getGraphEdge()->asArray();\n\n\t?>\n\t<form action=\"\" method=\"POST\">\n\t\t<select name=\"page\" single>\n\t<?php\n\tforeach ($pages as $key) {\n\t\t?>\n\t\t<option value=\"<?php echo $key['id']; ?>\"><?php echo $key['name']; ?></option>\n\t\t<?php\n\t}\n\t?>\n\t</select>\n\t<input type=\"submit\" name=\"submit\">\n\t</form>\n\t<?php\n\tif (isset($_POST['submit'])) {\n\t\t$page = $fb->get('/' . $_POST['page'] . '?fields=access_token, name, id');\n\t\t$page = $page->getGraphNode()->asArray();\n\n\t\t$addTab = $fb->post('/' . $page['id'] . '/tabs', array('app_id' => 'APP_ID'), $page['access_token']);\n\t\t$addTab = $addTab->getGraphNode()->asArray();\n\t\tprint_r($addTab);\n\t}\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "canvas-app-login-get-basic-info.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['email']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tunset($_SESSION['facebook_access_token']);\n\t\techo \"<script>window.top.location.href='https://apps.facebook.com/APP_NAMESPACE/'</script>\";\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// priting basic info about user on the screen\n\tprint_r($profile);\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "check-declined-granted-permission.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = ['publish_actions'];\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting declined and granted permissions\n\t$permissions = $fb->get('/me/permissions');\n\t$permissions = $permissions->getGraphEdge()->asArray();\n\n\t// printing declined and granted permission\n\techo \"<pre>\";\n\tprint_r($permissions);\n\techo \"</pre>\";\n\n\t// making new login URL with declined permissions attached to it\n\tforeach ($permissions as $key) {\n\t\tif ($key['status'] == 'declined') {\n\t\t\t$declined[] = $key['permission'];\n\t\t\t$loginUrl = $helper->getReRequestUrl('http://sohaibilyas.com/APP_DIR/', $declined);\n\t\t\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n\t\t}\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/APP_DIR/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "comment-on-public-posts.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://sohaibilyas.com/fbapp/');\n\n$permissions = ['publish_actions']; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n}\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$post = $fb->post('/object-id/comments', array('message' => 'this message should come from user-end'));\n\t\t$post = $post->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\tprint_r($post);\n\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "debug-access-token.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = ['email']; // optional\n\ntry {\n\tif (isset($_SESSION['localhost_app_token'])) {\n\t\t$accessToken = $_SESSION['localhost_app_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['localhost_app_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['localhost_app_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['localhost_app_token']);\n\n\t\t$_SESSION['localhost_app_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// redirecting user back to app login page\n\t\theader(\"Location: ./\");\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// debugging access token\n\t$debugToken = $fb->get('/debug_token?input_token='. $_SESSION['localhost_app_token']);\n\t$debugToken = $debugToken->getGraphNode()->asArray();\n\n\t// printing out debugToken response array on screen\n\techo \"<pre>\";\n\tprint_r($debugToken);\n\techo \"</pre>\";\n  \t// Now you can redirect to another page and use the access token from $_SESSION['localhost_app_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/fbapp/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "get-all-photos-of-user.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['user_photos']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t}\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting all photos of user\n\ttry {\n\t\t$photos_request = $fb->get('/me/photos?limit=100&type=uploaded');\n\t\t$photos = $photos_request->getGraphEdge();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t$all_photos = array();\n\tif ($fb->next($photos)) {\n\t\t$photos_array = $photos->asArray();\n\t\t$all_photos = array_merge($photos_array, $all_photos);\n\t\twhile ($photos = $fb->next($photos)) {\n\t\t\t$photos_array = $photos->asArray();\n\t\t\t$all_photos = array_merge($photos_array, $all_photos);\n\t\t}\n\t} else {\n\t\t$photos_array = $photos->asArray();\n\t\t$all_photos = array_merge($photos_array, $all_photos);\n\t}\n\n\tforeach ($all_photos as $key) {\n\t\t$photo_request = $fb->get('/'.$key['id'].'?fields=images');\n\t\t$photo = $photo_request->getGraphNode()->asArray();\n\t\techo '<img src=\"'.$photo['images'][2]['source'].'\"><br>';\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/');\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "get-all-posts-on-user-timeline.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['user_posts']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting all posts published by user\n\ttry {\n\t\t$posts_request = $fb->get('/me/posts?limit=500');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t$total_posts = array();\n\t$posts_response = $posts_request->getGraphEdge();\n\tif($fb->next($posts_response)) {\n\t\t$response_array = $posts_response->asArray();\n\t\t$total_posts = array_merge($total_posts, $response_array);\n\t\twhile ($posts_response = $fb->next($posts_response)) {\n\t\t\t$response_array = $posts_response->asArray();\n\t\t\t$total_posts = array_merge($total_posts, $response_array);\n\t\t}\n\t\tprint_r($total_posts);\n\t} else {\n\t\t$posts_response = $posts_request->getGraphEdge()->asArray();\n\t\tprint_r($posts_response);\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "get-basic-page-info.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = []; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// redirecting user back to app login page\n\t\theader(\"Location: ./\");\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// get basic page info\n\t$page = $fb->get('/funnydemons?fields=username,picture.width(500),cover,');\n\t$page = $page->getGraphNode()->asArray();\n\n\techo \"<img src='{$page['cover']['source']}'>\";\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/APP_DIR/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "get-likes-data-posts-photos.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://sohaibilyas.com/fbapp/');\n\n$permissions = ['user_posts', 'user_photos']; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting likes data of recent 100 posts by user\n\t$getPostsLikes = $fb->get('/me/posts?fields=likes.limit(1000){name,id}&limit=100');\n\t$getPostsLikes = $getPostsLikes->getGraphEdge()->asArray();\n\n\t// printing likes data as per requirements\n\tforeach ($getPostsLikes as $key) {\n\t\tif (isset($key['likes'])) {\n\t\t\techo count($key['likes']) . '<br>';\n\t\t\tforeach ($key['likes'] as $key) {\n\t\t\t\techo $key['name'] . '<br>';\n\t\t\t}\n\t\t}\n\t}\n\n\t// getting likes data of recent 100 photos by user\n\t$getPhotosLikes = $fb->get('/me/photos?fields=likes.limit(1000){name,id}&limit=100&type=uploaded');\n\t$getPhotosLikes = $getPhotosLikes->getGraphEdge()->asArray();\n\n\t// printing likes data as per requirements\n\tforeach ($getPhotosLikes as $key) {\n\t\tif (isset($key['likes'])) {\n\t\t\techo count($key['likes']) . '<br>';\n\t\t\tforeach ($key['likes'] as $key) {\n\t\t\t\techo $key['name'] . '<br>';\n\t\t\t}\n\t\t}\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "get-list-of-friends-names.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['user_friends']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t}\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// get list of friends' names\n\ttry {\n\t\t$requestFriends = $fb->get('/me/taggable_friends?fields=name&limit=100');\n\t\t$friends = $requestFriends->getGraphEdge();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// if have more friends than 100 as we defined the limit above on line no. 68\n\tif ($fb->next($friends)) {\n\t\t$allFriends = array();\n\t\t$friendsArray = $friends->asArray();\n\t\t$allFriends = array_merge($friendsArray, $allFriends);\n\t\twhile ($friends = $fb->next($friends)) {\n\t\t\t$friendsArray = $friends->asArray();\n\t\t\t$allFriends = array_merge($friendsArray, $allFriends);\n\t\t}\n\t\tforeach ($allFriends as $key) {\n\t\t\techo $key['name'] . \"<br>\";\n\t\t}\n\t\techo count($allFriends);\n\t} else {\n\t\t$allFriends = $friends->asArray();\n\t\t$totalFriends = count($allFriends);\n\t\tforeach ($allFriends as $key) {\n\t\t\techo $key['name'] . \"<br>\";\n\t\t}\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "get-list-of-liked-pages.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['user_likes']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// get list of pages liked by user\n\ttry {\n\t\t$requestLikes = $fb->get('/me/likes?limit=100');\n\t\t$likes = $requestLikes->getGraphEdge();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n \t\techo 'Graph returned an error: ' . $e->getMessage();\n  \t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t$totalLikes = array();\n\tif ($fb->next($likes)) {\t\n\t\t$likesArray = $likes->asArray();\n\t\t$totalLikes = array_merge($totalLikes, $likesArray); \n\t\twhile ($likes = $fb->next($likes)) { \n\t\t\t$likesArray = $likes->asArray();\n\t\t\t$totalLikes = array_merge($totalLikes, $likesArray);\n\t\t}\n\t} else {\n\t\t$likesArray = $likes->asArray();\n\t\t$totalLikes = array_merge($totalLikes, $likesArray);\n\t}\n\n\t// printing data on screen\n\tforeach ($totalLikes as $key) {\n\t\techo $key['name'] . '<br>';\n\t}\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "get-list-of-managed-groups.php",
    "content": "<?php\nsession_start();\n\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['user_managed_groups']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect user back to app when page receives $_GET['code'] variable\n\tif (isset($_GET['code'])) {\n\t\techo \"<script>window.top.location.href='https://apps.facebook.com/APP_NAMESPACE';</script>\";\n\t\texit;\n\t}\n\t\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// get list of groups managed by user\n\ttry {\n\t\t$requestGroups = $fb->get('/me/groups');\n\t\t$groups = $requestGroups->getGraphEdge()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n  \t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\tforeach ($groups as $key) {\n\t\techo $key['name'] . \"<br>\";\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}"
  },
  {
    "path": "get-more-user-info.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = ['user_birthday', 'user_location', 'user_website']; // optional\n\t\ntry {\n\tif (isset($_SESSION['localhost_app_token'])) {\n\t\t$accessToken = $_SESSION['localhost_app_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['localhost_app_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['localhost_app_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['localhost_app_token']);\n\n\t\t$_SESSION['localhost_app_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me?fields=name,first_name,last_name,birthday,website,location');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// redirecting user back to app login page\n\t\theader(\"Location: ./\");\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\t\n\t// printing $profile array on the screen which holds the basic info about user\n\techo $profile['birthday']->format('d-m-Y');\n\techo $profile['website'];\n\techo $profile['location']['name'];\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['localhost_app_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/fbapp/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "get-user-profile-picture.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['email']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting profile picture of the user\n\ttry {\n\t\t$requestPicture = $fb->get('/me/picture?redirect=false&height=300'); //getting user picture\n\t\t$requestProfile = $fb->get('/me'); // getting basic info\n\t\t$picture = $requestPicture->getGraphUser();\n\t\t$profile = $requestProfile->getGraphUser();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\t\n\t// showing picture on the screen\n\techo \"<img src='\".$picture['url'].\"'/>\";\n\n\t// saving picture\n\t$img = __DIR__.'/'.$profile['id'].'.jpg';\n\tfile_put_contents($img, file_get_contents($picture['url']));\n\t\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/');\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "like-public-posts.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://sohaibilyas.com/APP_DIR/');\n\n$permissions = ['publish_actions']; // optional\n\t\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t$posts = $fb->get('/id/feed'); // page username or group id or user id\n\t$posts = $posts->getGraphEdge()->asArray(); // got posts along ids\n\n\t$post =  $posts[0]['id']; // getting specific post id\n\n\t$like = $fb->post('/' . $posts[0]['id'] . '/likes'); // liking that post on facebook\n\tprint_r($like->getGraphNode()->asArray());\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "live-video-api.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://WEBSITE.com/fbapp/');\n\n$permissions = ['publish_actions'];\n\ntry {\n\tif (isset($_SESSION['fb_token'])) {\n\t\t$accessToken = $_SESSION['fb_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['fb_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['fb_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['fb_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['fb_token']);\n\n\t\t$_SESSION['fb_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['fb_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// to create live video\n\t$createLiveVideo = $fb->post('/me/live_videos', ['title' => 'new video', 'description' => 'descrip of the video']);\n\t$createLiveVideo = $createLiveVideo->getGraphNode()->asArray();\n\tprint_r($createLiveVideo);\n\n\t// to get live video info\n\t$LiveVideo = $fb->get('/live_video_id');\n\t$LiveVideo = $LiveVideo->getGraphNode()->asArray();\n\tprint_r($LiveVideo);\n\n\t// to update live video\n\t$LiveVideo = $fb->post('/live_video_id', ['title' => 'title of the new video']);\n\t$LiveVideo = $LiveVideo->getGraphNode()->asArray();\n\tprint_r($LiveVideo);\n\n\t// to delete live video\n\t$LiveVideo = $fb->delete('/live_video_id');\n\t$LiveVideo = $LiveVideo->getGraphNode()->asArray();\n\tprint_r($LiveVideo);\n\n\t// Now you can redirect to another page and use the access token from $_SESSION['fb_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "login-on-website-get-basic-info.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = ['email']; // optional\n\t\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// redirecting user back to app login page\n\t\theader(\"Location: ./\");\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\t\n\t// printing $profile array on the screen which holds the basic info about user\n\tprint_r($profile);\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('https://sohaibilyas.com/fbapp/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "multi-photo-story.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://WEBSITE_URL.com/fbapp/');\n\n$permissions = ['publish_actions', 'user_photos']; // optional\n\ntry {\n\tif (isset($_SESSION['fb_token'])) {\n\t\t$accessToken = $_SESSION['fb_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['fb_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['fb_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['fb_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['fb_token']);\n\n\t\t$_SESSION['fb_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['fb_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// post multi-photo story\n\t$uploadPhoto1 = $fb->post('/me/photos', ['published' => 'false', 'url' => 'https://pbs.twimg.com/profile_images/766914502919086080/lchcXIiJ_400x400.jpg']);\n\t$uploadPhoto2 = $fb->post('/me/photos', ['published' => 'false', 'url' => 'https://pbs.twimg.com/profile_images/648888480974508032/66_cUYfj_400x400.jpg']);\n\t$uploadPhoto3 = $fb->post('/me/photos', ['published' => 'false', 'url' => 'https://pbs.twimg.com/profile_images/775931351480401921/qAGIo8Kt_400x400.jpg']);\n\n\t$uploadPhoto1 = $uploadPhoto1->getGraphNode()->asArray();\n\t$uploadPhoto2 = $uploadPhoto2->getGraphNode()->asArray();\n\t$uploadPhoto3 = $uploadPhoto3->getGraphNode()->asArray();\n\n\t$photo1 = $uploadPhoto1['id'];\n\t$photo2 = $uploadPhoto2['id'];\n\t$photo3 = $uploadPhoto3['id'];\n\n\t$multiPhotoPost = $fb->post('/me/feed', [\n\t\t'attached_media[0]' => '{\"media_fbid\":\"'.$photo1.'\"}',\n\t\t'attached_media[1]' => '{\"media_fbid\":\"'.$photo2.'\"}',\n\t\t'attached_media[2]' => '{\"media_fbid\":\"'.$photo3.'\"}',\n\t\t'message' => 'just today story'\n\t]);\n\n\t// Now you can redirect to another page and use the access token from $_SESSION['fb_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "post-as-page.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = ['manage_pages', 'publish_pages']; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// redirecting user back to app login page\n\t\theader(\"Location: ./\");\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// post on behalf of page\n\t$pages = $fb->get('/me/accounts');\n\t$pages = $pages->getGraphEdge()->asArray();\n\n\tforeach ($pages as $key) {\n\t\tif ($key['name'] == 'Funny Demons') {\n\t\t\t$post = $fb->post('/' . $key['id'] . '/feed', array('message' => 'just for testing...'), $key['access_token']);\n\t\t\t$post = $post->getGraphNode()->asArray();\n\t\t\tprint_r($post);\n\t\t}\n\t}\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('http://sohaibilyas.com/APP_DIR/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "post-on-user-timeline.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n$permissions = ['email', 'publish_actions']; // optional\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\t\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t \tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\t\n\t// posting on user timeline using publish_actins permission\n\ttry {\n\t\t// message must come from the user-end\n\t\t$data = ['message' => 'testing...'];\n\t\t$request = $fb->post('/me/feed', $data);\n\t\t$response = $request->getGraphNode()->asArray;\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\techo $response['id'];\n\n  \t// Now you can redirect to another page and use the\n  \t// access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "post-reactions-info.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://WEBSITE.com/fbapp/');\n\n$permissions = []; // optional\n\ntry {\n\tif (isset($_SESSION['fb_token'])) {\n\t\t$accessToken = $_SESSION['fb_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['fb_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['fb_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['fb_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['fb_token']);\n\n\t\t$_SESSION['fb_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['fb_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t$reactions = $fb->get('/6738073078_10155808173158079/reactions?summary=total_count&type=LIKE');\n\t$reactions = $reactions->getGraphEdge()->getTotalCount();\n\n\tprint_r($reactions);\n\n\t// Now you can redirect to another page and use the access token from $_SESSION['fb_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "search.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://sohaibilyas.com/APP_DIR/');\n\n$permissions = []; // optional\n\t\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\t\n\t// type can be user, group, page or event\n\t$search = $fb->get('/search?q=programming&type=page');\n\t$search = $search->getGraphEdge()->asArray();\n\n\tforeach ($search as $key) {\n\t\techo $key['name'] . '<br>';\n\t}\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "send-html-form-data.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n$permissions = ['publish_actions']; // optional\n\t\ntry {\n\tif (isset($_SESSION['localhost_app_token'])) {\n\t\t$accessToken = $_SESSION['localhost_app_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['localhost_app_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['localhost_app_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['localhost_app_token']);\n\n\t\t$_SESSION['localhost_app_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['localhost_app_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me?fields=name,first_name,last_name');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// redirecting user back to app login page\n\t\theader(\"Location: ./\");\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\tif (isset($_POST['message'])) {\n\t\t\t$post = $fb->post('/me/feed', array('message' => $_POST['message']));\n\t\t\t$post = $post->getGraphNode()->asArray();\n\n\t\t\techo $post['id'];\n\t}\t\n?>\n\t<form action=\"\" method=\"POST\">\n\t\tMessage: <input name=\"message\" type=\"text\">\n\t\t<br>\n\t\t<input type=\"submit\" name=\"submit\" value=\"Submit\">\n\t</form>\n<?php\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['localhost_app_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl('http://localhost.com/fbapp/', $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "send-notification-user.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = []; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting basic info about user\n\ttry {\n\t\t$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');\n\t\t$profile = $profile_request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tunset($_SESSION['facebook_access_token']);\n\t\techo \"<script>window.top.location.href='https://apps.facebook.com/APP_NAMESPACE/'</script>\";\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// sending notification to user\n\t$sendNotif = $fb->post('/' . $profile['id'] . '/notifications', array('href' => '?true=43', 'template' => 'click here for more information!'), 'APP_ACCESS_TOKEN');\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}"
  },
  {
    "path": "single-multiple-posting-managed-groups.php",
    "content": "<?php\nsession_start();\n\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n\n$helper = $fb->getCanvasHelper();\n\n$permissions = ['user_managed_groups', 'publish_actions']; // optionnal\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect user back to app when page receives $_GET['code'] variable\n\tif (isset($_GET['code'])) {\n\t\techo \"<script>window.top.location.href='https://apps.facebook.com/APP_NAMESPACE';</script>\";\n\t\texit;\n\t}\n\t\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// get list of groups managed by user\n\ttry {\n\t\t$requestGroups = $fb->get('/me/groups');\n\t\t$groups = $requestGroups->getGraphEdge()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n  \t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// post in single group managed by user\n\tforeach ($groups as $key) {\n\t\tif ($key['name'] == 'Funny Demons') {\n\t\t\t$groupId = $key['id'];\n\t\t}\n\t}\n\n\ttry {\n\t\t$requestPost = $fb->post('/' . $groupId . '/feed', array('message' => 'this message field must come from user-end as Facebook strictly prohibits the pre-filled message field'));\n\t\t$post = $requestPost->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n  \t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// get response of single posting\n\tprint_r($post);\n\n\t// post in all groups managed by user\n\tforeach ($groups as $key) {\n\t\ttry {\n\t\t\t$requestMultiPost = $fb->post('/' . $key['id'] . '/feed', array('message' => 'this message field must come from user-end as Facebook strictly prohibitsf the pre-filled message field'));\n\t\t\t$multiPost = $requestMultiPost->getGraphNode()->asArray();\n\t\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t\t// When Graph returns an error\n\t\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t  \t\texit;\n\t\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t\t// When validation fails or other local issues\n\t\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\t\texit;\n\t\t}\n\t}\n\t\n\t// get response for multiple posting\n\tprint_r($multiPost);\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}"
  },
  {
    "path": "tag-friends-in-photo.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://WEBSITE.com/fbapp/');\n\n$permissions = ['publish_actions', 'user_friends']; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting all friends of user\n\t$friends = $fb->get('/me/taggable_friends');\n\t$friends = $friends->getGraphEdge()->asArray();\n\n\t// getting random friend out of all friends\n\t$totalFriends = count($friends);\n\t$random = rand(0, $totalFriends);\n\t$random1 = rand(0, $totalFriends);\n\t$random2 = rand(0, $totalFriends);\n\n\t// tag a friend in a photo\n\t$uploadPhoto = $fb->post(\"/me/photos\", array('url' => 'https://pbs.twimg.com/profile_images/762704058243219457/aLbu-kMF.jpg'));\n\t$uploadPhoto = $uploadPhoto->getGraphNode()->asArray();\n\t$tagInPhoto = $fb->post('/' . $uploadPhoto['id'] . '/tags', array('tag_uid' => $friends[$random]['id']));\n\n\t// tag multiple friends in a photo\n\t$tags = [];\n\t$tag['tag_uid'] = $friends[$random]['id'];\n\t$tags[] = $tag;\n\t$tag['tag_uid'] = $friends[$random1]['id'];\n\t$tags[] = $tag;\n\t$tag['tag_uid'] = $friends[$random2]['id'];\n\t$tags[] = $tag;\n\n\t$uploadPhoto = $fb->post(\"/me/photos\", array('url' => 'https://pbs.twimg.com/profile_images/762704058243219457/aLbu-kMF.jpg'));\n\t$uploadPhoto = $uploadPhoto->getGraphNode()->asArray();\n\t$tagInPhoto = $fb->post('/' . $uploadPhoto['id'] . '/tags', array('tags' => $tags));\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "tag-friends.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12'\n  ]);\n\n$helper = $fb->getRedirectLoginHelper();\n\n// app directory could be anything but website URL must match the URL given in the developers.facebook.com/apps\ndefine('APP_URL', 'http://sohaibilyas.com/fbapp/');\n\n$permissions = ['publish_actions', 'user_friends']; // optional\n\t\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t// getting short-lived access token\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t// setting default access token to be used in script\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// redirect the user back to the same page if it has \"code\" GET variable\n\tif (isset($_GET['code'])) {\n\t\theader('Location: ./');\n\t}\n\n\t// validating user access token\n\ttry {\n\t\t$user = $fb->get('/me');\n\t\t$user = $user->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\tsession_destroy();\n\t\t// if access token is invalid or expired you can simply redirect to login page using header() function\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\t\n\t// getting all friends of user\n\t$friends = $fb->get('/me/taggable_friends');\n\t$friends = $friends->getGraphEdge()->asArray();\n\n\t// getting random friend out of all friends\n\t$totalFriends = count($friends);\n\t$random = rand(0, $totalFriends);\n\n\t// posting on facebook and tagging friend with it\n\t$post = $fb->post('/me/feed', array('message' => 'my message', 'tags' => $friends[$random]['id']));\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here\n\t$loginUrl = $helper->getLoginUrl(APP_URL, $permissions);\n\techo '<a href=\"' . $loginUrl . '\">Log in with Facebook!</a>';\n}\n"
  },
  {
    "path": "upload-photo-on-timeline.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n]);\n$helper = $fb->getCanvasHelper();\n$permissions = ['email', 'publish_actions']; // optional\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\t\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t\texit;\n\t\t}\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\ttry {\n\t\t// message must come from the user-end\n\t\t$data = ['source' => $fb->fileToUpload(__DIR__.'/photo.jpg'), 'message' => 'my photo'];\n\t\t$request = $fb->post('/me/photos', $data);\n\t\t$response = $request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\techo $response['id'];\n\n  \t// Now you can redirect to another page and use the\n  \t// access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  },
  {
    "path": "validating-access-token.php",
    "content": "<?php\nsession_start();\nrequire_once __DIR__ . '/src/Facebook/autoload.php'; // download official fb sdk for php @ https://github.com/facebook/php-graph-sdk\n\n$fb = new Facebook\\Facebook([\n  'app_id' => 'APP_ID',\n  'app_secret' => 'APP_SECRET',\n  'default_graph_version' => 'v2.12',\n  ]);\n\n$helper = $fb->getCanvasHelper();\n$permissions = ['email']; // optional\n\ntry {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t$accessToken = $_SESSION['facebook_access_token'];\n\t} else {\n  \t\t$accessToken = $helper->getAccessToken();\n\t}\n} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n \t// When Graph returns an error\n \techo 'Graph returned an error: ' . $e->getMessage();\n  \texit;\n} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n \t// When validation fails or other local issues\n\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n  \texit;\n }\n\nif (isset($accessToken)) {\n\tif (isset($_SESSION['facebook_access_token'])) {\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t} else {\n\t\t$_SESSION['facebook_access_token'] = (string) $accessToken;\n\n\t  \t// OAuth 2.0 client handler\n\t\t$oAuth2Client = $fb->getOAuth2Client();\n\n\t\t// Exchanges a short-lived access token for a long-lived one\n\t\t$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);\n\n\t\t$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;\n\n\t\t$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);\n\t}\n\n\t// validating the access token\n\ttry {\n\t\t$request = $fb->get('/me');\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\tif ($e->getCode() == 190) {\n\t\t\tunset($_SESSION['facebook_access_token']);\n\t\t\t$helper = $fb->getRedirectLoginHelper();\n\t\t\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\t\t\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n\t\t}\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\t// getting basic info about logged-in user\n\ttry {\n\t\t$request = $fb->get('/me?fields=name,email');\n\t\t$profile = $request->getGraphNode()->asArray();\n\t} catch(Facebook\\Exceptions\\FacebookResponseException $e) {\n\t\t// When Graph returns an error\n\t\techo 'Graph returned an error: ' . $e->getMessage();\n\t\texit;\n\t} catch(Facebook\\Exceptions\\FacebookSDKException $e) {\n\t\t// When validation fails or other local issues\n\t\techo 'Facebook SDK returned an error: ' . $e->getMessage();\n\t\texit;\n\t}\n\n\techo $profile['name'];\n\n  \t// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']\n} else {\n\t$helper = $fb->getRedirectLoginHelper();\n\t$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/APP_NAMESPACE/', $permissions);\n\techo \"<script>window.top.location.href='\".$loginUrl.\"'</script>\";\n}\n"
  }
]