Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.25 KB | None | 0 0
  1. <?php
  2.  
  3.     //removed the question marks at the end.... no need for them
  4.  
  5.     $servername = "localhost";
  6.     $username   = "root";
  7.     $password   = "arsenal";
  8.     $db_name    = "dcuatt";
  9.  
  10.     $conn = new mysqli($servername, $username, $password, $db_name);
  11.    
  12.     if ($conn->connect_error) {
  13.         trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
  14.     }
  15.  
  16.     $rs->data_seek(0);
  17.    
  18.     while($res = $rs->fetch_assoc()) {
  19.         $a_topic = array(
  20.             "topic_id" => $res["topic_id"],
  21.             "twitter_post" => $res["twitter_post"],
  22.             "twitter_image" => $res["twitter_image"],
  23.             "twitter_pubstatus" => $res["twitter_pubstatus"]
  24.         );
  25.          array_push($share_topics, $a_topic);
  26.     }
  27.  // create array with topics to be posted on Twitter
  28.     $sql = 'SELECT id as topic_id, twitter_post, twitter_image, twitter_pubstatus FROM topics ' .
  29.             'WHERE date_published IS NOT NULL AND date_published <= ' . "'" . $now . "' " .
  30.             'AND twitter_pubstatus = 0 ' .  'ORDER BY date_published ASC';
  31.  
  32.     $rs = $conn->query($sql);
  33.     if($rs === false) {
  34.         $user_error = 'Wrong SQL: ' . $sql . '<br>' . 'Error: ' . $conn->errno . ' ' . $conn->error;
  35.         trigger_error($user_error, E_USER_ERROR);
  36.     }
  37.     $rs->data_seek(0);
  38.     while($res = $rs->fetch_assoc()) {
  39.         $a_topic = array(
  40.             "topic_id" => $res["topic_id"],
  41.             "twitter_post" => $res["twitter_post"],
  42.             "twitter_image" => $res["twitter_image"],
  43.             "twitter_pubstatus" => $res["twitter_pubstatus"]
  44.         );
  45.         array_push($share_topics, $a_topic);
  46.     }
  47.     $rs->free();
  48.  
  49.     $consumer_key = 'r79zg5sfH994UeN1wwn1YCEG2';
  50.     $consumer_key_secret = '2QtZWsaDuZFzWbeslDc22mT5H3KaokI1wwowFDCe1OUNke0Jjv';
  51.    
  52.     //our oAuth tokens
  53.     $access_token = '3026239739-nnaCrKl7Gby7Rn2urHlyO2xc7qnUwaURurrulT3';
  54.     $access_token_secret = 'NFFQRIegPRJQCstDTGFVZnWzLRoV2w8wNv9LDywSc6HeI';
  55.  
  56.     //connect code bird to the twitter api
  57.     require_once ('codebird.php');
  58.     \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_key_secret);
  59.     $cb = \Codebird\CodeBird::getInstance();
  60.     $cb->setToken($access_token, $access_token_secret);
  61.  
  62. // AUTOMATIC TWEET EACH TOPIC
  63.     foreach($share_topics as $share_topic) {
  64.         if($share_topic['twitter_status'] == 0) {
  65.             if($share_topic['twitter_image']) {
  66.                 $params = array(
  67.                     'status' => $share_topic['twitter_post'],
  68.                     'media[]' => $share_topic['twitter_image']
  69.                 );
  70.                 $reply = $cb->statuses_updateWithMedia($params);
  71.             }
  72.             else {
  73.                 $params = array(
  74.                     'status' => $share_topic['twitter_post']
  75.                 );
  76.                 $reply = $cb->statuses_update($params);
  77.             }
  78.         // check if tweet successfully posted
  79.         $status = $reply->httpstatus;
  80.         if($status == 200) {
  81.             $sql = 'UPDATE topics SET twitter_pubstatus = 1 WHERE id = ' . $share_topic['topic_id'];
  82.             if($conn->query($sql) === false) {
  83.                 trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
  84.             }
  85.             $result .= $share_topic['topic_id'] . ' ' . $share_topic['twitter_post'] . ' success (' . $status . ')' . $line_break;
  86.         }
  87.         else {
  88.             $result .= $share_topic['topic_id'] . ' ' . $share_topic['twitter_post'] . ' FAILED... (' . $status . ')' . $line_break;
  89.         }
  90.     sleep(3);
  91.   }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement