Stuck with empty $postData = file_get_contents('php://input');

682 views
Skip to first unread message

Igor Donin

unread,
Aug 13, 2012, 9:01:44 AM8/13/12
to google-iden...@googlegroups.com
Hi, all!

It's been a whole day now and I'm stuck with getting empty results from $postData = file_get_contents('php://input');

My environment is PHP 5.3.3, Apache 2.2.6, localhost.

If anyone can point me in the right direction, it'll be much appreciated.

--


Google API Console Config

Google Identity Toolkit
Account Chooser login widget
This tool helps you to configure your login widget and generate JavaScript code.
Callback URL:
OpenID Realm:
 <To be configured>
User status URL:
Login URL:
Signup URL:
Home URL:
Logout URL:
Identity providers:
 Gmail,Yahoo
Company name:
 Valentinos Pizzaria
Language:
 en


In my index.php

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/googleapis/0.0.4/googleapis.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/jsapi"></script>
<script type="text/javascript">
  google.load("identitytoolkit", "1", {packages: ["ac"], language:"en"});
</script>
<script type="text/javascript">
  $(function() {
    window.google.identitytoolkit.setConfig({
        developerKey: "my_developer_key", # Should I post it?
        companyName: "Valentinos Pizzaria",
        callbackUrl: "http://localhost/valentinos/callback.php",
        realm: "",
        userStatusUrl: "http://localhost/valentinos/user.php",
        loginUrl: "http://localhost/valentinos/login.php",
        logoutUrl: "http://localhost/valentinos/logout.php",
        idps: ["Gmail", "Yahoo"],
        tryFederatedFirst: true,
        useCachedUserStatus: false,
        useContextParam: true
    });
    $("#navbar").accountChooser();
  });
</script>


my callback php

#header('Content-type: application/json');

  $url = EasyRpService::getCurrentUrl();
  #$postData = @file_get_contents('php://input');
  $postData = file_get_contents('php://input');
   
  
  var_dump($postData);
  echo "<br><br>";
  
  $result = EasyRpService::verify($url, $postData);
  // Turn on for debugging.
  var_dump($result);
  echo "<br><br>";
  die();


class EasyRpService {
  // Replace $YOUR_DEVELOPER_KEY

# One question: should I have replaced www.googleapis.com with http://localhost/valentinos ??


  public static function getCurrentUrl() {
    $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
    $url .= $_SERVER['SERVER_NAME'];
    if ($_SERVER['SERVER_PORT'] != '80') {
      $url .= ':'. $_SERVER['SERVER_PORT'];
    }
    $url .= $_SERVER['REQUEST_URI'];
    return $url;
  }
  
  private static function post($postData) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => EasyRpService::$SERVER_URL,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
        CURLOPT_POSTFIELDS => json_encode($postData)));
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($http_code == '200' && !empty($response)) {
      return json_decode($response, true);
    }
    return NULL;
  }

  public static function verify($continueUri, $response) {
$request = array();
    $request['method'] = 'identitytoolkit.relyingparty.verifyAssertion';
    $request['apiVersion'] = 'v1';
    $request['params'] = array();
    $request['params']['requestUri'] = $continueUri;
    $request['params']['postBody'] = $response;

    $result = EasyRpService::post($request);
    if (!empty($result['result'])) {
      return $result['result'];
    }
    return NULL;
  }
  
} # End Class EasyRpService
Reply all
Reply to author
Forward
0 new messages