Grow your CSS skills. Land your dream job.

API help

  • # June 24, 2013 at 5:06 pm

    I’m having trouble wrapping my ahead around grabbing data from this API. What I’m basically trying to do is grab the latest movie I’ve watched and output it. Are there any resources to understand how to go about this or any guidance?

    Also, is PHP 5.5 safe to use? My host said that they’re using 5.3 since 5.4 isn’t that stable.

    # June 24, 2013 at 5:30 pm

    You might wanna try this http://trakt.tv/api-docs/activity-user, I suspect it’s chronological sorted (since it returns the most recent activities) so you can just grab the first/last activity.

    # June 24, 2013 at 6:01 pm

    >What I’m basically trying to do is grab the latest movie I’ve watched and output it.

    It looks like you’ll have to get the complete list and pull the last one listed yourself.

    >is PHP 5.5 _[sic;?]_ safe to use? My host said that they’re using 5.3 since 5.4 isn’t that stable.

    Your host just doesn’t want to upgrade. The only semi-legitimate reason is that there are some backwards-incompatible changes (all things you _shouldn’t be doing anyway…!_) and they don’t want any of their clients to think that they “broke their website.”

    However, there is _no_ reason not to let people “opt-in” to 5.4.

    `5.4` is the current stable release. It is perfectly safe to use (safer than 5.3, in fact). It is also **awesome**; I highly recommend you [find a host that allows you to] upgrade.

    # June 24, 2013 at 6:31 pm

    @CrocoDillon Ah, thanks for looking into that. I would have wasted my time.

    @traq

    > It looks like you’ll have to get the complete list and pull the last one listed yourself.

    Yeah, I figured that. Just don’t know how to connect to the API. I was looking for a tutorial that shows steps on how to do it or are all API’s different in the way they go about it?

    > However, there is no reason not to let people “opt-in” to 5.4.

    They do allow us to use whatever version we want by specifying it somewhere in one of our files. I forgot what I have to add to starting using 5.4 but it is possible. What about 5.5 that was just released a few days ago?

    # June 24, 2013 at 6:44 pm

    You mean how you connect to the API in PHP or something, like cURL?

    # June 24, 2013 at 6:52 pm

    Preferably PHP, yes @CrocoDillon. Although it states something about JSON.

    Note: I just tried upgrading my site to 5.5 and it just keeps refreshing. Weird.

    @traq I think I’m going to stick to 5.3. I think you’ve mentioned that Kirby has some issues with 5.4 at the moment.

    # June 24, 2013 at 7:07 pm

    Yes, you can get the JSON using cURL, and then decode it to a PHP object.

    $ch = curl_init(“http://api.trakt.tv/user/library/movies/watched.json/apikey/username”);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    var_dump(json_decode(curl_exec($ch)));
    curl_close($ch);

    I’m pretty new to this and while it works, you probably need to set a timeout and some fallback in case connecting fails.

    # June 24, 2013 at 7:11 pm

    @CrocoDillon Thank you very much. Where did you learn to do that?! That’s why I’m trying to figure out so I don’t have to ask these questions in the future.

    # June 24, 2013 at 7:17 pm

    You’re welcome. I’m new to cURL but I did already know about it and that’s what you need to connect to APIs like that. So it was easy to look it up in PHP.net manual and do some Googling (the part about ‘RETURNTRANSFER’, because without it there was a trailing 1 after the JSON part, which was weird to me).

    I just Googled ‘parse json in php’ to get to the `json_decode` function. I learned new stuff here too, which is good :)

    # June 24, 2013 at 7:25 pm

    @CrocoDillon What do you mean there was a trailing 1?

    # June 24, 2013 at 7:26 pm

    >What about 5.5 that was just released a few days ago?

    Ah – I wasn’t sure if that was a typo or not. No, I wouldn’t recommend 5.5 yet. It’s for the adventurous; it’s not done yet. (Though it looks like it’ll be pretty cool once its ready.)

    >I think you’ve mentioned that Kirby has some issues with 5.4 at the moment.

    Yeah, I forgot about that. Actually looks like it was fixed (not sure when; I can’t find the version log. But I just tried 1.1.2-13 and t seems to work :D ).

    # June 24, 2013 at 7:32 pm

    @traq Awesome. I’ll update to 5.4. Thanks.

    # June 24, 2013 at 8:02 pm

    At first I tried

    $ch = curl_init(“http://api.trakt.tv/user/library/movies/watched.json/apikey/username”);
    echo curl_exec($ch);
    curl_close($ch);

    and I got something like `{…some json…}1`… so I figured I couldn’t just `json_decode` that. Turned out curl_exec already prints the content if the RETURNTRANSFER option is not set, and the echo statement printed the ’1′… I think. Not something to hurt your head over.

    # June 24, 2013 at 8:13 pm

    @CrocoDillon Gotcha.

    # June 24, 2013 at 9:35 pm

    @chrisburton all of your questions are getting such thoughtful responses, full of information. Learning a lot from them, keep ‘em coming!

Viewing 15 posts - 1 through 15 (of 31 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".