示例#1
0
 /**
  * Determine first name of Target or return Handle
  *
  * @param Integer $id
  * @return String
  */
 public function name($id)
 {
     $twitter = new TwitterOAuth(env('CONSUMER_KEY'), env('CONSUMER_SECRET'), env('ACCESS_TOKEN'), env('ACCESS_SECRET'));
     // Get Target Handle
     $target = Targets::find($id);
     $handle = $target->handle;
     // Attempt to get real name from Twitter
     $user = $twitter->get('/users/show', ['screen_name' => $handle]);
     if ($twitter->getLastHttpCode() === 200 && isset($user->name)) {
         $target->object = json_encode($user);
         $target->save();
         // Get first name
         $name = explode(' ', $user->name);
         $fname = $name[0];
         if (ctype_alpha($fname)) {
             $handle = $fname;
         }
     }
     return $handle;
 }
 /**
  * Send Tweet through Twitter API
  *
  * @return Void
  */
 public function send()
 {
     /**
      * Initiate new Twitter
      */
     $twitter = new TwitterOAuth(env('CONSUMER_KEY'), env('CONSUMER_SECRET'), env('ACCESS_TOKEN'), env('ACCESS_SECRET'));
     /**
      * Get Tweets from Queue
      */
     $tweets = $this->next();
     /**
      * Collate Responses
      */
     $results = array();
     /**
      * Generate Images and Upload/Send each Tweet
      */
     foreach ($tweets as $tweet) {
         // Get Name
         $name = Admin::name($tweet->target);
         // Get Target Handle
         $target = Targets::find($tweet->target);
         $handle = $target->handle;
         // Remove Linebreaks from Message
         $tweet->message_clean = str_replace(array("\r", "\n"), ' ', $tweet->message_clean);
         if (trim($name) != '' && trim($tweet->message_clean != '')) {
             // Generate Image
             Image::setDetails($name, $tweet->message_clean);
             $image = Image::paintImage();
             $details = Image::saveImage($image);
             // Save Tweet with Image URL
             $tweet->image_url = $details['image_url'];
             $tweet->save();
             // The Message
             $hashtag = '#tweetthelove';
             $message = '@' . $handle . ' ' . $tweet->message_clean . ' ' . $hashtag;
             // Upload to Twitter
             $media = $twitter->upload('media/upload', ['media' => $details['image_url']]);
             if (isset($media->media_id_string)) {
                 // Post Tweet
                 $status = $twitter->post('statuses/update', ['status' => $message, 'media_ids' => $media->media_id_string]);
             }
             if ($twitter->getLastHttpCode() === 200) {
                 // Mark as Sent
                 $tweet->sent = 1;
                 $tweet->save();
                 $result = true;
             } else {
                 $tweet->failed = 1;
                 $tweet->save();
                 $result = json_encode($twitter->getLastBody());
             }
         } else {
             $tweet->failed = 1;
             $tweet->save();
             $result = false;
         }
         $results[$tweet->id] = array('status' => $result, 'message' => $message);
     }
     return $results;
 }