使用基本 oauth 发送推文 [英] Using basic oauth to send a tweet

查看:56
本文介绍了使用基本 oauth 发送推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was using basic auth to send tweets from a server every time a song changed. Now they have blocked basic auth and I am not sure how to incorporate it. I have a server at home that updates an html file on the webserver and then calls the following script to tweet out from that file. Any ideas on how to accomplish this simply?

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

推荐答案

http 下载最新版本的 TwitterOAuth://github.com/abraham/twitteroauth/downloads 解压下载并将 twitteroauth.php 和 OAuth.php 文件放在与具有以下代码的文件相同的目录中.在 http://dev.twitter.com/apps 注册一个应用程序,然后从您的新应用程序详情页面点击在我的访问令牌"上获取您的访问令牌.在下面的脚本中填写四个必需的变量,然后您可以运行它来发布新推文.

Download the latest version of TwitterOAuth from http://github.com/abraham/twitteroauth/downloads Unpack the download and place the twitteroauth.php and OAuth.php files in the same directory as a file with the following code. Register an application at http://dev.twitter.com/apps and from your new apps details page click on "my access token" to get your access token. Fill the four required variables into the script below and you can then run it to post new 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'));

这篇关于使用基本 oauth 发送推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