user2081588
user2081588

Reputation: 31

Facebook PHP app on Heroku not receiving signed_request

I have added a PHP/JavaScript app as a Facebook page tab, and I'm hosting it on Heroku. However it's not receiving the signed_request. It comes through just fine when I host the tab on my own server, just not on Heroku.

Here is the code:

require_once('sdk/src/facebook.php');

$facebook = new Facebook(array(
  'appId'  => AppInfo::appID(),
  'secret' => AppInfo::appSecret(),
  'sharedSession' => true,
  'trustForwarded' => true,
  'cookie' => true
));

$signed_request = $facebook->getSignedRequest();
$liked = $signed_request['page']['liked'];

if( $liked ) {
    echo('fan');
}
else {
    echo('not a fan ') ;
}

What could the issue be?

Upvotes: 2

Views: 615

Answers (1)

Rustedwolf
Rustedwolf

Reputation: 31

I had the same problem. If you're using heroku's base php template get rid of this code, that enforces the https on production:

// Enforce https on production
if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
    header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    exit();
}

When the user doesn't uses the secure connection, this line redirects to another page losing the POST requests.

You can try and send the signed_request with with GET method if you want to keep it. It may look like this:

// Enforce https on production
    if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
        $signed_request = "";
        if (isset($_REQUEST['signed_request'])){
            $signed_request = "&signed_request=" . $_REQUEST['signed_request'];
        }
        header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "?" . $signed_request );
        exit();
    }

That's my solution.

Upvotes: 3

Related Questions