简体   繁体   English

如何使用 get_contents 提供我的凭据? php

[英]how I can give my credentials with get_contents? php

I have looked at some examples and they use Curl in php etc but I just want a simple command that passes my credentials to a site so it can login and give me access to download a file.我查看了一些示例,他们在 php 等中使用 Curl 等,但我只想要一个简单的命令,将我的凭据传递给站点,以便它可以登录并允许我访问下载文件。 Current problem is when I try to use get_contents I get stuck at login page because it needs a login before it can allow a download so isnt there a way to send my login info before get_contents in php?当前的问题是,当我尝试使用 get_contents 时,我会卡在登录页面,因为它需要登录才能允许下载,所以没有办法在 php 中的 get_contents 之前发送我的登录信息吗? Thanks谢谢

for example we can assume the website is located at www.confluence.com例如,我们可以假设网站位于 www.confluence.com

You're out of luck.你运气不好。 file_get_contents() can only get content, hence the name. file_get_contents()只能获取内容,因此得名。 You can try to authenticate via the get syntax for standard authentication, like http://username:password@example.com .您可以尝试通过标准身份验证的 get 语法进行身份验证,例如http://username:password@example.com

If you have to post your credentials via HTTP POST, you'll have to use curl.如果您必须通过 HTTP POST 发布您的凭据,则必须使用 curl。

The problem is that when you log in the server send to your browser a cookies that your browser automatically stores.问题是,当您登录服务器时,您的浏览器会自动存储一个 cookies 到您的浏览器。 With file_get_content() you can actually pass cookies ( the third context parameter of file_get_content() can handle this).使用file_get_content()您实际上可以传递 cookies ( file_get_content() 的第三个context参数可以处理此问题)。

Have a look at stream context create .看看stream 上下文创建 By the way, you need to first send your login info to the login page (with curl ), when you recive cookies back, pass them as option to file_get_content() and the trick is done.顺便说一句,您需要先将您的登录信息发送到登录页面(使用curl ),当您收到 cookies 回来时,将它们作为选项传递给file_get_content()并完成。

If the server is using a login system different from cookies let us know, so we can help you如果服务器使用不同于 cookies 的登录系统,请告诉我们,以便我们为您提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM