簡體   English   中英

Twitter API 1.1推文

[英]Twitter API 1.1 Tweets

您好,twitter將其API更新為1.1后,我面臨一個非常嚴重的問題。 您可以看到很多變化。 例如,看一下我的代碼,在更新Twitter API之后,它不再起作用。

<?php
$content = "";
if (!isset($username)){
    $username = "Hassan_Tech";
}
if (!isset($number)){
    $number = 5;
}

$url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name={$username}&count={$number}";
$tweets = file_get_contents($url);
$feed = new SimpleXMLElement($tweets);

function time_stamp($date){
    if (empty($date)){
        return "No date provided";
    }
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");
    $now = time();
    $unix_date = strtotime($date);
    if (empty($unix_date)){
        return "Bad date";
    }
    if ($now > $unix_date){
        $difference = $now - $unix_date;
        $tense = "ago";
    } else {
        $difference = $unix_date - $now;
        $tense = "from now";
    }
    for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++){
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if ($difference != 1){
        $periods[$j] .= "s";
    }
    return "$difference $periods[$j] $tense";
}

for ($i = 0; $i <= $number-1; $i++){
    $status = $feed->status[$i];

    if (isset($status->id)){
        $id = $status->id;
    }

    $created_at = $status->created_at;
    $text = $status->text;
    $text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text);
    $text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);

    if (preg_match("/#(\w+)/", $text, $matches)){
        foreach($matches as $match){
            $match = str_replace("#", "", $match);
            $text = preg_replace("/#(\w+)/","<a href=\"http://twitter.com/search?q={$match}\">$0</a>",$text);
            $text = str_replace("<a href=\"http://twitter.com/search?q={$match}\">#","<span class=\"hash\">#</span><a href=\"http://twitter.com/search?q={$match}\" target=\"_blank\">",$text);
        }
    }

    if (preg_match("/@(\w+)/", $text, $matches)) {
        foreach($matches as $match) {
            $match = str_replace("@", "", $match);
            $text = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/{$match}\">$0</a>", $text);
            $text = str_replace("<a href=\"http://www.twitter.com/{$match}\">@", "<span class=\"at\">@</span><a href=\"http://www.twitter.com/{$match}\" target=\"_blank\">", $text);
        }
    }

    $content .= "<div class=\"status\"><div class=\"text\">{$text}</div>";

    if (isset($id)){
        $content .= "<div class=\"date\"><a href=\"http://twitter.com/Hassan_Tech/status/{$id}\" target=\"_blank\" title=\"".date("g:i A M jS", strtotime($created_at))."\">".time_stamp($created_at)."</a></div>";
    } else {
        $content .= "<div class=\"date\"><a href=\"\" target=\"_blank\" title=\"".date("g:i A M jS", strtotime($created_at))."\">".time_stamp($created_at)."</a></div>";
    }
    $content .= "</div>";
}

echo $content;
?>

你能幫我解決這個問題嗎? 我將衷心感謝您的幫助。 謝謝。

首先,您必須創建一個Twitter應用程序,然后可以使用您的Twitter憑據登錄https://dev.twitter.com/apps/new

然后單擊按鈕“創建我的訪問令牌”,然后復制您的Twitter應用程序的以下代碼:

  • 消費者密鑰
  • 消費者秘密
  • 訪問令牌
  • 訪問令牌機密

然后從此github存儲庫下載OAuth.php和twitteroauth.php,並將這些文件放入您的php腳本文件夾https://github.com/abraham/twitteroauth/tree/master/twitteroauth

最終您可以使用此代碼,我評論了已棄用的代碼

<?php

require_once('twitteroauth.php');


define('CONSUMER_KEY', '');
define('CONSUMER_SECRET', '');
define('OAUTH_TOKEN', '');
define('OAUTH_TOKEN_SECRET', '');

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);


$content = "";
if (!isset($username)){
    $username = "Hassan_Tech";
}
if (!isset($number)){
    $number = 5;
}

$feed = $connection->get('statuses/user_timeline', array('screen_name' => $username, 'count' => $number));

//$url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name={$username}&count={$number}";
//$tweets = file_get_contents($url);
//$feed = new SimpleXMLElement($tweets);

function time_stamp($date){
    if (empty($date)){
        return "No date provided";
    }
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");
    $now = time();
    $unix_date = strtotime($date);
    if (empty($unix_date)){
        return "Bad date";
    }
    if ($now > $unix_date){
        $difference = $now - $unix_date;
        $tense = "ago";
    } else {
        $difference = $unix_date - $now;
        $tense = "from now";
    }
    for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++){
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if ($difference != 1){
        $periods[$j] .= "s";
    }
    return "$difference $periods[$j] $tense";
}

for ($i = 0; $i <= $number-1; $i++){
    $status = $feed[$i];
    //$status = $feed->status[$i];

    if (isset($status->id)){
        $id = $status->id;
    }

    $created_at = $status->created_at;
    $text = $status->text;
    $text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $text);
    $text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);

    if (preg_match("/#(\w+)/", $text, $matches)){
        foreach($matches as $match){
            $match = str_replace("#", "", $match);
            $text = preg_replace("/#(\w+)/","<a href=\"http://twitter.com/search?q={$match}\">$0</a>",$text);
            $text = str_replace("<a href=\"http://twitter.com/search?q={$match}\">#","<span class=\"hash\">#</span><a href=\"http://twitter.com/search?q={$match}\" target=\"_blank\">",$text);
        }
    }

    if (preg_match("/@(\w+)/", $text, $matches)) {
        foreach($matches as $match) {
            $match = str_replace("@", "", $match);
            $text = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/{$match}\">$0</a>", $text);
            $text = str_replace("<a href=\"http://www.twitter.com/{$match}\">@", "<span class=\"at\">@</span><a href=\"http://www.twitter.com/{$match}\" target=\"_blank\">", $text);
        }
    }

    $content .= "<div class=\"status\"><div class=\"text\">{$text}</div>";

    if (isset($id)){
        $content .= "<div class=\"date\"><a href=\"http://twitter.com/Hassan_Tech/status/{$id}\" target=\"_blank\" title=\"".date("g:i A M jS", strtotime($created_at))."\">".time_stamp($created_at)."</a></div>";
    } else {
        $content .= "<div class=\"date\"><a href=\"\" target=\"_blank\" title=\"".date("g:i A M jS", strtotime($created_at))."\">".time_stamp($created_at)."</a></div>";
    }
    $content .= "</div>";
}

echo $content;
?>

它對我有用,我希望它對你也有用:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM