简体   繁体   English

Twitter推文PHP错误

[英]Twitter Tweets PHP Error

I cannot include my tweets.php file, it gives this error when I try to include it with php: Fatal error: Cannot use object of type stdClass as array in C:\\xampp\\htdocs\\Documents\\PHP\\Projects\\HassanTech\\tweets.php on line 56 我无法包含我的tweets.php文件,当我尝试将其包含在php中时,它会出现此错误:致命错误:无法将stdClass类型的对象用作C:\\ xampp \\ htdocs \\ Documents \\ PHP \\ Projects \\ HassanTech \\ tweets中的数组.php行56

The source code for the file is: 该文件的源代码为:

    <?php

require_once('twitteroauth.php');


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

$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]; // LINE 56
    //$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;
?>

Please help me out, thank you. 请帮帮我,谢谢。 I will really appreciate it. 我会很感激的。 If you need any extra information, please let me know. 如果您需要任何其他信息,请告诉我。 Thank you. 谢谢。 I know you guys are really smart and can surely help me out. 我知道你们真的很聪明,一定可以帮助我。 Sorry for bad english. 对不起,英语不好。

The response returned by twitter API is in json format, You need to convert it to array. twitter API返回的响应为json格式,您需要将其转换为数组。

$feed = json_decode($feed);

The above line will convert json into an array. 上一行将json转换为数组。 Use it after you get API response. 得到API响应后,请使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM