2010-09-03 103 views
0

每當歌曲發生變化時,我都使用基本身份驗證從服務器發送推文。現在他們已經阻止了基本身份驗證,我不知道如何合併它。我有一臺家中的服務器,用於更新網絡服務器上的html文件,然後調用以下腳本從該文件中發出信息。任何想法如何完成這個簡單?使用基本的oauth發送推文

<?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

if format_your_code: readibility ++ – denniss 2010-09-03 05:47:01

回答

1

下載從http://github.com/abraham/twitteroauth/downloads解壓下載TwitterOAuth對象的最新版本,並放置在同一個目錄中twitteroauth.php和OAuth.php文件,用下面的代碼文件。在http://dev.twitter.com/apps處註冊應用程序,然後從新的應用程序詳細信息頁面單擊「我的訪問令牌」以獲取您的訪問令牌。將四個必需的變量填入下面的腳本中,然後運行它發佈新的推文。

<?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

這樣做了。謝謝。 – 2010-10-23 17:37:30