Search code examples
facebookurlfacebook-likehref

Facebook app dynamic like


I am developing a facebook app where users watch videos from a playlist and view images from galleries. The app itself is hosted on my server then set as a canvas page in facebook. What I want is for users to be able to "Like" individual videos and photos. I have currently set it so that the when a photo/video is selected it ammends the href attribute of the "Like" button with the url of the appropriate page appended with a querystring for the image/video id.

e.g. href="http://www.myApp.com/index.php?id=0001"

This works but the problem is when that "Like" event is posted on the users wall the link created goes to hosted page and not to the facebook canvas page (in other words it leaves facebook). What I want is for the "Like" link on the users wall to take them to the canvas page and select the correct page and image/video within the page.

I could set the href to go to the canvas page but that would the app at the homepage and I want it to go straight to the liked image/video. Is there perhaps a way I can ammend the url so that when it is clicked it tells facebook exactly what url to set for the canvas page?


Solution

  • Incase anyone else is having difficulty with this I have found a solution.

    Set the href attribute of your "Like" button to the facebook page of your app i.e. "apps.facebook.com/myApp/"

    You can then add your own querystrings on the end to identify where in the app you want to go. Pick names which are likely to be unique to your app so it doesn't accidently clash with anything Facebook might be processing at the same time. i.e.

    "apps.facebook.com/myApp/?myAppPage=photos&myAppId=003"

    Now in your landing page code put something like this at the top:

    if (isset($_REQUEST['myAppPage'])) {
    $myAppPage = $_REQUEST['myAppPage'];
    if (isset($_REQUEST['myAppId'])) {
        $myAppId = $_REQUEST['myAppId'];
        $qsName;
        switch ($myAppPage) {
            case "photos":
                $qsName = 'gal';
                break;
            Case "videos":
                $qsName = 'vid';
                break;
        }
        header("Location: $myAppPage.php?$qsName=$myAppId");
    }
    }
    

    This should redirect your app to the page you want without leaving Facebook. Then you just need to program your page to load the correct content based on the new querystrings passed in the header.

    Hope this helps someone.