使用基本的oauth发送推文

时间:2010-09-03 05:45:04

标签: twitter oauth

每次更改歌曲时,我都使用基本身份验证来从服务器发送推文。现在他们已经阻止了基本身份验证,我不知道如何合并它。我家里有一台服务器更新了网络服务器上的html文件,然后调用以下脚本从该文件发出推文。关于如何完成这个的任何想法都很简单?

  <?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="";
$f=@fopen(htmlfile, "r");

if ($f!=0)
{
  $da=@fread($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 个答案:

答案 0 :(得分: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'));