/** * @param Article $article * @param string $header */ public function provide(Article $article, $header = "") { $this->twitter->post("statuses/update", array("status" => "{$header} {$article->getTitle()} >> http://matomepp.net/p/{$article->getArticleId()}")); if ($this->twitter->getLastHttpCode() != 200) { throw new \RuntimeException(json_encode($this->twitter->getLastBody())); } $this->storeTweets($article); }
/** * @depends testLastResult */ public function testResetLastResponse() { $this->twitter->resetLastResponse(); $this->assertEquals('', $this->twitter->getLastApiPath()); $this->assertEquals(0, $this->twitter->getLastHttpCode()); $this->assertEquals(array(), $this->twitter->getLastBody()); }
/** * 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; }