hackalive Posted January 14, 2012 Share Posted January 14, 2012 Hi Guys, I am working on my own API using OAuth 2, like Facebook. Now I have worked out all the OAuth part of the API exactly like Facebook has thier Graph API. My remaining issue is rendering data from a DB call in JSON. So two part question really: 1. Facebook API uses URLs such as http://api.facebook.com/me/name to get your name. Does it do this by having an index.php and doing an if on the url, for example if me and if name then .... or something else? How should I make this work? 2. How to render the output from MySQLi as JSON. Many many thanks in advance. Cheers Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 14, 2012 Share Posted January 14, 2012 So two part question really: 1. Facebook API uses URLs such as http://api.facebook.com/me/name to get your name. Does it do this by having an index.php and doing an if on the url, for example if me and if name then .... or something else? How should I make this work? How facebook does it is something you'd only know if you worked at facebook. With that said, these types of things are typically done with a script that takes url parameters: http://api.yoursite.com/me.php?name=yourname Then using mod_rewrite or some similar trick, a url like the one you presented is converted on the fly. 2. How to render the output from MySQLi as JSON. json_encode json_decode Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 Any one else have an answer to Q1 or Q2? Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 14, 2012 Share Posted January 14, 2012 Any one else have an answer to Q1 or Q2? I just gave you the answer to both questions. Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 @gizmola: So how would you handle one like https://graph.facebook.com/me/friends/1207059 ? REF: https://developers.facebook.com/docs/reference/api/user/ Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 If someone can provide an answer based on the above (for Q1) that would be much and greatly appreciated. Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 14, 2012 Share Posted January 14, 2012 The principle is the same. In general, the same way as I described previously. Let's revisit that and generalize. Let's say you have a controller (the C in MVC) responsible for routing. Generalized it could be: http://www.yoursite.com/me.php?action=x¶m1=foo¶m2=bar&etc. me.php simply needs to be your router. This assumes that behind the scenes (using session variables) the api already has authenticated you, and knows who you are. me.php needs to take the action parameter, and use that to load the appropriate handler code for that action, and pass any subsequent parameters to it. Mod rewrite allows you to present the url: www.yoursite.com/me/friends Which would be translated internally to: www.yoursite.com/me.php?function=friends The variation you are asking about, would be: www.yoursite.com/me.php?action=friends&friend_id=1207059 Of course the code that is loaded to handle the "friends" action, needs to have logic which does the right thing when a friend_id is passed, vs the case where there is no friend_id. This is a high level description of how front end controllers work. Have a look at zend framework, code igniter, symfony or cakephp to get an idea of how they handle this, and what sort of design patterns and options they provide. Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 @gizmola: Ok, that tells me how it works (sort of) but not how to make it work. Whats the PHP and whats the mod_rewrite to go with it to make the URLS like those of Facebook? Espeically the noted sample I posted. Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 Im not interested in using an MVC, and Facebook does not use a traditional MVC, I am after how I can implement this without an MVC and just Plain Apache and PHP. So if you could tell me how to do that it would be much appreciated. Quote Link to comment Share on other sites More sharing options...
laffin Posted January 14, 2012 Share Posted January 14, 2012 It's using Apache Mod Rewrite. Which takes one uri and formats it into another. A Quick google search for Apache Mod Rewerite tutorial pretty urls should give you a good examples but if you plan on making your own a good knowledge of regex is required Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 laffin, id greatly appreciate your help with this if possible It would be much and greatly appreciated. Quote Link to comment Share on other sites More sharing options...
trq Posted January 14, 2012 Share Posted January 14, 2012 Where exactly are you stuck? A big part of programming is thinking a problem through. Both of your questions have been answered in enough detail that you should be able to start researching. If you can't be bothered, your in the wrong game. If your stuck, tell us specifically where you are stuck. Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 thrope, Thanks for pointing out my lack of information appreciated. I know basically how to get mod_rewrite to work and do like mydomain.com/me/friends which is me.php?action=friends (or similar) But how do I make the mod_rewrite work to do something more like mydomain.com/me/friends/adam which is me.php?action=friends&name=adam It how to do that which I am stuck with. Thanks again thorpe. Quote Link to comment Share on other sites More sharing options...
trq Posted January 14, 2012 Share Posted January 14, 2012 Thanks for pointing out my lack of information Sorry, but that makes little sense. I know basically how to get mod_rewrite to work and do like mydomain.com/me/friends which is me.php?action=friends (or similar) But how do I make the mod_rewrite work to do something more like mydomain.com/me/friends/adam which is me.php?action=friends&name=adam It is done no differently to the one you say you know how to do. Quote Link to comment Share on other sites More sharing options...
hackalive Posted January 14, 2012 Author Share Posted January 14, 2012 I found this http://wettone.com/code/clean-urls I think thats a good resource for anyone else who ever gets stuck with this Thanks thorpe for your help. Quote Link to comment Share on other sites More sharing options...
gizmola Posted January 14, 2012 Share Posted January 14, 2012 Im not interested in using an MVC, and Facebook does not use a traditional MVC, I am after how I can implement this without an MVC and just Plain Apache and PHP. So if you could tell me how to do that it would be much appreciated. You are completely wrong. MVC is a "Design pattern". There are lots of ways to implement that pattern or portions of that pattern. Facebook absolutely does implement the "Controller" part of MVC. It seems we are not communicating effectively, because I went into some detail on how and why a controller is being used there. See http://en.wikipedia.org/wiki/Front_Controller_pattern. Each of the discreet api entry points can implement a front controller pattern to provide different responses based on the parameters passed to it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.