Jump to content

reading JSONP responses


skaterdav85
 Share

Recommended Posts

Is there a way to format a JSONP response so i can easily read it in my browser and understand the hierarchy of the properties for when I parse through it in my callback function? For example, I have the URL from my twitter account, but it is awful to read (I'm using Chrome). http://twitter.com/status/user_timeline/dtang85.json?count=5Any suggestions?

Link to comment
Share on other sites

hey, not to hijack your thread, but how are you implementing twitter? I just finally figured out the whole OAuth process using cURL in PHP and am now looking to get some simple information from a given account. Would you mind discussing your strategies/methods of implementation? i.e. are you using a library, coding on your own, etc? I'm getting stumped on their post tweet example from their OAuth guide, but all I really need is some basic read information from a users account.As far your question, it looks like the response comes back as an object inside a single element array. So I imagine, depending on what you're looking for, to access a specific member, you might do something like this?

var userInfo = response; var profileImage = userInfo[0]['profile_image_url'];  //regex, string replace or something on profileImage to remove the escaping \'sdocument.getElementById('user_profile').src = profileImage;  //set the image URL to an image somewhere on the page

edit: nm, I just realized you were looking for a way to format the response from a readiblity standpoint for yourself.

Edited by thescientist
Link to comment
Share on other sites

hey, not to hijack your thread, but how are you implementing twitter? I just finally figured out the whole OAuth process using cURL in PHP and am now looking to get some simple information from a given account. Would you mind discussing your strategies/methods of implementation? i.e. are you using a library, coding on your own, etc? I'm getting stumped on their post tweet example from their OAuth guide, but all I really need is some basic read information from a users account.As far your question, it looks like the response comes back as an object inside a single element array. So I imagine, depending on what you're looking for, to access a specific member, you might do something like this?
var userInfo = response; var profileImage = userInfo[0]['profile_image_url'];  //regex, string replace or something on profileImage to remove the escaping \'sdocument.getElementById('user_profile').src = profileImage;  //set the image URL to an image somewhere on the page

edit: nm, I just realized you were looking for a way to format the response from a readiblity standpoint for yourself.

I haven't actually used Twitter's oAuth library. In the past I have just read out non-private data such as tweets. To do this, I created a class that tries to use file_get_contents() to read in the xml/json of the remote file. If file_get_contents() is disabled, then I try and use cURL. I think there are some prebuilt classes and libraries you can use to help you with oAuth. Have you tried those? or are you doing it from scratch?
Is something like this what you're looking for?
That is exactly what I am looking for! Thanks!
Link to comment
Share on other sites

I figured out how to do OAuth from scratch, but now I'm just working on doing normal stuff like reading a users followers list and tweets, and stuff like that. I was trying a similar method that I was using for OAuth, but from what I've seen, most of this information is public anyway. I was wondering if the implementation for non-Oauth was different (and hopefully a bit easier.) I'll look into file_get_contents too, and see what that get's me.edit: although after looking at your example

http://twitter.com/status/user_timeline/dtang85.json?count=5

I think I see something that I might have overlooked. Are you doing anything special for these calls with cURL, like signing them, using headers, or anything like that?

Link to comment
Share on other sites

I figured out how to do OAuth from scratch, but now I'm just working on doing normal stuff like reading a users followers list and tweets, and stuff like that. I was trying a similar method that I was using for OAuth, but from what I've seen, most of this information is public anyway. I was wondering if the implementation for non-Oauth was different (and hopefully a bit easier.) I'll look into file_get_contents too, and see what that get's me.
Reading public data from Twitter is much easier than using oAuth. oAuth seemed pretty complicated and I never had the need to use it. file_get_contents is a simple way to just retrieve the contents of a remote file. However, it is not always enabled. There is some directive in the ini file that allows that function to work.
edit: although after looking at your example
http://twitter.com/status/user_timeline/dtang85.json?count=5

I think I see something that I might have overlooked. Are you doing anything special for these calls with cURL, like signing them, using headers, or anything like that?

Nothing special. I am just initializing the cURL session, setting 1 option, executing the session, saving the returned contents to an instance variable, and then closing the session. Here is a class I built when I was first learning cURL:
class Curl {		protected $_url;	protected $_returnedContents;	public function __construct($url){		$this->_url = $url;		$this->getContents();	}		protected function getContents(){		$session = curl_init($this->_url);		curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);		$this->_returnedContents = curl_exec($session);		curl_close($session);	}		public function __toString(){		return $this->_returnedContents;		}	}

