在 PHP 中模拟启用 cookie 的浏览器
如何使用 PHP 打开网页并接收其 cookie?
动机:我正在尝试使用 feed43 从非-启用RSS的HighLearn网站(远程学习网站)。 我找到了包含我需要解析的提要内容的网页,但是,它需要先登录。 幸运的是,登录可以通过 GET 请求完成,因此就像 fopen()ing "http://highlearn.website/login_page.asp?userID=foo&password=bar”。 但我仍然需要获取登录时生成的cookie,将cookie传递给真实客户端(也许使用setcookie()?),然后重定向。
How can I open a web-page and receive its cookies using PHP?
The motivation: I am trying to use feed43 to create an RSS feed from the non-RSS-enabled HighLearn website (remote learning website). I found the web-page that contains the feed contents I need to parse, however, it requires to login first.
Luckily, logging in can be done via a GET request so it's as easy as fopen()ing "http://highlearn.website/login_page.asp?userID=foo&password=bar" for example. But I still need to get the cookies generated when I logged in, pass the cookies to the real client (using setcookie() maybe?) and then redirect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于服务器端 HTTP 客户端,您应该使用 cURL 模块。 它将允许您在多个请求中保留 cookie。 它还做了一些其他巧妙的事情,例如捆绑请求(curl_multi)和透明地处理重定向。
当涉及到将会话返回给用户时,我认为这是不可能的。 您需要能够覆盖其他域的 cookie。 这会导致严重的安全问题,因此没有浏览器会实现它。
For a server-side HTTP client you should use the cURL module. It will allow you to persist cookies across multiple requests. It also does some other neat things like bundling requests (curl_multi) and transparently handling redirects.
When it comes to returning a session to your user, I don't think this is possible. You would need to be able to overwrite the cookies of other domains. This would cause massive security issues, so no browser would implement it.
我之前曾使用 Simpletest 中的 Scriptable Browser 组件进行此类屏幕抓取。 它在模拟浏览器方面做得非常好。
您不需要将会话传递给真正的客户端(尽管它可能,具体取决于站点的安全级别) - 您可以简单地让您的 PHP 脚本作为目标站点和您的最终用户。
I've used the Scriptable Browser component from Simpletest for this kind of screen scraping before. It does a pretty good job of simulating a browser.
You don't need to pass the session on to the real client (Even though it may be possible, depending on the site's security level) - You can simply let your PHP-script be a proxy between the target site and your end-user.
不幸的是,除非网站位于同一域中,否则这是不可能的。 Cookie 仅在其来源域上有效。 此外,子域也算作不同的域。 否则,(请记住我没有使用太多 php)您可以从响应中提取标头,并将 cookie 复制到客户端。
Unfortunately, this is not possible unless the websites are on the same domain. Cookies are only valid on the domain they originated from. Also, subdomains count as different domains. Otherwise, (keep in mind I haven't used much php) you could pull the headers out of the response, and copy the cookie out to the client.