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

4

1 回答 1

0

好的,我的主机似乎没有接受 get_file_contents,所以我重试了一个 cURL 函数并让它正常工作。下面的代码:

<?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>  
于 2012-12-05T01:33:27.243 回答