保存远程网页的Cookie [英] Save cookies for remote web pages
问题描述
我有一个PHP应用程序需要从另一个网页抓取内容,我正在阅读的网页需要一个cookie。
ve找到了有关如何进行此调用一旦我有cookie的信息( http ://groups.google.com/group/comp.lang.php/msg/4f618114ab15ae2a ),但我不知道如何生成cookie,或如何/在哪里保存cookie。
例如,要通过wget读取此网页,请执行以下操作:
wget --quiet --save-cookies cookie.file --output-document = who.cares \
http://remoteServer/login.php?user = xxx& pass = yyy
wget --quiet --load-cookies cookie.file --output-document = documentiwant.html \
http://remoteServer/pageicareabout.html
...我的问题是我如何在PHP中的--save-cookies位,以便我可以使用cookie, up PHP stream_context_create / file_get_contents block:
$ opts = array(http'=> array(
'method'=>GET,
'header'=>
Accept-language:en\r\\\
:** NoClueAtAll ** \r\\\
)
);
$ context = stream_context_create($ opts);
$ documentiwant = file_get_contents(http://remoteServer/pageicareabout.html,
0,$ context);
解决方案=http://www.php.net/curl =noreferrer> cURL 。
使用 curl_setopt 设置Cookie处理选项。
如果这只是一次性的事情,你可以使用Firefox Live HTTP Headers 以获取标题,然后将其粘贴到您的PHP代码中。
I've got a PHP application which needs to grab the contents from another web page, and the web page I'm reading needs a cookie.
I've found info on how to make this call once i have the cookie ( http://groups.google.com/group/comp.lang.php/msg/4f618114ab15ae2a ), however I've no idea how to generate the cookie, or how / where the cookie is saved.
For example, to read this web page via wget I do the following:
wget --quiet --save-cookies cookie.file --output-document=who.cares \ http://remoteServer/login.php?user=xxx&pass=yyy wget --quiet --load-cookies cookie.file --output-document=documentiwant.html \ http://remoteServer/pageicareabout.html
... my question is how do I do the '--save-cookies' bit in PHP so that I can use the cookie in the follow-up PHP stream_context_create / file_get_contents block:
$opts = array(http'=> array( 'method'=> "GET", 'header'=> "Accept-language: en\r\n" . "Cookie: **NoClueAtAll**\r\n" ) ); $context = stream_context_create($opts); $documentiwant = file_get_contents("http://remoteServer/pageicareabout.html", 0, $context);
解决方案You'd probably be better off using cURL. Use curl_setopt to set up the cookie handling options.
If this is just a one-off thing, you could use Firefox with Live HTTP Headers to get the header, then paste it into your PHP code.
这篇关于保存远程网页的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!