OPiQuotations  v.02.00.00 — October 26, 2015
 All Classes Namespaces Files Functions Variables Pages
uneOPiCitation_pour_Facebook.php
Go to the documentation of this file.
1 <?php /* -*- coding: utf-8 -*- */
2 
3 /** \file uneOPiCitation_pour_Facebook.php
4  * (February 6, 2014)
5  *
6  * \brief
7  * Little PHP application to send one quotation (from OPiCitations)
8  * to Facebook account https://www.facebook.com/1OPiCitationParJour
9  *
10  * Use the https://github.com/facebook/facebook-php-sdk library.
11  *
12  * Piece of OPiQuotations.
13  * https://bitbucket.org/OPiMedia/opiquotations
14  *
15  * GPLv3 --- Copyright (C) 2014 Olivier Pirson
16  * http://www.opimedia.be/
17  *
18  * @package OPiCitations
19  */
20 
21 set_include_path(get_include_path().PATH_SEPARATOR.dirname(realpath(__FILE__)));
22 require_once 'OPiQuotations/log.inc';
23 
24 #DEBUG
25 if (true) {
26  // Development configuration
27  ini_set('display_errors', 'stdout');
28  ini_set('display_startup_errors', 1);
29  ini_set('html_errors', 1);
30 
31  error_reporting(-1);
32 
33  assert_options(ASSERT_ACTIVE, true);
34  assert_options(ASSERT_WARNING, true);
35  assert_options(ASSERT_BAIL, true);
36 }
37 else {
38 #DEBUG_END
39  // Production configuration
40  ini_set('display_errors', 'stderr');
41  ini_set('display_startup_errors', 0);
42  ini_set('html_errors', 0);
43 
44  error_reporting(-1);
45 
46  assert_options(ASSERT_ACTIVE, false);
47  assert_options(ASSERT_WARNING, false);
48  assert_options(ASSERT_BAIL, false);
49 
50  set_error_handler('\OPiQuotations\error_handler');
51 #DEBUG
52 }
53 #DEBUG_END
54 
55 mb_internal_encoding('UTF-8');
56 mb_regex_encoding('UTF-8');
57 mb_http_output('UTF-8');
58 mb_detect_order('UTF-8');
59 
60 require_once 'OPiQuotations/OPiQuotations.inc';
61 
62 
63 // Choose a quotation at random
65 
66 $quots = $opiquotations->quotations_by_random(1, 'Facebook - 1OPiCitationParJour');
67 
68 if (empty($quots)) {
69  OPiQuotations\to_log('uneOPiCitation_pour_Facebook.php: empty quots!');
70 
71  return false;
72 }
73 
74 $text = $quots[0]->to_text_facebook('http://www.opimedia.be/OPiCitations/?id='.$quots[0]->id());
75 
76 
77 // Connect and send to Facebook
78 require_once('.private/Facebook_login.inc');
79 
80 $ch = curl_init('https://graph.facebook.com/'.$page_id.'/feed');
81 if (!$ch) {
82  OPiQuotations\to_log('uneOPiCitation_pour_Facebook.php curl_init error');
83 
84  return false;
85 }
86 
87 $r = curl_setopt_array($ch,
88  array(CURLOPT_POST => 1,
89  CURLOPT_POSTFIELDS => array('access_token' => $access_token,
90  'message' => $text),
91  CURLOPT_RETURNTRANSFER => 1,
92  CURLOPT_SSL_VERIFYPEER => false));
93 if ($ch === false) {
94  OPiQuotations\to_log('uneOPiCitation_pour_Facebook.php curl_setopt_array error '.curl_errno($ch).' : '.curl_error($ch));
95 
96  return false;
97 }
98 
99 $return = curl_exec($ch);
100 if ($return === false) {
101  OPiQuotations\to_log('uneOPiCitation_pour_Facebook.php curl_exec error '.curl_errno($ch).' : '.curl_error($ch));
102 
103  return false;
104 }
105 
106 curl_close($ch);
107 
108 $return = json_decode($return, true);
109 
110 if (($return === null) || isset($return['error'])) {
111  OPiQuotations\to_log('uneOPiCitation_pour_Facebook.php Facebook error '.print_r($return, true));
112 
113  return false;
114 }
115 
116 ?>