OPiQuotations  v.03.00.00 — April 5, 2019
une-OPiCitation-pour-Facebook.php
Go to the documentation of this file.
1 <?php /* -*- coding: utf-8 -*- */
2 
3 /** \file une-OPiCitation-pour-Facebook.php
4  * (September 13, 2017)
5  *
6  * \brief
7  * Little PHP application to send one quotation (from OPiCitations)
8  * to Facebook account https://www.facebook.com/1OPiCitationParJour
9  *
10  * Piece of OPiQuotations.
11  * https://bitbucket.org/OPiMedia/opiquotations
12  *
13  * GPLv3 --- Copyright (C) 2014, 2016, 2017 Olivier Pirson
14  * http://www.opimedia.be/
15  *
16  * @package OPiCitations
17  */
18 
19 set_include_path(get_include_path().PATH_SEPARATOR.dirname(realpath(__FILE__)));
20 require_once 'OPiQuotations/log.inc';
21 
22 #DEBUG
23 if (true) {
24  // Development configuration
25  ini_set('display_errors', 'stdout');
26  ini_set('display_startup_errors', 1);
27  ini_set('html_errors', 1);
28 
29  error_reporting(-1);
30 
31  assert_options(ASSERT_ACTIVE, true);
32  assert_options(ASSERT_WARNING, true);
33  assert_options(ASSERT_BAIL, true);
34 }
35 else {
36 #DEBUG_END
37  // Production configuration
38  ini_set('display_errors', 'stderr');
39  ini_set('display_startup_errors', 0);
40  ini_set('html_errors', 0);
41 
42  error_reporting(-1);
43 
44  assert_options(ASSERT_ACTIVE, false);
45  assert_options(ASSERT_WARNING, false);
46  assert_options(ASSERT_BAIL, false);
47 
48  set_error_handler('\OPiQuotations\error_handler');
49 #DEBUG
50 }
51 #DEBUG_END
52 
53 mb_internal_encoding('UTF-8');
54 mb_regex_encoding('UTF-8');
55 mb_http_output('UTF-8');
56 mb_detect_order('UTF-8');
57 
58 require_once 'OPiQuotations/OPiQuotations.inc';
59 
60 
61 $LABEL = 'Facebook - 1OPiCitationParJour';
62 
63 // Choose a quotation at random
65 
66 $quot = $opiquotations->quotation_by_random($LABEL);
67 #DEBUG
68 #$quot = $opiquotations->quotation_by_id(9);
69 #$quot = $opiquotations->quotation_by_id(110);
70 #$quot = $opiquotations->quotation_by_id(459);
71 #$quot = $opiquotations->quotation_by_id(3062);
72 #DEBUG_END
73 
74 if ($quot === null) {
75  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php: no random quotation!');
76 
77  return false;
78 }
79 
80 $text = $quot->to_text_facebook('http://www.opimedia.be/OPiCitations/?id='.$quot->id(),
81  'fr', array('Web'));
82 
83 #DEBUG
84 if (true) {
85  echo mb_strlen($text).'<pre>'.htmlspecialchars($text).'</pre>';
86 
87  return false; // exit to *don't* post the message when do tests
88 }
89 #DEBUG_END
90 
91 
92 // Connect and send to Facebook
93 require_once('.private/Facebook_login.inc');
94 
95 $ch = curl_init('https://graph.facebook.com/'.$page_id.'/feed');
96 if (!$ch) {
97  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php curl_init error');
98 
99  return false;
100 }
101 
102 $r = curl_setopt_array($ch,
103  array(CURLOPT_POST => 1,
104  CURLOPT_POSTFIELDS => array('access_token' => $access_token,
105  'message' => $text),
106  CURLOPT_RETURNTRANSFER => 1,
107  CURLOPT_SSL_VERIFYPEER => false));
108 if ($ch === false) {
109  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php curl_setopt_array error '.curl_errno($ch).' : '.curl_error($ch));
110 
111  return false;
112 }
113 
114 $result = curl_exec($ch);
115 if ($result === false) {
116  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php curl_exec error '.curl_errno($ch).' : '.curl_error($ch));
117 
118  return false;
119 }
120 
121 curl_close($ch);
122 
123 $result = json_decode($result, true);
124 
125 if (($result === null) || isset($result['error'])) {
126  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php Facebook error '.print_r($result, true));
127 
128  return false;
129 }
130 
131 
132 if (empty($result['id'])) {
133  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php: no add selection! '.$quot->id());
134 }
135 else {
136  // Update DB with URL
137  $msg_id = explode('_', $result['id']);
138  $msg_id = array_pop($msg_id);
139 
140  $url = 'https://www.facebook.com/1OPiCitationParJour/posts/'.$msg_id;
141  #DEBUG
142  if (true) {
143  echo '<pre>id:
144 ';
145  var_dump($result['id']);
146  echo '</pre><a href="', $url, '">', $url, '</a>';
147  }
148  #DEBUG_END
149 
151 
152  $ok = $opiquotations->quotation_add_selection($quot, $selection);
153 
154  if (!$ok) {
155  OPiQuotations\to_log('une-OPiCitation-pour-Facebook.php: add selection failed! '.$quot->id());
156  }
157 }
158 
159 ?>
to_log($message)
Append $message in LOG_FILE.
Definition: log.inc:104
Class selection.
if($quot===null) $text
Class to get OPiQuotation and informations from the databse.
if($ch===false) $result