在PHP中模拟启用Cookie的浏览器 [英] Simulating a cookie-enabled browser in PHP
问题描述
如何使用PHP开启网页并接收Cookie?
How can I open a web-page and receive its cookies using PHP?
动机:我尝试使用 feed43 从非启用RSS的HighLearn网站(远程学习网站)创建RSS Feed。我找到了包含我需要解析的feed内容的网页,但是,它需要先登录。
幸运的是,登录可以通过GET请求来完成,所以它很容易像fopen()ing例如,http://highlearn.website/login_page.asp?userID=foo&password=bar 。但我仍然需要获取我登录后生成的Cookie ,将cookie传递给真实的客户端(使用setcookie()也许?),然后重定向。
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.
推荐答案
对于服务器端HTTP客户端,您应该使用 cURL 模块。它将允许您跨多个请求保留Cookie。它还做了一些其他整洁的事情,如捆绑请求(curl_multi)和透明地处理重定向。
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.
当谈到返回一个会话给你的用户,我不认为这是可能的。您需要能够覆盖其他域的Cookie。这将导致大量的安全问题,所以没有浏览器会实现它。
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.
这篇关于在PHP中模拟启用Cookie的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!