How to get the top URL from a Facebook application

2019-03-02 21:47发布

问题:

How can I get the top URL (Facebook's address) in my iframe application?

Is there a good solution on that?

回答1:

You mean a URL on Facebook's domain?

That's not going to be possible due to the Same Origin Policy.



回答2:

You can probably figure this out from the "signed_request" POST parameter that Facebook sends to apps loaded within pages.

In PHP that's:

$_POST["signed_request"]
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$page_id = $data["page"]["id"];

If you want to prevent technical users from being able to spoof the parent page's identity, you can validate the $encoded_sig; this is probably overkill, however.

The $page_id is just a number, so you'll need to use other Facebook APIs (or make a call to "http://www.facebook.com/pages/k/$page_id" and see where you're redirected to) to get the full URL.



回答3:

https://www.facebook.com/pages/YOUR_PAGE_NAME/YOUR_PAGE_ID?sk=app_APP_ID&app_data=My%20custom%20data

you can pass any string data by app_data parameter



回答4:

If you can wait for first load, you can use jQuery:

$(document).ready(function()
{
    var myurl = window.location.pathname;
    $.get("test.php", { url: myurl } );
});