
However, when i found out they are shutting down the basic api, i had no other choice then to learn how it works. What it turned out is that with the right tools it’s pretty easy to do. Especially if you are (like me) looking to setup application that will be using only single user. For example to automatically update your status.
The setup
With basic authorization all we needed was Twitter login and password, with OAuth it’s more complicated but a lot more secure at the same time, even if your app will be hacked your Twitter credentials are still safe.Ok let’s get started with it:
- Go to dev.twitter.com sign in with your Twitter credentials, next click “Register an application” link
- Fill the form, it’s automatically accepted so Twitter guys probably don’t check it all, but still … keep it proffesional. One important thing here is to select “Read and Write” on “Default Access type”. Click “Register Application” and you’re done
- Now you will need 4 keys to make your app work. Go to “View Your Applications” and then click “Edit details” on your newly created app. Scroll down and copy (somewhere) “Consumer Key” and “Consumer Secret”
- Next in the right sidebar click link “My access token” and copy both: “Access Token (oauth_token)” and “Access Token Secret (oauth_token_secret)”
Twitter and PHP OAuth integration
Now let’s get straight to the actual code. The good news it’s there almost nothing to do: just include TwitterOAuth library, put keys and tokens you copied earlier into the script and you are done.<?php require_once 'TwitterOAuth.php'; define("CONSUMER_KEY", "????"); define("CONSUMER_SECRET", "????"); define("OAUTH_TOKEN", "????"); define("OAUTH_SECRET", "????"); $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET); $content = $connection->get('account/verify_credentials'); $connection->post('statuses/update', array('status' => date(DATE_RFC822)));
No comments:
Post a Comment