Belém-te-vi: Utilizando a API do Twitter em PHP

Compreendendo a API do Twitter e como ela foi utilizada no www.belemtevi.com.br »

Belém-te-vi
Utilizando a API do twitter em PHP
API
Application Programming Interface, 
uma série de funções que o Twitter 
disponibiliza para que qualquer 
pessoa acesse os recursos do sistema. 
Tipos de
Tweet Button
http://dev.twitter.com/pages/tweet_button
@anywhere
http://dev.twitter.com/anywhere/begin
Hovercards
<script src="http://platform.twitter.com/anywhere.js?id=YOUR_API_KEY&v=1" type="text/javascript"></script>

Tweet Box
Parameters
counter:

height:

width:

label:

defaultContent:


counter:
exibe o contador de caracteres
define a altura da caixa
define a largura da caixa
a pergunta ou texto acima da caixa
conteúdo padrão dentro da caixa
ex.: @sbvirtual
Login & Sign-Up
twttr.anywhere(function (T) {

    var currentUser, screenName, profileImage, profileImageTag;

    if (T.isConnected()) {
      currentUser = T.currentUser;
      screenName = currentUser.data('screen_name');
      profileImage = currentUser.data('profile_image_url');
      profileImageTag = "<img src='" + profileImage + "'/>";
      $('#twitter-connect-placeholder').append("Logado como " + profileImageTag + " " + screenName);
      T("#twitter-connect-placeholder").connectButton({
          signOut: function() {  
             location.reload();  
          }
  });
      $("#login-logout").append('<button id="signout" type="button">Logout do Twitter</button>');

        $("#signout").bind("click", function () {
          twttr.anywhere.signOut();
        });

    } else {
      T("#twitter-connect-placeholder").connectButton();
    };

  });
Streaming
Search
REST
Não foi usada no @belemtevi
http://dev.twitter.com/pages/streaming_api
A API REST permite realizar ações no Twitter. Para PHP utilizaremos a biblioteca CURL
http://curl.haxx.se/
Lendo a TimeLine
<?php

     $username = 'xxx';
 $password = 'yyy';
     $curlhandle = curl_init();
 curl_setopt($curlhandle, CURLOPT_URL, "http://twitter.com/statuses/user_timeline.xml");
 curl_setopt($curlhandle, CURLOPT_USERPWD, $username.':'.$password);
     curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
     $response = curl_exec($curlhandle);
     curl_close($curlhandle);
     $xmlobj = new SimpleXMLElement($response); 

 foreach ($xmlobj->status as $status)
 {
    echo $status->text.'<br /> by'.$status->user->screen_name.' at '
    .date("g:i: A D, F jS Y",strtotime($status->created_at)).'<br /> <br /> ' ;
    }
?> 

Publicando um Tweet
<?php
    $username = 'xxx';
    $password = 'yyy';
    $status = 'Teste do Curl e Twitter'

    $curlhandle = curl_init();
        curl_setopt ($curlhandle, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
        curl_setopt($curlhandle, CURLOPT_USERPWD, $username.':'.$password);
    curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlhandle, CURLOPT_POST, 1);
    curl_setopt($curlhandle, CURLOPT_POSTFIELDS, "status=$status");
    $response = curl_exec($curlhandle);
    curl_close($curlhandle);

?>

Retorna os tweets segundo uma busca específica
SEARCH
URL
http://search.twitter.com/search.format?q=query
REQUEST METHODS
GET
Autenticação
false
Formatos
Atom
Json
Resultados
{"results":[
     {"text":"@twitterapi  http:\/\/tinyurl.com\/ctrefg",
     "to_user_id":396524,
     "to_user":"TwitterAPI",
     "from_user":"jkoum",
     "metadata":
     {
      "result_type":"popular",
      "recent_retweets": 100

     },
     "id":1478555574,
     "from_user_id":1833773,
     "iso_language_code":"nl",
     "source":"<a href="http:\/\/twitter.com\/">twitter<\/a>",
     "profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/118412707\/2522215727_a5f07da155_b_normal.jpg",
     "created_at":"Wed, 08 Apr 2009 19:22:10 +0000"},
     ... truncated ...],
     "since_id":0,
     "max_id":1480307926,
     "refresh_url":"?since_id=1480307926&q=%40twitterapi",
     "results_per_page":15,
     "next_page":"?page=2&max_id=1480307926&q=%40twitterapi",
     "completed_in":0.031704,
     "page":1,
     "query":"%40twitterapi"}
}
O Feed
<entry>
    <id>tag:search.twitter.com,2005:22021585658</id>
    <published>2010-08-24T18:30:02Z</published>
    <link type="text/html" href="http://twitter.com/WebObsessed/statuses/22021585658" rel="alternate"/>
    <title>Internet Marketing - http://bit.ly/9gvb6S Introducing the Google Small Business Blog http://ow.ly/18HXSO</title>
    <content type="html">Internet Marketing - &lt;a href=&quot;http://bit.ly/9gvb6S&quot;&gt;http://bit.ly/9gvb6S&lt;/a&gt; Introducing the Google Small Business Blog &lt;a href=&quot;http://ow.ly/18HXSO&quot;&gt;http://ow.ly/18HXSO&lt;/a&gt;</content>
    <updated>2010-08-24T18:30:02Z</updated>
    <link type="image/png" href="http://a1.twimg.com/profile_images/109635549/zwinky_normal.png" rel="image"/>
    <twitter:geo>
    </twitter:geo>
    <twitter:metadata>
      <twitter:result_type>recent</twitter:result_type>
    </twitter:metadata>
    <twitter:source>&lt;a href=&quot;http://www.hootsuite.com&quot; rel=&quot;nofollow&quot;&gt;HootSuite&lt;/a&gt;</twitter:source>
    <twitter:lang>en</twitter:lang>
    <author>
      <name>WebObsessed (Andrea Redmond)</name>
      <uri>http://twitter.com/WebObsessed</uri>
    </author>
  </entry>
A mágica
Search + SimplePie
http://simplepie.org/
<div class="item">
    <img src="<?php echo $item->get_link(0, 'image');?>"/>

    <p><?php echo $item->get_content();?></p>
</div>
Obrigado!
Luciano Santa Brígida (@lucianosb)
www.belemtevi.com.br
@belemtevi
www.sbvirtual.com

Loading comments...

Please log in to add your comment.

Report abuse

More presentations by Luciano Santa Brígida

More prezis by author