2010-09-03 2 views
0

J'utilisais l'authentification de base pour envoyer des tweets à partir d'un serveur chaque fois qu'un morceau était modifié. Maintenant, ils ont bloqué l'authentification de base et je ne suis pas sûr de savoir comment l'incorporer. J'ai un serveur à la maison qui met à jour un fichier HTML sur le serveur Web, puis appelle le script suivant pour tweeter à partir de ce fichier. Des idées sur la façon d'accomplir cela simplement?Utilisation d'oauth de base pour envoyer un tweet

<?php 


//==================================================== 
//   CONFIGURATION 
//==================================================== 

// YOUR TWITTER USERNAME AND PASSWORD 
$username = '#####'; 
$password = '#####'; 


DEFINE(htmlfile, '/homec/public_html/site.com/twitter.html'); 

$stationURL = "http://www.site.com"; 

$maxLimit = "139"; 

$da=""; 
[email protected](htmlfile, "r"); 

if ($f!=0) 
{ 
    [email protected]($f, 4096); 
    fclose($f); 
} 
else 
{ 
    exit; 
} 

$da=str_replace("\r", "\n", $da); 
$da=str_replace("\n\n", "\n", $da); 
$d=explode("\n", $da); 

$d[0]=trim($d[0], "|"); // title 
$d[1]=trim($d[1], "|"); // artist 

//==================================================== 
if ($d[0]=="" || $d[1]=="") 
{ 
    // IF WE COULD NOT GRAB THE ARTIST AND 
    // SONG TITLE FROM THE SAM-GENERATED HTML FILE, 
    // WE'LL BAIL OUT NOW WITHOUT SUBMITTING ANY TEXT 
    // TO TWITTER. 
    exit; 
} 
else 
{ 
    // SUCCESS IN GETTING ARTIST AND TITLE! 
    // WE'LL PROCEED WITH BUILDING A TEXT STRING TO SUBMIT TO TWITTER. 

    $message = urlencode('' . $d[1] . ' - ' . $d[0] . ' #bandradio #nowplaying '); 

    $stationURL = urlencode(' ' . $stationURL); 

    if ((strlen($message) + strlen($stationURL)) > $maxLimit) 
    { 
    // We have to truncate the artist-title string to make room for the station URL string. 
    $message = substr($message, 0, (($maxLimit - 2) - strlen($stationURL))); 
    $message .= ".." . $stationURL; 
    } 
    else 
    { 
    // No need to truncate, it all fits. 
    $message = $message . $stationURL; 
    } 
} // if ($d[0]=="" || $d[1]=="") 

//==================================================== 
// The twitter API address 
$url = 'http://twitter.com/statuses/update.json'; 

// Set up and execute the curl process 
$curl_handle = curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, "$url"); 
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl_handle, CURLOPT_POST, 1); 
//curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message"); 
//curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password"); 
$buffer = curl_exec($curl_handle); 
$resultArray = curl_getinfo($curl_handle); 
curl_close($curl_handle); 
+1

si format_your_code: ++ readibility – denniss

Répondre

1

Télécharger la dernière version de twitteroauth de http://github.com/abraham/twitteroauth/downloads Déballez le téléchargement et placez le twitteroauth.php et les fichiers OAuth.php dans le même répertoire que le fichier avec le code suivant. Enregistrez une application au http://dev.twitter.com/apps et à partir de la page de vos nouvelles applications, cliquez sur "mon jeton d'accès" pour obtenir votre jeton d'accès. Remplissez les quatre variables requises dans le script ci-dessous et vous pouvez ensuite l'exécuter pour poster de nouveaux tweets.

<?php 
require_once('twitteroauth.php'); 
$connection = new TwitterOAuth('app consumer key', 'app consumer secret', 'my access token', 'my access token secret'); 
$connection->post('statuses/update', array('status' => 'text to be tweeted')); 
+0

qui a fait l'affaire. Merci. –

Questions connexes