0

I have a twitter feed in the sidebar of my magento built site, the following PHP calls to the twitter API and posts the feed as a list:

 <?php
        $feedUrl = 'https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=ecpublish&count=4';
        $feedXml = file_get_contents($feedUrl);
        try {
            $feedXml = new SimpleXMLElement($feedXml);
        } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }

        ?>
        <ul>
                <?php foreach($feedXml[0]->channel->item as $elName => $child): ?>          
                <li>
                    <a href="<?php echo (string)$child->link; ?>" title="" target="_blank"><?php echo substr((string)$child->title, 11); ?></a>
                </li>
            <?php endforeach ?>                 
        </ul>  

Unfortunately I'm getting an error in my system.log that reads as follows:

2012-12-04T01:02:33+00:00 ERR (3): Warning: file_get_contents(https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=ecpublish&count=4) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /clientdata/apache-www/e/c/ecpublishing.com.au/www/app/design/frontend/default/ecp/template/page/sidebar/twitter.phtml on line 9

I have tried fixes such as urlencode(), cURL, checked PHP version and fopen status, nothing is helping me out. I have a feeling it may be due to the twitter rate limiting: https://dev.twitter.com/docs/rate-limiting but can't work out why this would be.

My site is: http://ecpublishing.com.au

j0k
  • 22,600
  • 28
  • 79
  • 90
joshzee
  • 187
  • 1
  • 12

1 Answers1

0

ok it seems the get_file_contents wasn't being accepted by my host, so I retried a cURL function and got it working perfectly. Code below:

<?php            
        $url = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=ecpublish&count=4';
        function get_data($url) {
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
            }
            $returned_content = get_data('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=ecpublish&count=4');
            try {
            $returned_content = new SimpleXMLElement($returned_content);
                } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
            }

        ?>
        <ul>
                <?php foreach($returned_content[0]->channel->item as $elName => $child): ?>          
                <li>
                    <a href="<?php echo (string)$child->link; ?>" title="" target="_blank"><?php echo substr((string)$child->title, 11); ?></a>
                </li>
            <?php endforeach ?>                 
        </ul>  
joshzee
  • 187
  • 1
  • 12