file_get_contents('php://input') 一直返回空


file_get_contents('php://input') keeps returning empty

我正在尝试在我的网站中设置 gitkit,但无法通过这一行代码。无论我做什么,file_get_contents总是空着返回。

我已经设置了我的php.ini : always_populate_raw_post_data = On

我的环境是PHP 5.3.3Apache 2.2.6,本地主机。

下面是一些代码。

在我的索引.php中,我调用谷歌 API 并尝试使用 gmail 帐户登录,换句话说,联合登录。

(这是来自谷歌API控制台)

<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: "HERE_GOES_MY_DEVELOPER_KEY",
        companyName: "Valentinos Pizzaria",
        callbackUrl: "http://localhost/valentinos/callback.php",
        realm: "",
        userStatusUrl: "http://localhost/valentinos/userstatus.php",
        loginUrl: "http://localhost/valentinos/login.php",
        signupUrl: "http://localhost/valentinos/register.php",
        homeUrl: "http://localhost/valentinos/index.php",
        logoutUrl: "http://localhost/valentinos/logout.php",
        idps: ["Gmail", "Yahoo"],
        tryFederatedFirst: true,
        useCachedUserStatus: false,
        useContextParam: true
    });
    $("#navbar").accountChooser();
  });
</script>

我收到 IDP 响应,登录并被要求提供权限。回到我的回调页面后,我使用了Google提供的代码示例(如下所示),这行代码似乎没有正确返回。

我到底在

做什么傻事吗?

任何帮助将不胜感激。

到目前为止.php这是整个回调(目前没有任何 HTML):

  session_start();
  $url = EasyRpService::getCurrentUrl();
  #$postData = @file_get_contents('php://input');
  $postData = file_get_contents('php://input');
  $result = EasyRpService::verify($url, $postData);
  // Turn on for debugging.
  // var_dump($result);
class EasyRpService {
  // Replace $YOUR_DEVELOPER_KEY
  private static $SERVER_URL = 'https://www.googleapis.com/rpc?key=HERE_GOES_MY_DEVELOPER_KEY';
  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

在有人问之前,我确实用我的开发人员密钥替换了HERE_GOES_MY_DEVELOPER_KEY......

再次,任何帮助将不胜感激。嘭。

你试过用$_POST吗? php://input不适用于 enctype="multipart/form-data"。在这种情况下,您可能正在收到响应,因为多部分/表单数据 $_POST[0] 应该可以工作。

HTTP POST数据通常填充在$_POST中,php://input 通常包含PUT数据。