1 | <?php |
||
2 | if(!session_id()) { |
||
3 | session_start(); |
||
4 | } |
||
5 | require_once 'lib\Facebook\autoload.php'; |
||
6 | |||
7 | $appId='362540437809242'; |
||
8 | $appSecret='538cd04f971479ff14dc409df2fbcf3b'; |
||
9 | |||
10 | $fb = new Facebook\Facebook([ |
||
11 | 'app_id' => $appId, // variable with My Facebook App ID |
||
12 | 'app_secret' => $appSecret, |
||
13 | 'default_graph_version' => 'v3.2', |
||
14 | ]); |
||
15 | $helper = $fb->getRedirectLoginHelper(); |
||
16 | if(isset($_GET['state'])){ |
||
17 | $helper->getPersistentDataHandler()->set('state',$_GET['state']); |
||
18 | } |
||
19 | try { |
||
20 | $accessToken = $helper->getAccessToken(); |
||
21 | } catch(Facebook\Exceptions\FacebookResponseException $e) { |
||
22 | // When Graph returns an error |
||
23 | echo 'Graph returned an error: ' . $e->getMessage(); |
||
24 | exit; |
||
25 | } catch(Facebook\Exceptions\FacebookSDKException $e) { |
||
26 | // When validation fails or other local issues |
||
27 | echo 'Facebook SDK returned an error: ' . $e->getMessage(); |
||
28 | exit; |
||
29 | } |
||
30 | if (! isset($accessToken)) { |
||
31 | if ($helper->getError()) { |
||
32 | header('HTTP/1.0 401 Unauthorized'); |
||
33 | echo "Error: " . $helper->getError() . "\n"; |
||
34 | echo "Error Code: " . $helper->getErrorCode() . "\n"; |
||
35 | echo "Error Reason: " . $helper->getErrorReason() . "\n"; |
||
36 | echo "Error Description: " . $helper->getErrorDescription() . "\n"; |
||
37 | } else { |
||
38 | header('HTTP/1.0 400 Bad Request'); |
||
39 | echo 'Bad request'; |
||
40 | } |
||
41 | exit; |
||
42 | } |
||
43 | // Logged in |
||
44 | //echo '<h3>Access Token</h3>'; |
||
45 | //($accessToken->getValue()); |
||
46 | // The OAuth 2.0 client handler helps us manage access tokens |
||
47 | $oAuth2Client = $fb->getOAuth2Client(); |
||
48 | // Get the access token metadata from /debug_token |
||
49 | $tokenMetadata = $oAuth2Client->debugToken($accessToken); |
||
50 | echo '<h3>Facebook Photos Challenge</h3>'; |
||
51 | //var_dump($tokenMetadata); |
||
52 | // Validation (these will throw FacebookSDKException's when they fail) |
||
53 | $tokenMetadata->validateAppId('362540437809242'); // My Facebook App ID |
||
54 | // If you know the user ID this access token belongs to, you can validate it here |
||
55 | //$tokenMetadata->validateUserId('123'); |
||
56 | $tokenMetadata->validateExpiration(); |
||
57 | if (! $accessToken->isLongLived()) { |
||
58 | // Exchanges a short-lived access token for a long-lived one |
||
59 | try { |
||
60 | $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); |
||
61 | } catch (Facebook\Exceptions\FacebookSDKException $e) { |
||
62 | echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n"; |
||
63 | exit; |
||
64 | } |
||
65 | echo '<h3>Long-lived</h3>'; |
||
66 | var_dump($accessToken->getValue()); |
||
67 | } |
||
68 | |||
69 | $_SESSION['fb_access_token'] = (string) $accessToken; |
||
70 | try { |
||
71 | // Returns a `Facebook\FacebookResponse` object |
||
72 | $response = $fb->get('/me?fields=id,name', $accessToken); |
||
73 | } catch(Facebook\Exceptions\FacebookResponseException $e) { |
||
74 | echo 'Graph returned an error: ' . $e->getMessage(); |
||
75 | exit; |
||
76 | } catch(Facebook\Exceptions\FacebookSDKException $e) { |
||
77 | echo 'Facebook SDK returned an error: ' . $e->getMessage(); |
||
78 | exit; |
||
79 | } |
||
80 | $user = $response->getGraphUser(); |
||
81 | |||
82 | echo 'ID: ' . $user['id'] ; |
||
83 | echo '<br/ >Welcome, ' . $user['name']; |
||
84 | echo '<a href="logout.php" > Logout </a>'; |
||
85 | |||
86 | // Generate graph access token |
||
87 | $graphActLink = "https://graph.facebook.com/oauth/access_token?client_id={$appId}&client_secret={$appSecret}&grant_type=client_credentials"; |
||
88 | |||
89 | // Retrieve access token |
||
90 | $accessTokenJson = file_get_contents($graphActLink); |
||
91 | $accessTokenObj = json_decode($accessTokenJson); |
||
92 | $access_token = $accessTokenObj->access_token; |
||
93 | |||
94 | |||
95 | // Get photo albums of Facebook page using Facebook Graph API |
||
96 | $fields = "id,name,description,link,cover_photo,count"; |
||
97 | $fb_page_id = $user['id']; |
||
98 | $graphAlbLink = "https://graph.facebook.com/v3.2/{$fb_page_id}/albums?fields={$fields}&access_token={$access_token}"; |
||
99 | |||
100 | $jsonData = file_get_contents($graphAlbLink); |
||
101 | $fbAlbumObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING); |
||
102 | |||
103 | // Facebook albums content |
||
104 | $fbAlbumData = $fbAlbumObj['data']; |
||
105 | |||
106 | // Render all photo albums |
||
107 | echo "<br/><br/>"; |
||
108 | foreach($fbAlbumData as $data){ |
||
109 | $id = isset($data['id'])?$data['id']:''; |
||
110 | $name = isset($data['name'])?$data['name']:''; |
||
111 | $description = isset($data['description'])?$data['description']:''; |
||
112 | $link = isset($data['link'])?$data['link']:''; |
||
113 | $cover_photo_id = isset($data['cover_photo']['id'])?$data['cover_photo']['id']:''; |
||
114 | $count = isset($data['count'])?$data['count']:''; |
||
115 | |||
116 | $pictureLink = "fb-callback.php?album_id={$id}&album_name={$name}"; |
||
117 | |||
118 | |||
119 | echo "<a href='{$pictureLink}'>"; |
||
120 | $cover_photo_id = (!empty($cover_photo_id ))?$cover_photo_id : 123456; |
||
121 | echo "<img width=100px height=100px src='https://graph.facebook.com/v3.2/{$cover_photo_id}/picture?access_token={$accessToken}' alt=''>"; |
||
122 | echo "</a>"; |
||
123 | echo "<p>{$name}</p>"; |
||
124 | |||
125 | $photoCount = ($count > 1)?$count. 'Photos':$count. 'Photo'; |
||
126 | |||
127 | echo "<p><span style='color:#888;'>{$photoCount} / <a href='{$link}' target='_blank'>View on Facebook</a></span></p>"; |
||
128 | echo "<p>{$description}</p>"; |
||
129 | } |
||
130 | ?> |
||
0 ignored issues
–
show
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.