I'm developing an application that which will work as a standalone 开发者_运维百科website and will also get rendered inside of a Facebook frame, i.e. a Facebook app.
Only a small section of the website will be available in the Facebook frame so I need a way of switching layouts depending on whether it's rendered in the frame.
I think the easiest way would be to use a distinct url for accessing the site through Facebook. For example if the external website is at www.xyz.com, configure fb.xyz.com on your webserver (and dns) to point to the same place and then use fb.xyz.com in your app settings. Your scripts can then easily check the hostname and determine if it's an app request or not.
A similar way would be to make a subdirectory that is just a symbolic link to the main directory and call it something like "faceapp". In other words have www.xyz.com and www.xyz.com/faceapp pointing the same place. Then of course use www.xyz.com/faceapp/ in your app settings. In your PHP you can do something like this:
$faceapp=(strpos($_SERVER['SCRIPT_NAME'], '/faceapp/')===false?0:1);
and use the $faceapp variable to determine how to render parts of the page.
The iframe is populated via a POST to your server that contains a parameter named signed-request
.
If this parameter exists in the request to your server, you know that the application lives inside facebook's iframe.
In javascript SDK there are methods under FB.Canvas namespace which are designed to work on canvas, you can check whether these methods are available i.e. after FB.init you can call if (FB.Canvas) most likely it will return undefined and if not you can check other FB.Canvas methods which will indicate if you are on canvas or not. Example:
https://developers.facebook.com/docs/reference/javascript/FB.Canvas.getPageInfo/
hope this helps
精彩评论