在 PHP 中为 Google App Engine 执行 URLFetch 时如何禁用重定向?

2024-01-06

我正在尝试在 GAE 站点上设置两个 PHP:一个充当 Web 服务层,另一个充当前端。到目前为止,一切都很棒。我正在尝试保护这两层之间的通信,以便不仅我可以调用我的 Web 服务。

我发现这篇文章讨论了发出请求,通过这样做,它可以设置X-Appengine-入站-Appid标题,这正是我想要的。https://developers.google.com/appengine/docs/php/urlfetch/#PHP_Making_requests https://developers.google.com/appengine/docs/php/urlfetch/#PHP_Making_requests

为此,文章说我应该“考虑告诉 UrlFetch 服务在调用它时不要遵循重定向”;然后立即不提供任何有关您实际上如何的文件DO that.

我的请求如下所示:

$data = ['data' => 'this', 'data2' => 'that'];
$data = http_build_query($data);
$context = [
    'http' => [
    'method' => 'get',
    'header' => "custom-header: custom-value\r\n" . "custom-header-two: custome-value-2\r\n",
    'content' => $data,
    ]
];
$context = stream_context_create($context);
$result = file_get_contents('urlgoeshere', false, $context);

感谢您的想法和帮助!


有两种方法,将 follow_location 设置为 false 和/或 max_redirects 设置为 0。

$context = [
    'http' => [
    'method' => 'get',
    'header' => "custom-header: custom-value\r\n" . "custom-header-two: custome-value-2\r\n",
    'content' => $data,
    'follow_location' => false,
    ]
];

or

$context = [
    'http' => [
    'method' => 'get',
    'header' => "custom-header: custom-value\r\n" . "custom-header-two: custome-value-2\r\n",
    'content' => $data,
    'max_redirects' => 0,
    ]
];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 PHP 中为 Google App Engine 执行 URLFetch 时如何禁用重定向? 的相关文章

随机推荐