WP_Http 重定向后获取最终目的地 (WordPress)
我正在通过 WordPress 向 API 发出一些请求,如果在 API 设置中打开了 SSL 连接,则该 API 会使用 SSL 连接。我想确定 SSL 是否打开或关闭,而不必询问用户其帐户是否打开 SSL,并且 API 在重定向方面做得很好,这意味着
- 如果我访问 http://api/endpoint 并且 SSL 已打开,我被重定向到 https://api/endpoint< /a>
- 如果我访问 https://api/endpoint 并且 SSL 已关闭,我会被重定向到 http://api/endpoint
现在我想做的是查看是否发生了重定向,并将其记录到我的选项中,以便将其他请求触发到正确的 URL没有任何重定向。
所以我的问题是:当请求被重定向时,有没有办法在触发 WP_Http->request()
后确定最终目的地?
我在响应数组中看不到任何相关信息,我只能看到最终响应,但我不知道来自哪个 URL。我能做的就是将 redirection
参数设置为 0 并捕获允许的最大重定向错误,但这并不是万无一失的,因为我仍然不知道重定向是从 http 发生到 https 还是只是发生http 下的另一个页面。
我希望这一切都有意义,如果您有任何想法,请告诉我。
谢谢!
〜K
I'm doing some requests to an API via WordPress, and the API uses SSL connections if they're turned on in the API settings. I'd like to determine whether SSL is turned on or off without having to ask the user if SSL is turned on on their account, and the API does a good job at redirecting, meaning
- If I access http://api/endpoint and SSL is turned on, I'm redirected to https://api/endpoint
- If I access https://api/endpoint and SSL is turned off, I'm redirected to http://api/endpoint
Now what I'd like to do is see whether a redirect happened or not and record that to my options so that the other requests are fired to the correct URL without any redirections.
So my question is: is there a way to determine the final destination after firing a WP_Http->request()
when the request is being redirected?
I can't see any info about that in the response arrays, I only get to see the final response but I have no idea what URL that came from. What I can do is set the redirection
parameter to 0 and catch the max redirects allowed error, but that's not bullet-proof, since I still don't know whether the redirect happened from http to https or simply another page under http.
I hope this all makes sense, let me know if you have any ideas.
Thanks!
~ K
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 $response['headers'] - 它们可能包含 'location' 键。
这完全取决于您使用的 HTTP 库。
请参阅 class-http.php(wp 3.0.1) 文件:
第 1393 行,http_api_curl 操作 - 可以直接使用curl 句柄来捕获任何内容。
打开:
检查第 887-888 行和 $http_response_header 变量。
另外,尝试重写 processHeaders 函数,因为它可以访问原始 http 标头。
check $response['headers'] - they may contain 'location' key.
It all depends on the HTTP library you are using.
See class-http.php(wp 3.0.1) file:
line 1393, http_api_curl action - curl handle available directly to catch anything.
fopen:
check lines 887-888, and $http_response_header variable.
also, try to override processHeaders function as it has an access to raw http headers.
WP_Http 类处理标头并删除除最后一个标头之外的所有标头。所以你可以做上面提到的jetdog。检查原始 URL 并将其与返回的 $response['headers']['location'] 进行比较。如果不同,您就知道它已重定向。
The WP_Http class processes the headers and removes all but the last one. So you could do what jetdog described above. Check the original URL and compare it to the returned $response['headers']['location']. If it is different, than you know it redirected.