Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //removed the question marks at the end.... no need for them
- $servername = "localhost";
- $username = "root";
- $password = "arsenal";
- $db_name = "dcuatt";
- $conn = new mysqli($servername, $username, $password, $db_name);
- if ($conn->connect_error) {
- trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
- }
- $rs->data_seek(0);
- while($res = $rs->fetch_assoc()) {
- $a_topic = array(
- "topic_id" => $res["topic_id"],
- "twitter_post" => $res["twitter_post"],
- "twitter_image" => $res["twitter_image"],
- "twitter_pubstatus" => $res["twitter_pubstatus"]
- );
- array_push($share_topics, $a_topic);
- }
- // create array with topics to be posted on Twitter
- $sql = 'SELECT id as topic_id, twitter_post, twitter_image, twitter_pubstatus FROM topics ' .
- 'WHERE date_published IS NOT NULL AND date_published <= ' . "'" . $now . "' " .
- 'AND twitter_pubstatus = 0 ' . 'ORDER BY date_published ASC';
- $rs = $conn->query($sql);
- if($rs === false) {
- $user_error = 'Wrong SQL: ' . $sql . '<br>' . 'Error: ' . $conn->errno . ' ' . $conn->error;
- trigger_error($user_error, E_USER_ERROR);
- }
- $rs->data_seek(0);
- while($res = $rs->fetch_assoc()) {
- $a_topic = array(
- "topic_id" => $res["topic_id"],
- "twitter_post" => $res["twitter_post"],
- "twitter_image" => $res["twitter_image"],
- "twitter_pubstatus" => $res["twitter_pubstatus"]
- );
- array_push($share_topics, $a_topic);
- }
- $rs->free();
- $consumer_key = 'r79zg5sfH994UeN1wwn1YCEG2';
- $consumer_key_secret = '2QtZWsaDuZFzWbeslDc22mT5H3KaokI1wwowFDCe1OUNke0Jjv';
- //our oAuth tokens
- $access_token = '3026239739-nnaCrKl7Gby7Rn2urHlyO2xc7qnUwaURurrulT3';
- $access_token_secret = 'NFFQRIegPRJQCstDTGFVZnWzLRoV2w8wNv9LDywSc6HeI';
- //connect code bird to the twitter api
- require_once ('codebird.php');
- \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_key_secret);
- $cb = \Codebird\CodeBird::getInstance();
- $cb->setToken($access_token, $access_token_secret);
- // AUTOMATIC TWEET EACH TOPIC
- foreach($share_topics as $share_topic) {
- if($share_topic['twitter_status'] == 0) {
- if($share_topic['twitter_image']) {
- $params = array(
- 'status' => $share_topic['twitter_post'],
- 'media[]' => $share_topic['twitter_image']
- );
- $reply = $cb->statuses_updateWithMedia($params);
- }
- else {
- $params = array(
- 'status' => $share_topic['twitter_post']
- );
- $reply = $cb->statuses_update($params);
- }
- // check if tweet successfully posted
- $status = $reply->httpstatus;
- if($status == 200) {
- $sql = 'UPDATE topics SET twitter_pubstatus = 1 WHERE id = ' . $share_topic['topic_id'];
- if($conn->query($sql) === false) {
- trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
- }
- $result .= $share_topic['topic_id'] . ' ' . $share_topic['twitter_post'] . ' success (' . $status . ')' . $line_break;
- }
- else {
- $result .= $share_topic['topic_id'] . ' ' . $share_topic['twitter_post'] . ' FAILED... (' . $status . ')' . $line_break;
- }
- sleep(3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement