Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/#findComment-1307502
Share on other sites

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&param1=foo&param2=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.

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/#findComment-1307515
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/#findComment-1307540
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/#findComment-1307541
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/#findComment-1307545
Share on other sites

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. 

 

 

Link to comment
https://forums.phpfreaks.com/topic/254996-url-get-data/#findComment-1307643
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.