如何在PHP中为Google App Engine执行URLFetch时禁用重定向? [英] How to disable redirects when doing a URLFetch in PHP for Google App Engine?

查看:99
本文介绍了如何在PHP中为Google App Engine执行URLFetch时禁用重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在GAE网站上设置两个PHP:一个充当Web服务层,另一个充当前端。到目前为止,事情很好。我试图保证这两层之间的通信,以便我不仅可以调用我的Web服务。



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



为此,文章说我应该考虑告诉UrlFetch服务在调用时不要遵循重定向。然后马上就不会提供任何有关您实际如何 DO 的文档。



我的请求如下所示:

  $ data = ['data'=> 'this','data2'=> '那']; 
$ data = http_build_query($ data);
$ context = [
'http'=> [
'method'=> 'get',
'header'=> custom-header:custom-value \r\\\
。 custom-header-two:custome-value-2 \r\\\

'content'=> $ data,
]
];
$ context = stream_context_create($ context);
$ result = file_get_contents('urlgoeshere',false,$ context);

想法&帮助表示赞赏!

解决方案

两种方法,将follow_location设置为false并将/或max_redirects设置为0。
$ b

  $ context = [
'http'=> [
'method'=> 'get',
'header'=> custom-header:custom-value \r\\\
。 custom-header-two:custome-value-2 \r\\\

'content'=> $ data,
'follow_location'=>假,
]
];

  $ context = [
'http'=> [
'method'=> 'get',
'header'=> custom-header:custom-value \r\\\
。 custom-header-two:custome-value-2 \r\\\

'content'=> $ data,
'max_redirects'=> 0,
]
];


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!

解决方案

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,
    ]
];

这篇关于如何在PHP中为Google App Engine执行URLFetch时禁用重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