disign | PHP: visualizza i tweet in un sito web | a digital sign for a best design!
A digital sign for a best design

Articolo stampato da http://disign.improntedigitali.it/php-visualizza-i-tweet-in-un-sito-web-436-2-1.html

© Luca Frassineti

Programmazione 4.235 visualizzazioni

PHP: visualizza i tweet in un sito web

07/09/2012

Un bellissimo script di Andrew MacBean, su cui sono intervenuto modificando alcune formattazioni e portando la data in formato italiano, utilissimo per pubblicare su un sito web esterno i propri (o altrui) tweet.

Lo script estrae e presenta i tweet in formato di lista, da associare a un foglio stile per garantirne la formattazione personalizzata.

Tra le diverse opzioni modificabili segnalo la variabile $nooftweets tramite cui si determina il numero di tweet estratti.

Infine la chiamata alla funzione get_tweets(@LucaFrassineti) in cui si assegna il nome dell'utente Twitter di cui presentarei tweet!

 

 /*
    Description: Twitter PHP code
    Author: Andrew MacBean
    Version: 1.0.0
    */
    
    /** Method to make twitter api call for the users timeline in XML */
    function twitter_status($twitter_id) {   
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, "https://twitter.com/statuses/user_timeline/$twitter_id.xml");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($c, CURLOPT_TIMEOUT, 5);
    $response = curl_exec($c);
    $responseInfo = curl_getinfo($c);
    curl_close($c);
    if (intval($responseInfo['http_code']) == 200) {
    if (class_exists('SimpleXMLElement')) {
    $xml = new SimpleXMLElement($response);
    return $xml;
    } else { 
    return $response;
    }
    } else { 
    return false;
    }
    }
    
    /** Method to add hyperlink html tags to any urls, twitter ids or hashtags in the tweet */
    function processLinks($text) {
    $text = utf8_decode( $text );
    $text = "<strong>" . $text;
    $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '</strong><br /><em>leggi tutto</em>&nbsp;<a href="$1" target="_blank">$1</a>', $text );
    $text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"https://www.twitter.com/\\2\" >@\\2</a>'", $text);
    $text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"https://hashtags.org/search?query=\\2\" >#\\2</a>'", $text);
    return $text;
    }
   
    /** Main method to retrieve the tweets and return html for display */    function get_tweets($twitter_id,
    $nooftweets=100,
    $dateFormat="w/n/Y  H:i",
    $includeReplies=false, $dateTimeZone="Europe/Rome",
    $beforeTweetsHtml="<ul>",
    $tweetStartHtml="<li>",
    $tweetMiddleHtml="<br />",
    $tweetEndHtml="</li>",
    $afterTweetsHtml="</ul>") {
   

     
     
    date_default_timezone_set($dateTimeZone);
    if ( $twitter_xml = twitter_status($twitter_id) ) {
    $result = $beforeTweetsHtml;
    foreach ($twitter_xml->status as $key => $status) {
    if ($includeReplies == true | substr_count($status->text,"@") == 0 | strpos($status->text,"@") != 0) {
    $message = processLinks($status->text);
   
    setlocale(LC_TIME, 'ita', 'it_IT');
    $quando = strtotime($status->created_at);
    $quando = strftime("%A %#d %B %Y", $quando);
   
    $result.="<p>".$tweetStartHtml.$message.$tweetMiddleHtml.$quando.$tweetEndHtml."</p>";
    ++$i;
    if ($i == $nooftweets) break;
    }
    }
    $result.=$afterTweetsHtml;
    }
    else {
    $result.= $beforeTweetsHtml."<li>Twitter in questo momento non &egrave; raggiungibile</li>".$afterTweetsHtml;
    }   
    echo $result;  
    }
   
    get_tweets(@LucaFrassineti);

Invia una domanda o un commento sull'articolo

I campi obbligatori sono segnalati *

*

*



 


Questo sito è totalmente privo di pubblicità, se volete sostenere questa inziativa, potete scegliere di fare una piccola donazione libera:


© 2009 - 2024 Impronte digitali di Luca Frassineti. Tutti i diritti riservati.