YaWK  24.1
Yet another WebKit
fbExample.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Use Facebook Graph API to get any data from a Facebook Page. Require App ID and Access Token.</b>
6  * <p>This is just an empty example widget for development and demo purpose!</p>
7  *
8  * <p>It is recommended to play around with the facebook graph explorer.
9  * You can set any api call and fields you like to play around and explore the resulting array.
10  * You can use this widget as base for your own facebook api projects.</p>
11  *
12  *
13  * @author Daniel Retzl <[email protected]>
14  * @copyright 2018 Daniel Retzl
15  * @version 1.0.0
16  * @brief Facebook Graph API explorer widget - for demo and development purpose only!
17  */
18 class fbExample
19 {
20  /** @param string your app ID (from developers.facebook.com) */
21  public $fbExampleAppId = '';
22  /** @param string your page ID (http://facebook.com/{YOURPAGEID} */
23  public $fbExamplePageId = '';
24  /** @param string your access token (secret word from developers.facebook.com) */
25  public $fbExampleAccessToken = '';
26  /** @param string your graph request */
27  public $fbExampleGraphRequest = '/events/';
28  /** @param string fields that should be selected from facebook graph */
29  public $fbExampleFields = 'id,name,description,place,start_time,cover,maybe_count,attending_count,is_canceled';
30  /** @param string show events of this time range */
31  public $fbExampleYearRange = '1';
32  /** @param string user defined start date */
33  public $fbExampleStartDate = '';
34  /** @param string user defined end date */
35  public $fbExampleEndDate = '';
36  /** @param string which events should be shown? future|past|all */
37  public $fbExampleType = 'future';
38  /** @param string events since this date (used for calc) */
39  public $sinceDate = '';
40  /** @param string events until this date (used for calc) */
41  public $untilDate = '';
42  /** @param string true|false was the js SDK loaded? */
43  public $jsSDKLoaded = 'false';
44  /** @param object api result (as object) */
45  public $apiObject;
46 
47 
48  public function __construct($db)
49  {
50  // load this widget settings from db
51  $widget = new \YAWK\widget();
52  $settings = $widget->getWidgetSettingsArray($db);
53  foreach ($settings as $property => $value)
54  {
55  $this->$property = $value;
56  }
57  // check if required settings are set
58  $this->checkRequirements();
59  }
60 
61  public function checkRequirements()
62  {
63  $this->checkAppId();
64  $this->checkAccessToken();
65  $this->checkPageId();
66  }
67 
68  public function checkAppId()
69  {
70  if (isset($this->fbExampleAppId) && (!empty($this->fbExampleAppId)))
71  {
72  if (is_numeric($this->fbExampleAppId))
73  {
74  return true;
75  }
76  else
77  {
78  die ("app ID is set, but not a numeric value! Please check your app ID - it should contain numbers only.");
79  }
80  }
81  else
82  {
83  die ("app ID is not set. Please add your app ID. You can obtain it from http://developers.facebook.com");
84  }
85  }
86 
87  public function checkAccessToken()
88  {
89  if (isset($this->fbExampleAccessToken) && (!empty($this->fbExampleAccessToken)))
90  {
91  if (is_string($this->fbExampleAccessToken))
92  {
93  return true;
94  }
95  else
96  {
97  die ("Access token is set, but not a string value! Please check your access token.");
98  }
99  }
100  else
101  {
102  die ("Access token is not set. Please add your access token. You can obtain it from http://developers.facebook.com");
103  }
104  }
105  public function checkPageId()
106  {
107  if (isset($this->fbExamplePageId) && (!empty($this->fbExamplePageId)))
108  {
109  if (is_string($this->fbExamplePageId))
110  {
111  return true;
112  }
113  /* to allow custom url requests, this is uncommented.
114  else
115  {
116  die ("Page ID is set, but not a string value! Please check your page ID.");
117  }
118  */
119  }
120  /* to allow custom url requests, this is uncommented.
121  else
122  {
123  die ("Page ID is not set. Please add your page ID. The Page ID is: https://www.facebook.com/{YOURPAGEID}");
124  }
125  */
126  }
127 
128  public function loadJSSDK()
129  { // check if fb JS SDK was loaded before
130  if ($this->jsSDKLoaded == 'false')
131  { // check if app ID is set
132  if ($this->checkAppId() == true)
133  {
134  // include facebook SDK JS
135  echo "<script>
136  window.fbAsyncInit = function() {
137  FB.init({
138  appId : '" . $this->fbExampleAppId . "',
139  xfbml : true,
140  version : 'v3.1'
141  });
142  FB.AppEvents.logPageView();
143  };
144 
145  (function(d, s, id){
146  var js, fjs = d.getElementsByTagName(s)[0];
147  if (d.getElementById(id)) {return;}
148  js = d.createElement(s); js.id = id;
149  js.src = \"https://connect.facebook.net/en_US/sdk.js\";
150  fjs.parentNode.insertBefore(js, fjs);
151  }(document, 'script', 'facebook-jssdk'));
152  </script>";
153  $this->jsSDKLoaded = 'true';
154  }
155  else
156  {
157  die ("unable to include facebook js SDK - checkAppId failed. Please check your app ID in the widget settings!");
158  }
159  }
160  }
161 
162  public function makeApiCall()
163  {
164  // WHICH EVENTS TO DISPLAY?
165  // evaluation of event type select field
166  if ($this->fbExampleType == "all")
167  {
168  // ALL EVENTS (FUTURE + PAST)
169  $this->sinceDate = date('Y-01-01', strtotime('-' . $this->fbExampleYearRange . ' years'));
170  $this->untilDate = date('Y-01-01', strtotime('+' . $this->fbExampleYearRange . ' years'));
171  }
172  elseif ($this->fbExampleType == "future")
173  {
174  // UPCOMING EVENTS
175  $this->sinceDate = date('Y-m-d');
176  $this->untilDate = date('Y-12-31', strtotime('+' . $this->fbExampleYearRange . ' years'));
177  }
178  elseif ($this->fbExampleType == "past")
179  {
180  // PAST EVENTS
181  $this->sinceDate = date('Y-01-01', strtotime('-' . $this->fbExampleYearRange . ' years'));
182  $this->untilDate = date('Y-m-d');
183  }
184  else
185  { // IF NOT SET - use default:
186  // UPCOMING EVENTS
187  $this->sinceDate = date('Y-m-d');
188  $this->untilDate = date('Y-12-31', strtotime('+' . $this->fbExampleYearRange . ' years'));
189  }
190 
191  // IF START + END DATE IS SET
192  if (isset($this->fbExampleStartDate) && (!empty($this->fbExampleStartDate))
193  && (isset($this->fbExampleEndDate) && (!empty($this->fbExampleEndDate))))
194  {
195  $this->sinceDate = date($this->fbExampleStartDate);
196  $this->untilDate = date($this->fbExampleEndDate);
197  }
198 
199  // unix timestamp years
200  $since_unix_timestamp = strtotime($this->sinceDate);
201  $until_unix_timestamp = strtotime($this->untilDate);
202  // check if pageID is set
203  if (isset($this->fbExamplePageId) && (!empty($this->fbExamplePageId)))
204  {
205  // set markup for pageID string
206  $pageIdMarkup = "{$this->fbExamplePageId}";
207  }
208  else
209  { // leave empty if no page id is given (to build custom graph string)
210  $this->fbExamplePageId = '';
211  }
212  // check if fields are set
213  if (isset($this->fbExampleFields) && (!empty($this->fbExampleFields)))
214  { // set markup for api query string
215  $fieldsMarkup = "&fields={$this->fbExampleFields}";
216  if (empty($this->fbExamplePageId))
217  {
218  $fieldsMarkup = "?fields={$this->fbExampleFields}";
219  }
220  }
221  else
222  { // no fields wanted, leave markup empty
223  $fieldsMarkup = '';
224  }
225 
226  // prepare API call
227  // $json_link = "https://graph.facebook.com/v2.7/{$this->fbExamplePageId}{$this->fbExampleGraphRequest}?fields={$this->fields}&access_token={$this->fbExampleAccessToken}";
228  $json_link = "https://graph.facebook.com/v3.1/{$this->fbExamplePageId}{$this->fbExampleGraphRequest}?access_token={$this->fbExampleAccessToken}&since={$since_unix_timestamp}&until={$until_unix_timestamp}" . $fieldsMarkup . "";
229  // get json string
230  $json = file_get_contents($json_link);
231 
232  // convert json to object
233  return $this->apiObject = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
234  }
235 
236  public function checkApiObjectData()
237  {
238  if (isset($this->apiObject['data']) && (!empty($this->apiObject['data'])))
239  {
240  return true;
241  }
242  else
243  {
244  return false;
245  }
246  }
247 
248  public function printApiObject()
249  {
250  $this->makeApiCall();
251  if ($this->checkApiObjectData() === true)
252  {
253  echo "<pre>";
254  print_r($this);
255  echo "</pre>";
256  }
257  else
258  {
259  echo "<pre>";
260  echo "Could not retrieve any data from Facebook. Please check your PageID, API request, field and date settings";
261  echo "</pre>";
262  exit;
263  }
264  }
265 
266  public function basicOutput()
267  {
268  $this->loadJSSDK();
269  $this->makeApiCall();
270 
271  foreach ($this->apiObject['data'] as $property => $value) {
272  echo "<b>$property </b>: $value<br>";
273 
274  foreach ($value as $entry => $key) {
275 
276  echo "$entry : $key<br>";
277 
278  if (is_array($key)) {
279  foreach ($key as $p => $v) {
280  echo "&nbsp;&nbsp;&nbsp;&nbsp;$p : $v<br>";
281  if (is_array($v)) {
282  foreach ($v as $a => $b) {
283  echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$a : $b<br>";
284  }
285  }
286  }
287  }
288  }
289  echo "<br>";
290  }
291  }
292 
293  public function drawGallery()
294  {
295  $this->loadJSSDK();
296  $this->makeApiCall();
297  if (isset($this->apiObject['data']) && (!empty($this->apiObject))) {
298  echo "<h1>Facebook Photo Albums</h1>";
299 
300  foreach ($this->apiObject['data'] as $property => $value)
301  {
302  if ($value['name'] != "//Profile Pictures"
303  && ($value['name'] !=
304  != "//Cover Photos")
305  {
306  $fn = $value['picture']['data']['url'];
307  echo "<div class=\"col-md-2 text-center\">
308  <img src=\"$fn\" style=\"width:200px;\" class=\"img-responsive hvr-grow\"><h3>$value[name]
309  <small><i>($value[count])</i></small></h3><br>
310  </div>";
311 
312 
313  }
314 
315  /*
316  foreach ($value['images'] as $photo)
317  {
318  echo "<pre>";
319  echo "<img src=\"".$photo['source']."\" class=\"img-responsive\">";
320  echo "</pre>";
321  }
322  */
323 /*
324  echo "<br><br>$property : $value<br>";
325 
326  if (is_array($value)) {
327 
328  foreach ($value as $entry => $key) {
329  echo "$entry : $key <br>";
330  if (is_array($key)) {
331  foreach ($key as $image) {
332  echo "$key : $image<br>";
333  if (is_array($key) || (is_array($image))) {
334  foreach ($key as $arrayKey => $arrayValue) {
335  echo "&nbsp;&nbsp;$arrayKey : $arrayValue<br>";
336  if (is_array($arrayValue)) {
337  foreach ($arrayValue as $p => $v) {
338  echo "&nbsp;&nbsp;$p : $v<br>";
339  }
340  }
341  }
342  }
343  }
344  }
345  }
346  }
347 */
348  }
349  }
350  }
351 } // end class events
352 } // end namespace
die
Definition: block-user.php:27
Facebook Graph API explorer widget - for demo and development purpose only!
Definition: fbExample.php:18
exit
if(isset($_POST['save'])) $settings
print $tourdates date
$value