How to get the top URL from a Facebook application?

后端 未结 4 1014
青春惊慌失措
青春惊慌失措 2021-01-25 15:02

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

Is there a good solution on that?

相关标签:
4条回答
  • 2021-01-25 15:25

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

    $(document).ready(function()
    {
        var myurl = window.location.pathname;
        $.get("test.php", { url: myurl } );
    });
    
    0 讨论(0)
  • You mean a URL on Facebook's domain?

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

    0 讨论(0)
  • 2021-01-25 15:38

    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.

    0 讨论(0)
  • 2021-01-25 15:47
    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

    0 讨论(0)
提交回复
热议问题