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

You might also like

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.

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

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.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

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.

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.

Xan Barksdale

Xan Barksdale

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

Close Menu