Issues with porting in Twitter feed?

software development

#1

Hi all,

I’m posting here, as I’ve been working on getting Twitter feeds running on some of my websites. Unfortunately, the sites that I post on my dreamhost server throw errors, while the exact same code works on other hosts. I’ve included links to the sites, as well as the code itself.

I’ve had these types of issues with different twitter-based scripts for a while now on DH, and can’t seem to pin down what it is since different scripts all have issues. In case it’s of any use in troubleshooting, I’m on the Havana server.

Is there anything I’m forgetting to activate that isn’t activated by default in DH?

Cheers,
-Danny

Links:
Dreamhost
Other hosting

(link to text file with code)

[code]

<?php $url = 'http://api.twitter.com/statuses/user_timeline/46952657.rss'; $cache_expire = 3600; // in seconds $ts = time(); $info_file = 'tmp-info.txt'; $cache_file = 'tmp-'.$ts.'.xml'; // current info $info = unserialize(@file_get_contents($info_file)); if (empty($info) OR $ts > ($info['cache_ts']+$cache_expire)) { $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); // if a description tag is present we're OK if (preg_match('//iS',$content)) { file_put_contents($cache_file,$content); @unlink($info['cache_file']); } // known error strings: "over capacity", "rate limit exceeded" // else if a description tag is not present something is wrong else { // use current cache until errors resolve itself $cache_file = $info['cache_file']; $content = file_get_contents($info['cache_file']); } // update next cache time and cache file name file_put_contents($info_file,serialize(array('cache_ts'=>$ts,'cache_file'=>$cache_file))); } else { $content = file_get_contents($info['cache_file']); } $feed = array(); $doc = new DOMDocument(); $doc->loadXML($content); foreach ($doc->getElementsByTagName('item') as $node) { array_push($feed, array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => preg_replace('/^\w+:/i','',$node->getElementsByTagName('description')->item(0)->nodeValue), 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue )); } function linkify_tweet($v) { $v = ' ' . $v; $v = preg_replace('/(^|\s)@(\w+)/', '\1@\2', $v); $v = preg_replace('/(^|\s)#(\w+)/', '\1#\2', $v); $v = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1\\2'", $v); $v = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1\\2'", $v); $v = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2@\\3", $v); return trim($v); } ?> <?php if (is_array($feed)): ?>
    <?php array_splice($feed,5); ?> <?php foreach ($feed as $item): ?>
  • <?php echo linkify_tweet($item['desc']); ?>
  • <?php endforeach; ?>
<?php endif; ?> [/code][hr] [b]UPDATE[/b]

Looked at php info, allow_call_time_pass_reference is disabled on DH and enabled on other host. Possibly the problem?

Argh don’t want to create custom install of php…


#2

A wordpress theme I use shows my twitter feed. Here’s its code, if that helps:


function theme_twitter_show($count=4)
{
	$id = obwp_get_meta(SHORTNAME."_twitter_id");
	if(!empty($id))
	{
	?>
	<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
	<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/<?php echo $id; ?>.json?callback=twitterCallback2&count=<?php echo $count; ?>"></script>
	<?php
	}
}

#3

Thanks for reply! Can you post the script too that contains the function “obwp_get_meta()”?[hr]

Sorry, I should’ve looked more closely before replying.

Basically, I need a script that will take the XML/CSS/JSON feed from twitter and interpret it so that I can post it within a page with proper classes applied. The scripts I’ve tried so far have run into problems, (I believe, due to DH’s php settings,) with parsing the feed from Twitter.

Has anyone else had similar issues in DH that they were able to resolve? I’m not even sure if this is an issue specific to parsing a Twitter feed, or just an issue in general with PHP that could be used to parse feeds while being run on DH.


#4

obwp_get_meta is an internal WordPress function. You can look up the source code online, but it’s unlikely to really enlighten you much. (It just looks up a configuration value from the database.)

That being said, we’ve occasionally seen some servers get blocked from Twitter for hitting the rate limit for requests. You may be able to work around this by using an API key for your requests, or by setting up a unique IP for your site and making your requests from there.

allow_call_time_pass_reference is a red herring. Ignore it.


#5

Thanks for the advice, Andrew!

This was recommended to me by a DH tech before Twitter went to OAuth but I don’t really understand what it’s referring to? Do you mean pay to have a static IP?


#6

If the exact same code works on other hosts, and it doesn’t work at all here, then I’d think there should be a no-cost solution to the problem. The IP address solution only helps if it is true that other users on your shared server are hammering their Twitter feeds.


#7

Yea, and I couldn’t believe it was that anyway, unless it got permabanned…

If this was the case, (havana got permabanned from requesting stuff from twitter,) there wouldn’t really be anything I could do, would there?


#8

So basically, I’ve been able to break it down to a basic script that determines that my request is in fact being blocked by Twitter:

Link: http://www.dannyjwood.com/twitterTesting/time.php

<html>
<body>
	<?php
	
	$url = 'http://twitter.com/statuses/user_timeline/96646729.css';
 
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	$content = curl_exec($ch);
	curl_close($ch);
	
	if (preg_match('/<description>/iS',$content))
	{
		echo "Success!";
	}
	else {
		echo 'Twitter blocked us =(';
	}
	
	?>
</body>
</html>

In a tech chat, I was told this…

So, is it true that the only options are VPS or static-IP?

Time to change hosts…


#9

Out of curiosity, I ran this on my home machine (on Charter cable) and also got the Blocked message. Something’s weird.


#10

Hmmm… weird indeed… gah… I just don’t get it. At the end of the day, the script works on other hosts and not dreamhost, even if I’m incapable of breaking it down and figuring out the root problem in the code.


#11

Might help to test against http://twitter.com/statuses/user_timeline/96646729.rss instead of http://twitter.com/statuses/user_timeline/96646729.css

That being said, Twitter definitely has some sort of grudge against havana:

havana:~# telnet twitter.com 80 Trying 128.242.240.20... Connected to twitter.com. Escape character is '^]'. Connection closed by foreign host.

We’ve tried talking to Twitter about this sort of thing, but haven’t gotten much useful advice out of them.


#12

Well, the “rss” part gave me better results, here (roseau) and at home. Success for both.