How to disable redirects when doing a URLFetch in

2019-03-05 08:22发布

I'm trying to set up two PHP on GAE sites: One to act as a web services layer, and another to act as the front-end. So far, things are great. I'm trying to secure communications between these two layers, so that not only I can call my web services.

I found this article that talks about making requests, and by doing so, it can set the X-Appengine-Inbound-Appid header, which is exactly what I want. https://developers.google.com/appengine/docs/php/urlfetch/#PHP_Making_requests

To do this, the article says I should "consider telling the UrlFetch service to not follow redirects when invoking it."; then promptly does not provide any documentation on how you actually DO that.

My request looks like this:

$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);

Thoughts & help are appreciated!

1条回答
混吃等死
2楼-- · 2019-03-05 09:08

Two ways, either set follow_location to false and/or max_redirects to 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,
    ]
];
查看更多
登录 后发表回答