How to Create Tweets Using the Twitter API

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

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 high-paying freelance clients?

I’ll show you what I learned over the last 15 years about building a 100K freelance business from complete scratch. What I did to land projects with Inc. Magazine, Tim Ferris, Lewis Howes, Michael Hyatt and others. Just enter your email address in the box below and let’s get started:

Leave a Reply on Twitter

Due to excessive spam, I’ve disabled comments on my website. If you have a question or would like to leave a comment, don’t hesitate to contact me on Twitter.

You might also like

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.

The simple secret to high-paying freelance clients?

Enter your email below and I’ll show how to charge more and build a 100K freelance business from scratch:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

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.”

Thabo Motsoahae

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

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

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!

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.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.