使用Get以下状态API 1.1时呈现在鸣叫链接 [英] Rendering links in tweet when using Get Statuses API 1.1

查看:129
本文介绍了使用Get以下状态API 1.1时呈现在鸣叫链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Twitter的API 1.1获取状态方法从客户端的网站的帐户返回最新的鸣叫。这是工作的罚款,但我无法找到如何渲染可能包含的任何链接的任何明确的文件(包括两个用户名和包括链接)作为可点击的链接?

我可以在JSON响应任何包含链接是在XML,但它不是我清楚如何去增加点击链接进入渲染输出看到。围绕新API的文档似乎缺乏实际的例子。

谁能指教?

在code我使用拉出最新的tweet如下:

  $令牌='TOKEN这里;
$ token_secret ='令牌密钥HERE;
$ CONSUMER_KEY ='消费者这里的关键;
$ CONSUMER_SECRET ='用户端密钥HERE;$主机='api.twitter.com';
$方法=GET;
$ PATH ='/1.1/statuses/user_timeline.json'; // API调用路径$查询=阵列(//查询参数
SCREEN_NAME'=> 网名HERE,
'数'=> '1'
);$ OAuth的=阵列(
oauth_consumer_key'=> $ CONSUMER_KEY,
组oauth_token'=> $令牌,
oauth_nonce'=> (字符串)mt_rand()//建议更强的随机数
oauth_timestamp'=>时间(),
oauth_signature_method'=> HMAC-SHA1,
oauth_version'=> '1.0'
);$ OAuth的= array_map(rawurlen code,$ OAuth的); //必须连接codeD排序前
$查询= array_map(rawurlen code,$查询);$ ARR = array_merge($ OAuth的,$查询); //然后进行排序相结合的值ASORT($ ARR); //第二排序(值)
ksort($ ARR); //主排序(键)// http_build_query自动连接codeS,但我们的参数
//已经连接codeD,而且必须是由这点,所以我们取消
//编码步骤
$查询字符串= urlde code(http_build_query($改编,'','和;'));$ URL =htt​​ps://开头$主机$ PATH//混搭一切融合在一起的文本散列
$ base_string = $法和安培;rawurlen code($网址)。&放大器;rawurlen code($查询字符串)。//用相同的关键
$键= rawurlen code($ CONSUMER_SECRET)。&放大器;rawurlen code($ token_secret)。//生成哈希
$签名= rawurlen code(base64_en code(hash_hmac(SHA1',$ base_string,$关键,真正的)));//这一次,我们使用的是正常的GET查询,我们只编码查询参数
//(不OAuth的PARAMS)
。$ URL = http_build_query($查询)?。$的OAuth ['oauth_signature'] = $签名; //不想放弃所有的工作!
ksort($ OAuth的); //可能没有必要,但Twitter的演示做它//也没有必要,但Twitter的演示做到这一点太
功能add_quotes($ STR){返回'$ str中。''; }
$ OAuth的= array_map(add_quotes,$ OAuth的);//这是授权线的全部价值
$ AUTH =OAuth的。 urlde code(http_build_query($ OAuth的,'',''));//如果你正在做后期,你需要跳过上面的GET建设
//而是提供查询参数CURLOPT_POSTFIELDS
$选项=数组(CURLOPT_HTTPHEADER =>阵列(授权:$ AUTH),
// CURLOPT_POSTFIELDS => $ postfields,
CURLOPT_HEADER =>假,
CURLOPT_URL => $网址,
CURLOPT_RETURNTRANSFER =>真正,
CURLOPT_SSL_VERIFYPEER =>假);//做我们的业务
$饲料= curl_init();
curl_setopt_array($饲料,$选项);
$ JSON = curl_exec($饲料);
curl_close($饲料);$ twitter_data = json_de code($ JSON);


解决方案

非常感谢您的回复。其实,我找到了一个解决方案,得益于在阿什维尔的家伙这个博客帖子 - <一个href=\"http://www.appliedtns.com/blog/tag/twitter/\">http://www.appliedtns.com/blog/tag/twitter/

它工作正常的我。

  //解析在我们的鸣叫中发现的任何链接
$格式化文本= preg_replace('/(\\ B(WWW \\ | HTTP \\:\\ / \\ /)\\ S + \\ B)/',&LT;目标='_空白HREF ='$ 1'&GT; $ 1 LT; / A&gt;中,$后&gt;文字);
$格式化文本= preg_replace('/ \\#(\\ w +)/',&LT;目标='_空白HREF =HTTP://search.twitter.com/search Q = $ 1'&GT;# $ 1所述; / A&gt;中,$格式化文本);
$格式化文本= preg_replace('/ \\ @(\\ w +)/',&LT;目标='_空白HREF =HTTP://twitter.com/$1'> @ $ 1 LT; / A&GT; ,$格式化文本);

I'm using the Twitter API 1.1 Get statuses method to return the latest tweet from an account on the client's website. This is working fine but I can't find any clear documentation on how to render any links that may be included (Both included usernames and included links) as clickable links?

I can see in the JSON response that any included links are in the XML but it's not clear to me how to go about adding clickable links into the rendered output. The documentation around the new API seems to be lacking practical examples.

Can anyone advise?

The code I'm using the pull out the latest tweet is as follows:

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

解决方案

Thanks a lot for your response. I actually found a solution thanks to this blog post from the guys at Asheville - http://www.appliedtns.com/blog/tag/twitter/

It works fine for me.

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

这篇关于使用Get以下状态API 1.1时呈现在鸣叫链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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