【问题标题】:file_get_contents() with POST method shows as GET?带有 POST 方法的 file_get_contents() 显示为 GET?
【发布时间】:2018-01-22 05:52:33
【问题描述】:

使用 Google App Engine,当使用 POST 方法调用我的 API 时,它显示为 GET - 为什么?

这是我的代码:

function call_api($client_id, $client_secret, $data) {
    $api_url = 'http://myapp.com/api.php';

    $options = array(
        'http' => array(
            'header' => "Authorization: Basic " . Base64_encode("$client_id:$client_secret") . "\r\nContent-type: application/x-www-form-urlencoded\r\n",
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($api_url, false, $context);
    return $result;
}

api.php的第一行是:

echo "<pre>"; print_r($_SERVER); echo "</pre>";

在该输出中,我看到:

[REQUEST_METHOD] => GET

这是如何/为什么会发生的?

值得一提的是,在 GAE 的 SDK 上测试此代码时,该方法显示为 POST。

【问题讨论】:

    标签: php google-app-engine


    【解决方案1】:

    我已经解决了,我需要回答我自己的问题,因为它太棒了!!

    虽然 url 是 http $api_url = 'http://myapp.com/api.php';,但与其他所有内容一样,GAE app.yaml 文件将所有脚本作为 https 提供:

    - url: /(.+\.php)$
      script: \1
      secure: always
    

    这意味着上面调用我的函数的页面是https所以api调用不喜欢请求因为Cross-orgin source sharing

    解决方案是简单地将 $api_url 更改为 https

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2014-01-29
      • 2021-10-20
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多