2013-03-25 23 views
4

我正在使用Twitter API 1.1獲取狀態方法以從客戶端網站上的帳戶返回最新的推文。這工作正常,但我無法找到任何明確的文檔如何呈現可能包含的任何鏈接(包括用戶名和包含的鏈接)作爲可點擊的鏈接?使用獲取狀態API時在推文中呈現鏈接1.1

我可以在JSON響應中看到任何包含的鏈接都在XML中,但我不清楚如何去將可點擊鏈接添加到呈現的輸出中。圍繞新API的文檔似乎缺乏實際的例子。

任何人都可以建議嗎?

的代碼我使用的是拉出最新的tweet如下:

$token = 'TOKEN HERE'; 
$token_secret = 'TOKEN SECRET HERE'; 
$consumer_key = 'CONSUMER KEY HERE'; 
$consumer_secret = 'CONSUMER SECRET HERE'; 

$host = 'api.twitter.com'; 
$method = 'GET'; 
$path = '/1.1/statuses/user_timeline.json'; // api call path 

$query = array(// query parameters 
'screen_name' => 'SCREEN NAME HERE', 
'count' => '1' 
); 

$oauth = array(
'oauth_consumer_key' => $consumer_key, 
'oauth_token' => $token, 
'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended 
'oauth_timestamp' => time(), 
'oauth_signature_method' => 'HMAC-SHA1', 
'oauth_version' => '1.0' 
); 

$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting 
$query = array_map("rawurlencode", $query); 

$arr = array_merge($oauth, $query); // combine the values THEN sort 

asort($arr); // secondary sort (value) 
ksort($arr); // primary sort (key) 

// http_build_query automatically encodes, but our parameters 
// are already encoded, and must be by this point, so we undo 
// the encoding step 
$querystring = urldecode(http_build_query($arr, '', '&')); 

$url = "https://$host$path"; 

// mash everything together for the text to hash 
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring); 

// same with the key 
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret); 

// generate the hash 
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true))); 

// this time we're using a normal GET query, and we're only encoding the query params 
// (without the oauth params) 
$url .= "?".http_build_query($query); 

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work! 
ksort($oauth); // probably not necessary, but twitter's demo does it 

// also not necessary, but twitter's demo does this too 
function add_quotes($str) { return '"'.$str.'"'; } 
$oauth = array_map("add_quotes", $oauth); 

// this is the full value of the Authorization line 
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', ')); 

// if you're doing post, you need to skip the GET building above 
// and instead supply query parameters to CURLOPT_POSTFIELDS 
$options = array(CURLOPT_HTTPHEADER => array("Authorization: $auth"), 
//CURLOPT_POSTFIELDS => $postfields, 
CURLOPT_HEADER => false, 
CURLOPT_URL => $url, 
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_SSL_VERIFYPEER => false); 

// do our business 
$feed = curl_init(); 
curl_setopt_array($feed, $options); 
$json = curl_exec($feed); 
curl_close($feed); 

$twitter_data = json_decode($json); 

回答

0

不知道這是否正是你需要的,但我現在用的是tmhOAuth庫我的應用程序,看到https://github.com/themattharris/tmhOAuth-examples。使用來自Matt Harris示例的代碼,我遍歷響應並構建輸出,如下面的代碼所示。推文中的鏈接由庫函數entify_with_options($tweet)創建。

// Decode response 
$timeline = json_decode($this->tmhOAuth->response['response'], true); 

if(!$timeline){ 
throw new Exception('Error: No response was found.'); 
} 
else{ 
// Start building the output 
foreach ($timeline as $tweet) : 

     ... start of response processing 

     // Format and set tweet text 
    $tw_entified_tweet = tmhUtilities::entify_with_options($tweet); 

     // Format and set creation date for permalink 
    $tw_created_at_formatted = is_twitterlist_format_date($tweet['created_at']); 

     // Format and set permalink 
    $tw_permalink = str_replace(
    array(
     '%screen_name%', 
     '%id%', 
     '%created_at%' 
    ), 
    array(
     $tweet['user']['screen_name'], 
     $tweet['id_str'], 
     $tw_created_at_formatted, 
     ), 
     '<a href="https://twitter.com/%screen_name%/status/%id%" target="_blank">%created_at%</a>' 
    ); 

     ... end response processing 


    endforeach; 
} 

日期格式功能是:

function is_twitterlist_format_date($created_date) 
{ 
    if (is_null($created_date)) { 
     return ''; 
    } 
    else{ 
     // Format: March 4th, 9:19 am 
     return date('F jS, g:i a', strtotime($created_date)); 
    } 
} 

希望這是非常有用的。

11

非常感謝您的回覆。我實際上找到了一個解決方案,感謝這位阿什維爾隊員的博客文章 - http://www.appliedtns.com/blog/tag/twitter/

它對我來說工作得很好。

// Parse any links found in our tweet 
$formatted_text = preg_replace('/(\b(www\.|http\:\/\/)\S+\b)/', "<a target='_blank' href='$1'>$1</a>", $post->text); 
$formatted_text = preg_replace('/\#(\w+)/', "<a target='_blank' href='http://search.twitter.com/search?q=$1'>#$1</a>", $formatted_text); 
$formatted_text = preg_replace('/\@(\w+)/', "<a target='_blank' href='http://twitter.com/$1'>@$1</a>", $formatted_text); 
+0

也爲我工作,感謝您在此發佈。 – 2014-03-21 13:01:34

+0

正是我正在尋找的東西 - 現貨,謝謝:) – 2015-03-18 12:37:28

+0

仍然有竅門,謝謝! – Bondt 2015-08-24 10:52:15

相關問題