Jump to content

Help showing tweets from twitter.


runnerjp

Recommended Posts

 Hello,

 

I have this page http://rolleragency.co.uk/action/ and in the blue section it should show 4 tweets per user all the time but for some reason they disappear and I cannot figure out why.

 

<?php
//includes database configuration
include('dbconfig.php');
include('classmytimeline.php');


//check if tweet already exist before expire time then load

$sql = "select * FROM twitter_person";

if (!$result = $db->query($sql)) {
    die('There was an error running the query select [' . $db->error . ']');
} else {
    while ($row = $result->fetch_assoc()) {
        $user_records[] = $row;
    }
    $result->free();
}

$db_values = '';
$tweet_feeds = '';

$min = 0;
$to_time = strtotime(date("Y-m-d H:i:s"));
$from_time = strtotime($user_records[0]['FeedUpdatedDate']);
$min = round(abs($to_time - $from_time) / 60, 0);



if ($min > 10) {
    
    
    
    $sql = "select * FROM tweet_feeds";
    if (!$result = $db->query($sql)) {
        die('There was an error running the query [' . $db->error . ']');
    }

    if ($result->num_rows > 0) {
        $result->free();

        $sql = "Delete FROM tweet_feeds";

        if (!$result = $db->query($sql)) {
            die('There was an error running the query [' . $db->error . ']');
        }
    }

    foreach ($user_records as $user) {

        $mytimeline = new MyTimeLine($user['TwitterName'], 4);
        $timeline = $mytimeline->get();

        $index = 0;
        foreach ($timeline as $tweet) {

            if ($index > 4)
                $db_values .= "('" . htmlentities($tweet[0], ENT_QUOTES, 'UTF-8') . "','" . $tweet[1] . "'," . $user['ID'] . "),";
            $index++;
        }
    }

    //insert all records in database 
    $db_values = substr($db_values, 0, strlen($db_values) - 1);
    $sql = "INSERT INTO tweet_feeds (TweetText,TweetTime,twitter_person_id) Values " . $db_values;

    if (!$result = $db->query($sql)) {
        die('There was an error running the query [' . $db->error . ']');
    }

    $sql = "UPDATE twitter_person SET FeedUpdatedDate = '" . date("Y-m-d H:i:s") . "' where ID > 0";

    if (!$result = $db->query($sql)) {
        die('There was an error running the query [' . $db->error . ']');
    }
}

$sql = "SELECT *
    FROM tweet_feeds left join twitter_person on (tweet_feeds.twitter_person_id = twitter_person.id)";
//ORDER BY twitter_person.id asc, TweetTime DESC
if (!$result = $db->query($sql)) {
    die('There was an error running the query [' . $db->error . ']');
} else {
    while ($row = $result->fetch_assoc()) {
        $tweet_feeds[] = $row;
    }
    $result->free();
}

$first_tweet_feeds[] = array();

$tweeter_id = 0;
$count = 0;
foreach ($tweet_feeds as $tweet) {
    $pattern = '/(#mudder)/';
    if(preg_match($pattern,$tweet['TweetText']))
    {            
        if ($tweeter_id != $tweet['twitter_person_id']) {
            $tweetcontent = $tweet['TweetText'];
            $tweetcontent = preg_replace('/[#]+([A-Za-z0-9-_]+)/', '<a href="http://twitter.com/search?q=%23$1" target="_blank"" class="tweet_hashtag">#$1</a>', $tweetcontent);
            $first_tweet_feeds[$tweet['twitter_person_id']] = html_entity_decode($tweetcontent);
            $tweeter_id = $tweet['twitter_person_id'];
        }
    }       
}

$tweets_list = array();
foreach ($tweet_feeds as $tweet) {

    $tweets_list[$tweet['twitter_person_id']][] = $tweet;
}

//unset feed data
unset($tweet_feeds);
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>The Passengers & Roller Support Action Duchenne</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <!-- Le styles -->
        <link href="assets/css/bootstrap.css" rel="stylesheet">
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,600,700,800,300' rel='stylesheet' >


        <link rel="stylesheet" href="assets/css/font-awesome.min.css">
        <!--[if IE 7]>
        <link rel="stylesheet" href="assets/css/font-awesome-ie7.min.css">
        <![endif]-->

        <link href="assets/css/styles.css" rel="stylesheet">

        <link href="assets/css/jquery.tweet.css" rel="stylesheet" >



        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="assets/js/html5shiv.js"></script>
        <![endif]-->



