我对Twitter的使用完全陌生,从来没有在任何项目中嵌入过“最新推文”。我只是试图嵌入3-4最新的推文在网站页脚没有额外的功能功能。我已经研究如何做到这一点已经有一段时间了,现在遇到了一些麻烦。
我将以下代码片段添加到项目中,它工作得很好,但是,我不确定如何更新代码片段,因此它使用我的Twitter帐户,而不是设置它的帐户。
<div id="twitter_update_list">
</div>
<script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stackoverflow&include_rts=true&count=4&callback=twitterCallback2">
</script>此外,我一直在读到,最常用的Twitter API将很快停止工作,因为Twitter希望人们使用自己的API,而不是第三方。
我不确定如何从这里开始。我非常感谢在这方面的任何建议。总而言之,我所要做的就是从我的账户上抓取3-4条最新的推文。
首先要感谢大家!
发布于 2013-06-12 12:29:25
所以你真的不想再做这个客户端了。(刚刚查阅了大量文档,开发人员建议做所有oAuth服务器端的工作)
你需要做的是:
First:在https://dev.twitter.com上注册,然后创建一个新应用程序。
Second:注意:您的消费者密钥/机密以及访问令牌/机密
Third:下载Twitter库(在本例中我使用的是OAuth库https://github.com/abraham/twitteroauth,其他库位于以下位置:https://dev.twitter.com/docs/twitter-libraries)
PHP Fourth:(如果使用)如果你在台灯上运行,请确保启用了cURL。下面是你需要的命令:
sudo apt-get install php5-curlPHP file :创建一个新的文件并插入以下内容:感谢Tom Elliot http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/
<?php
session_start();
require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library you downloaded in step 3
$twitteruser = "twitterusername"; //user name you want to reference
$notweets = 30; //how many tweets you want to retrieve
$consumerkey = "12345"; //Noted keys from step 2
$consumersecret = "123456789"; //Noted keys from step 2
$accesstoken = "123456789"; //Noted keys from step 2
$accesstokensecret = "12345"; //Noted keys from step 2
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echo json_encode($tweets);
echo $tweets; //testing remove for production
?>和boom你们玩完了我知道这不是一个纯粹的js解决方案,但再次通读新的Twitter API 1.1文档,他们真的不希望你这样做客户端。希望这能有所帮助!
发布于 2015-03-10 01:53:11
如何仅使用核心PHP功能(不需要CURL或Twitter oAuth库)获取用户的最近几条tweet:
// auth参数$api_key = urlencode('REPLACEWITHAPPAPIKEY');//消费密钥(API Key) $api_secret = urlencode('REPLACEWITHAPPAPISECRET');//消费秘密(API Secret) $auth_url = 'https://api.twitter.com/oauth2/token';//我们想要什么?$data_username =‘独立’;//用户名$data_count = 10;//推文数量$data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended';//获取接口访问令牌$api_credentials = base64_encode($ api _ Key .':'.$api_ Secret );$auth_headers =‘授权:基本’.$api_credentials。“\r\n”。‘内容类型: application/x-www-form-urlencoded;charset=UTF-8'."\r\n";$auth_context = stream_context_create( 'http’=>数组( 'header‘=> $auth_headers,'method’=> 'POST','content'=> http_build_query(数组(‘grant_type’=> 'client_credentials',)),) );$auth_response = json_decode(file_get_contents($auth_url,0,$auth_context),true);$auth_token =$auth_response‘’access_token‘;// get推文$data_context = stream_context_create( array( 'http’=>数组( 'header‘=>’授权:承载'.$auth_token."\r\n",)) );result = json_decode(file_get_contents($data_url.'&count='.$data_count.'&screen_name='.urlencode($data_username),0,result),print_r($data); );// $data -按需打印(‘’);$data_context
使用XAMPP for Windows和Centos6默认安装(PHP5.3)进行了测试
最可能的问题可能是php.ini中没有启用openssl。
修复检查php.ini中是否存在extension=php_openssl.dll或extension=php_openssl.so行且未注释的步骤
发布于 2013-09-05 18:35:44
实际上,twitter有很多限制,因为有很多来自耐克和其他公司的竞争。tweet的阅读受到限制,如果你正在通过最新的API阅读,它实际上有点落后于时间。
他们还控制了DM延迟,这意味着即使你这样做了,你也不能立即DM,对方只会在X个时间量之后收到。如果你通过脚本这样做,即使你试图从一个单独的ip进行很多DM,twitter也会简单地阻止你。
https://stackoverflow.com/questions/17049821
复制相似问题