Twitter For Laravel Part 2

Nov 21, 2016 twitter laravel tutorial api php
Following on from Part 1, in this short tutorial we’ll post tweets to your account and also add hashtags. I won’t be going over how to retrieve hashtags from the blog, that is something that won’t be covered in this tutorial but you can always grab what I’ve done so far to my website via Github.

The main wrapper/API call we’ll be using is this

It takes two arguments, the first will be your tweet which will be in String type and the format the tweet will be sent it. I don’t believe the format is too important since we are sending information to twitter’s server.

As an example
Twitter::postTweet(['status' => $tweet, 'format' => 'json']);

You’ll want to put this code into your PostController class under store or an equivalent function.

As a little bonus I’ve written a short loop to grab hashtags from an array I collected in my form. You'll also want to add spaces between each hashtag or twitter won't recognise that it is one.

$hashtags = '';
$maxHashtags = 3;
if (!empty($tagListTwitter)) {
for ($i = 0; $i < $maxHashtags; $i++) {
$hashtags = '#' . $tagListTwitter[$i] . $hashtags . ' ';
$hashtags = ' ' . $hashtags;

and I also place the title of the post and url. It’s 140 characters long and I believe it should fit a url, title and a few hashtags to get attention. Here it is:

Twitter::postTweet(['status' => $post->title . ' ' . '' . $post->slug . $hashtags, 'format' => 'json']);

You can actually put anything you want into the ‘status’ argument, it’s up to you!

If you found the following info helpful, I'm happy to accept any donations of the following cryptocurrencies.

  • Bitcoin - 17DTiPExzP9StqveW428acEyB4mVMfKbiK
  • Ethereum - 0x87B8307FD20dc90cc05c94905Ec593134D32B6FF
  • Litecoin - LZMiz5U5sVq9doMLYE3gfLJrxCQDKuyCmU
  • Neo - AXv71WB38ajc1KUUEnxQKhynLLPc4BapVb