Passed
Push — master ( bfeae7...87a9ab )
by Rakesh
01:57
created

fb-callback.php (1 issue)

1
<?php
2
if(!session_id()) {
3
  session_start();
4
}
5
require_once 'appconfig.php';
6
7
$fb = new Facebook\Facebook([
8
  'app_id' => $appId, // variable with My Facebook App ID
9
  'app_secret' => $appSecret,
10
  'default_graph_version' => 'v3.2',
11
  ]);
12
$helper = $fb->getRedirectLoginHelper();
13
if(isset($_GET['state'])){
14
  $helper->getPersistentDataHandler()->set('state',$_GET['state']);
15
}
16
try {
17
  $accessToken = $helper->getAccessToken();
18
} catch(Facebook\Exceptions\FacebookResponseException $e) {
19
  // When Graph returns an error
20
  echo 'Graph returned an error: ' . $e->getMessage();
21
  exit;
22
} catch(Facebook\Exceptions\FacebookSDKException $e) {
23
  // When validation fails or other local issues
24
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
25
  exit;
26
}
27
if (! isset($accessToken)) {
28
  if ($helper->getError()) {
29
    header('HTTP/1.0 401 Unauthorized');
30
    echo "Error: " . $helper->getError() . "\n";
31
    echo "Error Code: " . $helper->getErrorCode() . "\n";
32
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
33
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
34
  } else {
35
    header('HTTP/1.0 400 Bad Request');
36
    echo 'Bad request';
37
  }
38
  exit;
39
}
40
// Logged in
41
echo '<h3>Access Token</h3>';
42
var_dump($accessToken->getValue());
43
// The OAuth 2.0 client handler helps us manage access tokens
44
$oAuth2Client = $fb->getOAuth2Client();
45
// Get the access token metadata from /debug_token
46
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
47
echo '<h3>Facebook Photos Challenge</h3>';
48
//var_dump($tokenMetadata);
49
// Validation (these will throw FacebookSDKException's when they fail)
50
$tokenMetadata->validateAppId($appId); // My Facebook App ID
51
// If you know the user ID this access token belongs to, you can validate it here
52
//$tokenMetadata->validateUserId('123');
53
$tokenMetadata->validateExpiration();
54
if (! $accessToken->isLongLived()) {
55
  // Exchanges a short-lived access token for a long-lived one
56
  try {
57
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
58
  } catch (Facebook\Exceptions\FacebookSDKException $e) {
59
    echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n";
60
    exit;
61
  }
62
  echo '<h3>Long-lived</h3>';
63
  var_dump($accessToken->getValue());
64
}
65
66
//$_SESSION['fb_access_token'] = (string) $accessToken;
67
try {
68
  // Returns a `Facebook\FacebookResponse` object
69
  $response = $fb->get('/me?fields=id,name', $accessToken);
70
} catch(Facebook\Exceptions\FacebookResponseException $e) {
71
  echo 'Graph returned an error: ' . $e->getMessage();
72
  exit;
73
} catch(Facebook\Exceptions\FacebookSDKException $e) {
74
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
75
  exit;
76
}
77
$user = $response->getGraphUser();
78
79
echo 'ID: ' . $user['id'] ;
80
echo '<br/ >Welcome, ' . $user['name'];
81
echo '<a href="logout.php" > Logout </a>';
82
83
// Get photo albums of Facebook page using Facebook Graph API
84
$fields = "id,name,description,link,cover_photo,count";
85
$fb_page_id = $user['id'];
86
$graphAlbLink = "https://graph.facebook.com/v3.2/{$fb_page_id}/albums?fields={$fields}&access_token={$accessToken}";
87
88
$jsonData = file_get_contents($graphAlbLink);
89
$fbAlbumObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);
90
91
// Facebook albums content
92
$fbAlbumData = $fbAlbumObj['data'];
93
94
// Render all photo albums
95
echo "<br/><br/>";
96
foreach($fbAlbumData as $data){
97
    $id = isset($data['id'])?$data['id']:'';
98
    $name = isset($data['name'])?$data['name']:'';
99
    $description = isset($data['description'])?$data['description']:'';
100
    $link = isset($data['link'])?$data['link']:'';
101
    $cover_photo_id = isset($data['cover_photo']['id'])?$data['cover_photo']['id']:'';
102
    $count = isset($data['count'])?$data['count']:'';
103
    
104
    $pictureLink = "slideshow.php?album_id={$id}&album_name={$name}";
105
    echo 'a';
106
    echo "<a href='{$pictureLink}'>";
107
    $cover_photo_id = (!empty($cover_photo_id ))?$cover_photo_id : 123456;
108
    echo "<img width=100px height=100px src='https://graph.facebook.com/v3.2/{$cover_photo_id}/picture?access_token={$accessToken}' alt=''>";
109
    echo "</a>";
110
    echo "<p>{$name}</p>";
111
112
    $photoCount = ($count > 1)?$count. 'Photos':$count. 'Photo';
113
    
114
    echo "<p><span style='color:#888;'>{$photoCount} / <a href='{$link}' target='_blank'>View on Facebook</a></span></p>";
115
    echo "<p>{$description}</p>";
116
    echo "</div>";
117
}
118
119
if(isset($_GET['album_id']) && isset($_GET['album_name'])){
120
  $album_id = $_GET['album_id'];
121
  $album_name = $_GET['album_name']; //isset($_GET['album_name'])?:header('Location: fb-callback.php');
122
123
// Get photos of Facebook page album using Facebook Graph API
124
$graphPhoLink = "https://graph.facebook.com/v3.2/{$album_id}/photos?fields=source,images,name&access_token={$accessToken}";
125
$jsonData = file_get_contents($graphPhoLink);
126
$fbPhotoObj = json_decode($jsonData, true, 512, JSON_BIGINT_AS_STRING);
127
128
// Facebook photos content
129
$fbPhotoData = $fbPhotoObj['data'];
130
131
echo "<h2>".$album_name."</h2>";
132
133
echo "<div class='slideshow-container'>";
134
135
// Render all photos 
136
    if (is_array($fbPhotoData) || is_object($fbPhotoData))
137
    {   
138
        foreach($fbPhotoData as $data){
139
        $imageData = end($data['images']);
140
        $imgSource = isset($imageData['source'])?$imageData['source']:'';
141
        $name = isset($data['name'])?$data['name']:'';
142
143
        echo "<div class='mySlides fade'>";
144
        echo "<img src='{$imgSource}' alt='' style='width:100%'>";
145
        echo "<div class='text'>{$name}</div>";
146
        echo "</div>";
147
      }
148
    }
149
}
150
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

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.

Loading history...