Category: PHP


Estuve buscando un poco sobre el uso de la nueva versión del API de twitter 1.1, encontré en http://stackoverflow.com 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 http://dev.twitter.com/apps 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: https://dev.twitter.com/apps/ **/ $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: https://dev.twitter.com/docs/api/1.1/ **/ $url = 'https://api.twitter.com/1.1/blocks/create.json'; $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 = 'https://api.twitter.com/1.1/followers/ids.json'; $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 = 'https://api.twitter.com/1.1/followers/list.json'; $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!

Anuncios

Estuve buscando una librería que permitiera hacer zoom a un contenido de un div y que este a su vez permitirá tomar todos sus elementos hijos HTML y los re dimensionara manteniendo las posiciones absolutas que se le fueron asignadas por temas de presentación.. encontre bastante para imagenes muy especiales para tiendas de mercado o galerias de arte.. pero solo eran para objetos img; por otro lado enocntre la función animate de jquery, que le podemos decir:

$(“#midiv”).animate({ ‘zoom’: scale }, 1) ;

Donde scale es la escala que deseamos del original, la original es 1, entonces si queremos el doble es 2, el tripple 3, etc…

Es bastante util pero la mala noticia es que solo sirve Chrome ya que lo que hace es añadir un tag: zoom al estilo del div padre; cosa que no reconoce aun mozilla ni IE.

Entonces el paso siguiente fue hallar que tags CSS  permitían esta actividad nativa mente, aquí fue donde halle en este link la respuesta de Matt en la que dice que los tags son:

ms-transform: scale(your value);
-moz-transform: scale(your value);
-o-transform: scale(your value);
-webkit-transform: scale(your value);
transform: scale(your value);

Ahora aca es donde entra JQuery, lo unico que se debe hacer es un metodo que reciba ese valor de la escala y lo actualice dada una acción, por ejemplo:

function updateSizeOfCategories(difZoom){

//aqui se realiza un calculo que sea mayor a la unidad sobre un valor porcentual
scale=1+(difZoom/100);
$(“#midiv”).css(‘-moz-transform’,’scale(‘+scale+’)’);
$(“#midiv”).css(‘-ms-transform’,’scale(‘+scale+’)’);
$(“#midiv”).css(‘-moz-transform’,’scale(‘+scale+’)’);
$(“#midiv”).css(‘-o-transform’,’scale(‘+scale+’)’);
$(“#midiv”).css(‘-webkit-transform’,’scale(‘+scale+’)’);
$(“#midiv”).css(‘transform’,’scale(‘+scale+’)’);
}

Y listos, esta acción se llama dada una acción (click, scroll, controles externos,slider, etc.) que necesitamos y el hace el zoom desde el div padre a todos sus elementos hijos; recomendable que el div padre del que le estamos haciendo zoom tenga un overlay: hidden, para no generar scroll y tal vez usar un drag para navegar.