2

I use the following code to make a request to display a list of latest tweets on a website for a client:

if (file_exists( 'twitter.json' ))
{
    $file = file_get_contents( 'twitter.json');
    $data = json_decode($file);
    if ($data->timestamp > (time() - 60 * 60) ) // if timestamp is NOT older than an hour
    {
        $twitter_result = $data->twitter_result;
    }
    else
    {
        $twitter_result = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?q=@Twitter&rpp=1&screen_name=Twitter&count=1');
        if( $twitter_result === false) /* something went wrong with API request */
        {
            $twitter_result = $data->twitter_result;
        }
        else
        {
            $json = array ('twitter_result' => $twitter_result, 'timestamp' => time());
            file_put_contents( 'twitter.json', json_encode($json) );
        }
    }

    header("content-type:application/json");
    if($_GET['callback'])
    {
        echo $_GET['callback'] . '(' . $twitter_result . ')';
    }
    else
    {
        echo $twitter_result;
    }

    exit;
}
else
{
    echo 'twitter.json does not exist!';

    exit;
}

However due to the 1.0 API being depreciated it will no longer work! I have tried getting it to work with the 1.1 API but I don't understand how to implement the authentication like it says in the docs. Can anyone point me in the right direction? I'm looking to make the least amount of changes to the above code if possible. Thanks.

I've tried for example:

https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2&oauth_token=123&oauth_token_secret=123

Where 123 are the token and secret but this also doesn't work?

sKhan
  • 9,694
  • 16
  • 55
  • 53
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • 2
    This question solves my problem: http://stackoverflow.com/questions/12916539/simplest-php-example-for-retrieving-user-timeline-with-twitter-api-version-1-1 – Cameron Sep 11 '13 at 21:34
  • Have a look at http://aamirafridi.com/twitter/twitter-api-1-1-using-php-and-javascript – Aamir Afridi Mar 07 '14 at 14:24

2 Answers2

3

Check this library out STTwitter

You can use method

- (void)getUserTimelineWithScreenName:(NSString *)screenName
                     successBlock:(void(^)(NSArray *statuses))successBlock
                       errorBlock:(void(^)(NSError *error))errorBlock;

it works with the new version of the API (1.1)

sKhan
  • 9,694
  • 16
  • 55
  • 53
2

All you need to change is the URL from:

http://api.twitter.com/1/statuses/user_timeline.json?q=@Twitter&rpp=1&screen_name=Twitter&count=1

To

https://api.twitter.com/1.1/statuses/user_timeline.json?q=@Twitter&rpp=1&screen_name=Twitter&count=1

The authentication has been added to 1.1,you will need to register an application in order for it to work.

sKhan
  • 9,694
  • 16
  • 55
  • 53
meda
  • 45,103
  • 14
  • 92
  • 122
  • Yeah changed the url to be 1.1 and created an application. But what do I need to add to my code to make it work? I've glanced over the OAuth parts of the documentation, but not sure how to do the type of authentication my code would require. Any examples? – Cameron Sep 09 '13 at 09:41
  • See edited OP for example of what I have tried regarding the OAuth. – Cameron Sep 09 '13 at 14:36