Current time: 04-24-2014, 12:44 PM Hello There, Guest! (LoginRegister)

Post Reply 
Issues with porting in Twitter feed?
08-19-2010, 09:35 AM (This post was last modified: 08-19-2010 10:22 AM by dannyjwood.)
Post: #1
Issues with porting in Twitter feed?
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('/<description>/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@<a href="http://www.twitter.com/\2">\2</a>', $v);
        $v = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $v);

        $v = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $v);
        $v = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $v);
        $v = preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $v);

        return trim($v);
    }

?>

<html>
<body>
<?php if (is_array($feed)): ?>
<ul>
    <?php array_splice($feed,5); ?>
    <?php foreach ($feed as $item): ?>
    <li><?php echo linkify_tweet($item['desc']); ?></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
</body>
</html>

UPDATE

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...

danny j wood
site
twitter
Find all posts by this user
Quote this message in a reply
08-19-2010, 10:41 AM
Post: #2
RE: Issues with porting in Twitter feed?
A wordpress theme I use shows my twitter feed. Here's its code, if that helps:
Code:
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&amp;count=<?php echo $count; ?>"></script>
    <?php
    }
}

-Scott
Find all posts by this user
Quote this message in a reply
08-19-2010, 10:47 AM (This post was last modified: 08-19-2010 10:59 AM by dannyjwood.)
Post: #3
RE: Issues with porting in Twitter feed?
(08-19-2010 10:41 AM)sdayman Wrote:  A wordpress theme I use shows my twitter feed. Here's its code, if that helps...

Thanks for reply! Can you post the script too that contains the function "obwp_get_meta()"?
(08-19-2010 10:41 AM)sdayman Wrote:  A wordpress theme I use shows my twitter feed. Here's its code, if that helps...

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.

danny j wood
site
twitter
Find all posts by this user
Quote this message in a reply
08-19-2010, 11:31 AM
Post: #4
RE: Issues with porting in Twitter feed?
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.
Find all posts by this user
Quote this message in a reply
08-19-2010, 12:29 PM
Post: #5
RE: Issues with porting in Twitter feed?
Thanks for the advice, Andrew!

(08-19-2010 11:31 AM)andrewf Wrote:  by setting up a unique IP for your site and making your requests from there.

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?

danny j wood
site
twitter
Find all posts by this user
Quote this message in a reply
08-19-2010, 12:40 PM
Post: #6
RE: Issues with porting in Twitter feed?
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.

-Scott
Find all posts by this user
Quote this message in a reply
08-19-2010, 01:23 PM
Post: #7
RE: Issues with porting in Twitter feed?
(08-19-2010 12:40 PM)sdayman Wrote:  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.

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?

danny j wood
site
twitter
Find all posts by this user
Quote this message in a reply
08-20-2010, 09:52 AM
Post: #8
RE: Issues with porting in Twitter feed?
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

Code:
<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...

Quote:Us and other companies are all affected by this, and it only gets worse as Twitter locks down their servers even more (they've actually started firewalling lately instead of just rejecting requests)

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

Time to change hosts...

danny j wood
site
twitter
Find all posts by this user
Quote this message in a reply
08-20-2010, 11:37 AM
Post: #9
RE: Issues with porting in Twitter feed?
Out of curiosity, I ran this on my home machine (on Charter cable) and also got the Blocked message. Something's weird.

-Scott
Find all posts by this user
Quote this message in a reply
08-20-2010, 12:18 PM
Post: #10
RE: Issues with porting in Twitter feed?
(08-20-2010 11:37 AM)sdayman Wrote:  Out of curiosity, I ran this on my home machine (on Charter cable) and also got the Blocked message. Something's weird.

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.

danny j wood
site
twitter
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: