How to Create Tweets Using the Twitter API

I’m going to quickly show you the exact code to send a tweet using the Twitter API. Then, how to learn to build a full-fledged Twitter app.

Let’s dig in.

Before you start, you need three things:

  1. A developer account on Twitter. (dev.twitter.com)
  2. A Twitter app for the API keys and access tokens (apps.twitter.com)
  3. The Twitter OAuth PHP class (twitteroauth.com)

Download the class to your working folder. Then, create a file called index.php (or whatever you want) and include the Twitter OAuth class like so:

require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

Next, we need to connect to the API:

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");

You, of course, need to change out CONSUMER_KEY, CONSUMER_SECRET, $access_token and $access_token_secret with the actual ones from your Twitter app (which is why you need to create one to do this).

Then to get tweets, do this:

$statuses = $connection->get("statuses/home_timeline", ["count" => 25, "exclude_replies" => true]);

This will get your last 25 tweets, excluding replies.

Finally, to create a new tweet, do this:

$statues = $connection->post("statuses/update", ["status" => "hello world"]);

Changing “hello world” to whatever you want to tweet.

That’s it. Pretty damn easy.

Now, if you’d like to see all of this in a video walk-through and learn how to build a full-fledged Twitter client, then grab a copy of the Complete Web Developer Course by Rob Percival 75% off the normal fee here.

In the API section, he covers all of this… plus a Google Maps client… and you’ll build 25 websites and mobile apps throughout the course.

No way you go through this and aren’t ready to take on a full-time IT job.

Anyway, go here to learn all this at 75% off the normal cost: http://johnmorrisonline.com/cwdc

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

You might also like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:

WHAT OTHERS ARE SAYING

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Close Menu