Estuve buscando un poco sobre el uso de la nueva versión del API de twitter 1.1, encontré en una respuesta bastante completa de los señores @lackovic10 and @rivers!. A continuación adjunto el tutorial:

So you want to use the Twitter v1.1 API?

Note: the files for these are on Github.

Version 1.0 will soon be deprecated and unauthorised requests won’t be allowed. So, here’s a post to help you do just that, along with a PHP class to make your life easier.

1. Create a Developer Account: Set yourself up a Developer account on Twitter

You need to visit the official Twitter developer site and register for a developer account. This is a freeand necessary step to make requests for the v1.1 API.

2. Create an Application: Create an Application on the twitter dev site

What? You thought you could make unauthenticated requests? Not with Twitter’s v1.1 API. You need to visit and click the “Create Application” button.

enter image description here

On this page, fill in whatever details you want. For me, it didn’t matter because I just wanted to make a load of block requests to get rid of spam followers. The point is you are going to get yourself a set of unique keys to use for your application.

So, the point of creating an application is to give yourself (and twitter) a set of keys. These are:

  • The consumer key
  • The consumer secret
  • The access token
  • The access token secret

There’s a little bit of information here on what these tokens for.

3. Create Access Tokens: You’ll need these to make successful requests

OAuth requests a few tokens. So you need to have them generated for you.

enter image description here

Click “create my access token” at the bottom, then once you scroll to the bottom again, you’ll have some newly generated keys. You need to grab the four previously labelled keys from this page for your API calls, so make a note of them somewhere.

4. Change Access Level: You don’t want read-only, do you?

If you want to make any decent use of this API, you’ll need to change your settings to Read & Write if you’re doing anything other than standard data retrieval using GET requests.

enter image description here

Choose the “Settings” tab near the top of the page.

enter image description here

Give your application read / write access, and hit “Update” at the bottom.

You can read more about the applications permission model that Twitter uses here.

5. Write code to access the API: I’ve done most of it for you

I combined the code above, with some modifications and changes, into a PHP class so it’s really simple to make the requests you require.

This uses oAuth and the Twitter v1.1 API, and the class I’ve created which you can find below.

require_once('TwitterAPIExchange.php'); /** Set access tokens here - see: **/ $settings = array( 'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN", 'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET", 'consumer_key' => "YOUR_CONSUMER_KEY", 'consumer_secret' => "YOUR_CONSUMER_SECRET" );

Make sure you put the keys you got from your application above in their respective spaces.

Next you need to choose a URL you want to make a request to. Twitter has their API documentation to help you choose which URL and also the request type (POST or GET).

/** URL for REST request, see: **/ $url = ''; $requestMethod = 'POST';

In the docs, each url states what you can pass to it. If we’re using the “blocks” URL like the one above, I can pass the following POST parameters:

/** POST fields required by the URL above. See relevant docs as above **/ $postfields = array( 'screen_name' => 'usernameToBlock', 'skip_status' => '1' );

Now that you’ve set up what you want to do with the API, it’s time to make the actual request.

/** Perform the request and echo the response **/ $twitter = new TwitterAPIExchange($settings); echo $twitter->buildOauth($url, $requestMethod) ->setPostfields($postfields) ->performRequest();

And for a POST request, that’s it!

For a GET request, it’s a little different. Here’s an example:

/** Note: Set the GET field BEFORE calling buildOauth(); **/ $url = ''; $getfield = '?username=J7mbo'; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); echo $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest();

Final Code Example: For a simple GET request for a list of my followers.

$url = ''; $getfield = '?username=J7mbo&skip_status=1'; $requestMethod = 'GET'; $twitter = new TwitterAPIExchange($settings); echo $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest();

I’ve put these files on Github with credit to @lackovic10 and @rivers! I hope someone finds it useful, I know I did (I used it for bulk blocking in a loop).

If anyone finds it useful – an upvote / comment on Github would go a long way. Cheers!