You might want to supress the headers by setting the following option:

curl_setopt($session, CURLOPT_HEADER, false);

But for me, it has worked fine without doing that.

Link to comment
Share on other sites

oh man, if I went through the oAuth process for nothing.... :) ah, but I for YouTube I know I was going to need to use headers in my cURL requests, so I was going to have to be forced to learn the HEADER process for cURL anyway.well thanks for all your insight and conversation, I'm looking forward to giving this a shot tonight. After doing some basic Facebook stuff, I didn't think anything else could have been more complicated. apparently I just had to leave that up to me! :)

Edited by thescientist
Link to comment
Share on other sites

your welcome. I haven'y done much web service stuff where I have had to write/update data or access private data, but I'm sure that gets a lot more challenging and complicated. There seems to be too much information and documentation on using oAuth to the point that it is confusing on where to start. If i had to, I would probably look into using one of the existing libraries people have written to handle oAuth. =)

Link to comment
Share on other sites

your welcome. I haven'y done much web service stuff where I have had to write/update data or access private data, but I'm sure that gets a lot more challenging and complicated. There seems to be too much information and documentation on using oAuth to the point that it is confusing on where to start. If i had to, I would probably look into using one of the existing libraries people have written to handle oAuth. =)
:) :) :((you can guess what that means! Thanks big dave! Well, at least I know how to successfully carry out OAuth on my own now, :))
Link to comment
Share on other sites

well, almost. That example works fine for that method of the API because that applies to public users, and doesn't require authentication. Which works well for getting the users profile image and tweets, but I also wanted to get followers/friends. Those kind of calls require authentication, so hopefully I should be all set with just working with what I've done. I'll only need it for one call, so it shouldn't be that bad, I think I got the hardest part done anyway. Thanks for getting me through 2/3'rds of what I was trying to do. Every little bit helps! :)

Link to comment
Share on other sites

awesome. glad i helped. I'm curious. How did you go about figuring out all that oAuth stuff? Any particular resources you found most helpful? I've Googled it in the past and I found long documentation with long code samples so I didn't bother trying to figure it out. How long do you think you have spent on figuring out oAuth?

Link to comment
Share on other sites

awesome. glad i helped. I'm curious. How did you go about figuring out all that oAuth stuff? Any particular resources you found most helpful? I've Googled it in the past and I found long documentation with long code samples so I didn't bother trying to figure it out. How long do you think you have spent on figuring out oAuth?
It took me about a week or so spending a couple hours a day after work here and there slowly but surely plowing through it. The tutorial I followed for OAuth came right from the Twitter API documentation.http://dev.twitter.com/pages/authThe hardest part by far was paying attention to the little details, and learning to use cURL with headers and a post body. The process in general was an easy enough concept to grasp, it's just actually following through on each of the steps. Not sure if I was doing it right made it that much harder to make sure I was signing it correctly, plus a lot of the examples online all used libraries, so I had a harder time making out the inner workings of the process. Unfortunately, I found their documentation a little lacking (typical I've found of API's in general), especially for the parts about signing the signature. Doing a little googling on that got me through it (and the php manual) so about half of it was brute force determination and a lucky google result from stackoverflow, or somebody else doing something similar. I'll tell you, the first time one of those responses came back....phew! Too bad it was past 11pm or I might have had a beer.
Link to comment
Share on other sites

that's a great tool. thanks!@thescientist - haha dang. well if I ever need to use oAuth, you'll be the first person I ask for help =)
sweet, well apparently getting a user's friends doesn't require authentication (unless it's a protected account), I was just looking on the wrong page of the API documentation. :)
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...