dongsuoying9059 2018-01-22 05:52
浏览 27

使用POST方法的file_get_contents()显示为GET?

Using Google App Engine, when calling my API using POST method, it shows as GET - why?

This is my code:

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") . "
Content-type: application/x-www-form-urlencoded
",
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($api_url, false, $context);
    return $result;
}

The first line of api.php is:

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

And within that output, I see:

[REQUEST_METHOD] => GET

How/why could this be happening?

It's also worth mentioning that the method shows as POST when testing this code on GAE's SDK.

  • 写回答

1条回答 默认 最新

  • douzhi3454 2018-01-22 06:22
    关注

    I worked it out and I need to answer my own question because it's a doozy!!

    Whilst the url is http $api_url = 'http://myapp.com/api.php';, as per everything else, the GAE app.yaml file serves all scripts as https as per:

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

    This means that the page that calls my function above is https so the api call doesn't like the request because of Cross-orgin source sharing.

    The solution was to simply change the $api_url to be https.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线