<?
if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPad'))
{
?>

Classtimeline

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class MyTimeLine {
/**
 * myTimeline PHP Script
 * This script gets a user's twitter timeline and returns it as a multidimension array
 * each array containing 'tweet, date and link' respectively.
 *
 * @author Opeyemi Obembe <[email protected]>
 * @copyright Copyright (c) 2010, devEdgeLabs.
 */

	var $count;
	var $feedUrl;
	var $username;
	
	//@params: twitter username, number of needed updates (20 max)
	function myTimeline($username, $count = 20)
	{
		$this->feedUrl = 'http://api.twitter.com/1/statuses/user_timeline/'.$username.'.rss';
		$this->count = $count > 20 ? 20 : $count;
		$this->username = $username;
	}
	
	function since($date)
	{
		$timestamp = strtotime($date);
		$seconds = time() - $timestamp;
		
		$units = array(
			'second' => 1,
			'minute' => 60,
			'hour' 	 => 3600,
			'day'	 => 86400,
			'month'  => 2629743,
			'year'   => 31556926
		);
		
		foreach($units as $k => $v)
		{
			if($seconds >= $v)
			{
				$results = floor($seconds/$v);
				if($k == 'day' | $k == 'month' | $k == 'year')
					$timeago = date('D, d M, Y h:ia', $timestamp);
				else
					$timeago = ($results >= 2) ? 'about '.$results.' '.$k.'s ago' : 'about '.$results.' '.$k.' ago';
			}
		}
		
		return $timeago;
	}
	
	// Returns a multidimentional array, each containg 'tweet, date and link' respectively
	function get()
	{
		// Append the count
		$url = $this->feedUrl;
		$url .= $this->count == 20 ? '' : '?count='.$this->count;
		
		// The http CURL thingy
		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_URL, $url);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($curl_handle, CURLOPT_TIMEOUT, 10);//10 secs max
		$data = curl_exec($curl_handle);
		curl_close($curl_handle);

		// Some error? Return an empty array
		// You may want to extend this to know the exact error
		// echo curl_error($curl_handle);
		// or know the http status
		// echo curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
		if(!$data)	return array();
		
		// Some reformatting
		$pattern = array(
			'/[^(:\/\/)](www\.[^ \n\r]+)/',
			'/(https?:\/\/[^ \n\r]+)/',
			'/@(\w+)/',
			'/^'.$this->username.':\s*/i'
		);
		$replace = array(
			'<a href="http://$1" rel="nofollow">$1</a>',
			'<a href="$1" rel="nofollow">$1</a>',
			'<a href="http://twitter.com/$1" rel="nofollow">@$1</a>'.
			''
		);
		
		$tweets = array();
		$xml = new SimpleXMLElement($data);
		foreach($xml->children()->children() as $item)
		{
			$tweet = preg_replace($pattern, $replace, $item->description);
			$date = $this->since($item->pubDate);
			$permalink = $item->link;
			$tweets[] = array($tweet, $date, $permalink);
		}
		
		return $tweets;
	}
}
?>

 

 

Any help would be great.

 

 

Link to comment
https://forums.phpfreaks.com/topic/276723-help-showing-tweets-from-twitter/
Share on other sites

Hello guys. 

I have 4 posts for each users in the db but it only displays one.

 

 

$sql = "SELECT *
    FROM tweet_feeds left join twitter_person on (tweet_feeds.twitter_person_id = twitter_person.id)";
//ORDER BY twitter_person.id asc, TweetTime DESC
if (!$result = $db->query($sql)) {
    die('There was an error running the query [' . $db->error . ']');
} else {
    while ($row = $result->fetch_assoc()) {
        $tweet_feeds[] = $row;
    }
    $result->free();
}


$first_tweet_feeds[] = array();


$tweeter_id = 0;
$count = 0;
foreach ($tweet_feeds as $tweet) {
    $pattern = '/(#mudder)/';
    if(preg_match($pattern,$tweet['TweetText']))
    {            
        if ($tweeter_id != $tweet['twitter_person_id']) {
            $tweetcontent = $tweet['TweetText'];
            $tweetcontent = preg_replace('/[#]+([A-Za-z0-9-_]+)/', '<a href="http://twitter.com/search?q=%23$1" target="_blank"" class="tweet_hashtag">#$1</a>', $tweetcontent);
            $first_tweet_feeds[$tweet['twitter_person_id']] = html_entity_decode($tweetcontent);
            $tweeter_id = $tweet['twitter_person_id'];
        }
    }       
}


$tweets_list = array();
foreach ($tweet_feeds as $tweet) {


    $tweets_list[$tweet['twitter_person_id']][] = $tweet;
}


//unset feed data
unset($tweet_feeds);
 if (isset($tweets_list[5]))
                                    foreach ($tweets_list[5] as $tweet) {
                                        $tweetcontent = $tweet['TweetText'];
                                        $tweetcontent = preg_replace('/[#]+([A-Za-z0-9-_]+)/', '<a href="http://twitter.com/search?q=%23$1" target="_blank"" class="tweet_hashtag">#$1</a>', $tweetcontent);
                                        $pattern = '/(#mudder)/';
                                        if(preg_match($pattern,$tweet['TweetText']))
                                        {                                        
                                        ?>                    

                                        <li class="tweet_first tweet_odd">
                                            <a class="tweet_avatar" href="http://twitter.com/<?php echo $tweet['TwitterName']; ?>">
                                                <img src="assets/img/largeTwitter.jpg" height="32" width="32" alt="<?php echo $tweet['TwitterName']; ?> avatar" title="<?php echo $tweet['TwitterName']; ?> avatar" border="0">
                                            </a>                            
                                            <span class="tweet_text">
                                                <?php echo html_entity_decode($tweetcontent); ?>
                                            </span>
                                            <span class="tweet_time" style="color:#000;">
                                                <?php echo $tweet['TweetTime']; ?>
                                            </span>                            
                                        </li>

                                        <?php
                                        }
                                    }
                                ?>   

Someone must be able to spot why

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.