2011-07-22 148 views
1

我想弄清楚如何讓我的用戶通過訪問我的網站上的鏈接發佈推文。這是我設計的過程:通過API發佈到Twitter

  1. 我的網站某處的鏈接將提供:「發送Twit並收到優惠券」。
  2. 一旦他們點擊優惠鏈接,他們被重定向到Twitter進行授權。
  3. 當他們授予連接他們的Twitter個人資料的權限之後,我想發送一個帖子,使用Twitter API和一個預先填充的消息,如:「免費試用這個在線工具:http」// mylink .COM」的消息發佈後,他們需要被重定向到我的網站的頁面中有一個優惠券代碼。

我得到了約75%迄今所做的。

  1. 我安裝twitterOauth library
  2. 基於演示示例,我現在可以執行以下操作: - - 鏈接到Twitter(DEMO) - 與Twitter 登錄用戶 - 重定向回我的網站

我還需要弄清楚的是如何發佈Twitter消息...理想情況下,我想顯示將發佈的消息,但沒有編輯它的功能,只是爲了讓他們知道。我可以在第一頁上顯示它,這意味着在重定向到優惠券代碼之前,一旦他們授予Twitter權限,我想自動發佈它。我在哪裏添加POST功能?

這裏是return.php代碼。

<?php 
/* Start session and load library. */ 
session_start(); 
require_once('twitteroauth/twitteroauth.php'); 
require_once('config.php'); 

// This is where we end up when the user comes back from twitter. 
// First, we creat a new connection object 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 
// Then we use it to send a twitter message 

$connection->post('statuses/update', array('status' => 'Test message')); 

// Finally, we redirect the user to the coupon page 
header('Location: /privacy'); // Supplies user with coupon 
?> 

回答

3

當Twitter將用戶重定向到您的網站時,發送Tweet,然後使用優惠券代碼加載頁面。儘管確保你已通知用戶,如果他們授權你的應用,他們的狀態將被更新。

你忘了交換的access_tokens:

<?php 
/* Start session and load library. */ 
session_start(); 
require_once('twitteroauth/twitteroauth.php'); 
require_once('config.php'); 

// This is where we end up when the user comes back from twitter. 
// First, we creat a new connection object 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 

$token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']); 
$_SESSION['oauth_token'] = $token_credentials['oauth_token'] 
$_SESSION['oauth_token_secret'] = $token_credentials['oauth_token_secret']; 

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 

// Then we use it to send a twitter message 

$connection->post('statuses/update', array('status' => 'Test message')); 

// Finally, we redirect the user to the coupon page 
header('Location: /privacy'); // Supplies user with coupon 
?> 
+0

是的,我會清楚的前期,他們甚至去到Twitter之前,甚至顯示更新的文本。我正在努力的是如何發佈信息。請使用代碼查看我更新的問題。 – santa

+0

@santa檢查我在第11-13行中添加的代碼,獲取訪問令牌。 – Arvin

+0

工作!謝謝!一個失蹤「;」上$ _SESSION ['oauth_token']行... – santa