Search code examples
twitterfeedtimeline

server not retrieving GET statuses/user_timeline information from twitter


I'm trying to create a simple widget to retrieve a users feeds, I have fully set my application up to work with the @anywhere an OAuth functionality of twitter's API.

The problem I'm having is when I try to retrieve a users tweets and writing them out again to a text file. My code for doing so is as follows

    <?php

    $cache = dirname(__FILE__) . '/../cache/twitter-json.txt';
    $data = file_get_contents('http://api.twitter.com/1/statuses/user_timeline/screen_name.json?count=3&include_rts=true&include_entities=true'); 

    $cachefile = fopen($cache, 'wb');
        fwrite($cachefile,utf8_encode($data));
        fclose($cachefile);
?>

okay now this code works great when I run the application locally but form some reason it does not work when I deploy it to the server. It creates the cache file on the server and any test data I added to check if the file writing procedure worked.

I have not set up the server on my own it is run by an external company I'm just using ftp commands to deploy the web application

Any help and ideas would be appreciated


Solution

  • Check with your host - they have almost certainly disallowed file_get_contents for security reasons.

    Your options are

    1. Ask them to enable it by editing php.ini to allow_url_fopen
    2. Use cURL to make the call

    Here's a basic example using curl in php

    $url = "http://api.twitter.com/1/statuses/user_timeline/screen_name.json?count=3&include_rts=true&include_entities=true"
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handle,CURLOPT_URL,$url);
    $data = curl_exec($curl_handle);
    curl_close($curl_handle);