web-dev-qa-db-ja.com

Twitter APIをセットアップして、最後のいくつかのツイートを取得する

私は一般的にTwitterを使用するのはまったく初めてで、どのプロジェクトにも「最新のツイート」を埋め込んだことはありません。機能の追加機能なしで、3〜4個の最新のツイートをサイトフッターに埋め込むだけです。私はかなり長い間これを行う方法を研究しており、いくつかの問題を抱えています。

次のコードスニペットをプロジェクトに追加しましたが、これは非常にうまく機能しますが、スニペットを更新する方法がわからないため、設定されているTwitterアカウントではなく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が望んでいるため、最も一般的に使用されているTwitter APIがすぐに機能しなくなることを読み続けています。

ここから先に進む方法がわかりません。この点に関する提案をいただければ幸いです。要約すると、私がやろうとしていることは、アカウントから最新の3〜4個のツイートを取得することだけです。

事前に感謝します!

27
AnchovyLegend

したがって、あなたは本当にこのクライアント側をもうやりたくありません。 (多くのドキュメントを調べただけで、開発者はoAuthサーバー側)

するべきこと:

最初のhttps://dev.Twitter.com にサインアップし、新しいアプリケーションを作成します。

Second:注:コンシューマキー/シークレットとアクセストークン/シークレット

Third:TwitterをダウンロードoAuth Library(この場合、PHPライブラリ https://github.com/abraham/twitteroauth 、ここにある追加ライブラリ: https://dev.Twitter.com/docs/Twitter-libraries

4番目:(phpを使用している場合)cAMPが有効になっていることを確認します。LAMPで実行する場合は、次のコマンドが必要です。

Sudo apt-get install php5-curl

5番目:新しいPHPファイルを作成し、以下を挿入します。ありがとうございます。TomElliot 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ドキュメントをもう一度読んでいます。彼らは本当にこのクライアントサイトをやりたくありません。これを願って助けて!

62
Starboy

コアPHP機能のみ(CURLまたはTwitterなしoAuthライブラリは必要ありません)で、ユーザーの最後のいくつかのツイートを取得する方法:

  1. アプリ/ウェブページを登録します https://apps.Twitter.com (個人アカウントの携帯電話番号も確認する必要がある場合があります)

  2. コンシューマキーとコンシューマシークレットに注意してください

  3. PHPコード:

    // auth parameters
    $api_key = urlencode('REPLACEWITHAPPAPIKEY'); // Consumer Key (API Key)
    $api_secret = urlencode('REPLACEWITHAPPAPISECRET'); // Consumer Secret (API Secret)
    $auth_url = 'https://api.Twitter.com/oauth2/token'; 
    
    // what we want?
    $data_username = 'Independent'; // username
    $data_count = 10; // number of tweets
    $data_url = 'https://api.Twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended';
    
    // get api access token
    $api_credentials = base64_encode($api_key.':'.$api_secret);
    
    $auth_headers = 'Authorization: Basic '.$api_credentials."\r\n".
                    'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'."\r\n";
    
    $auth_context = stream_context_create(
        array(
            'http' => array(
                'header' => $auth_headers,
                'method' => 'POST',
                'content'=> http_build_query(array('grant_type' => 'client_credentials', )),
            )
        )
    );
    
    $auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true);
    $auth_token = $auth_response['access_token'];
    
    // get tweets
    $data_context = stream_context_create( array( 'http' => array( 'header' => 'Authorization: Bearer '.$auth_token."\r\n", ) ) );
    
    $data = json_decode(file_get_contents($data_url.'&count='.$data_count.'&screen_name='.urlencode($data_username), 0, $data_context), true);
    
    // result - do what you want
    print('<pre>');
    print_r($data);
    

XAMPP for WindowsおよびCentos6のデフォルトインストールでテスト済み(PHP 5.3)

これに関する最も可能性の高い問題は、php.iniでopensslが有効になっていないことです。

Extension = php_openssl.dllまたはextension = php_openssl.soの行がphp.iniに存在し、コメントが解除されているかどうかを確認するには、修正します。

25
Rauli Rajande