api - 使用 Get Statuses API 1.1 时呈现推文中的链接

标签 api twitter hyperlink status

我正在使用 Twitter API 1.1 获取状态方法从客户网站上的帐户返回最新推文。这工作正常,但我找不到任何关于如何将可能包含的任何链接(包括用户名和包含的链接)呈现为可点击链接的明确文档?

我可以在 JSON 响应中看到任何包含的链接都在 XML 中,但我不清楚如何将可点击链接添加到呈现的输出中。有关新 API 的文档似乎缺乏实际示例。

谁能给点建议吗?

我使用的提取最新推文的代码如下:

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

最佳答案

非常感谢您的回复。我实际上找到了一个解决方案,这要归功于阿什维尔的人的这篇博客文章 - 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);

关于api - 使用 Get Statuses API 1.1 时呈现推文中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610968/

相关文章:

python - 如何从 auth_user django python 中选择给定用户名和哈希密码的用户

java - Lucene评分函数

javascript - Chrome 中的 "insecure content"用于书签

css - 棘手的 CSS 纠结 : Link styles from separate divs all seeming to pull style from elsewhere

facebook - iOS Facebook 应用程序链接不起作用

python - 如何修改lxml自动链接更加自由?

ruby-on-rails - 使用 shopify Price Rules api 在 RoR 中创建折扣

vb.net - 在 VB.net 中加载 GET/POST 请求的结果

api - NginX(开源,不是Nginx plus)支持API网关吗?

android - 找不到处理 Intent twitter android 的 Activity