简体   繁体   English

加载页面以获取Cookie并同时读取源代码

[英]Load a page to get cookie and read source code at the same time

I am searching 3 days for an answer and I cannot find one because I always find some obstacles. 我正在寻找3天的答案,但找不到答案,因为我总是会发现一些障碍。

I need to load a web page (the reason for this is to accept a cookie) and then at the same time read the source code of the new page without hitting it again. 我需要加载一个网页(这样做的原因是接受cookie),然后同时读取新页面的源代码,而无需再次点击它。 The reason for this is that the page is dynamic so the content will change. 原因是页面是动态的,因此内容将更改。
I have tried to do this using iFrame( document.body.innerHTML ) but the fact that these pages run on different servers I hit cross-site scripting issues. 我尝试使用iFrame( document.body.innerHTML )来执行此操作,但是这些页面在不同服务器上运行的事实使我遇到了跨站点脚本问题。
I have also tried writing a php script using get_contents but this doesn't allow the cookie to be stored in my local. 我也尝试过使用get_contents编写php脚本,但这不允许将cookie存储在本地。

This is driving me crazy.... Any suggestion will be helful! 这让我发疯....任何建议都会很高兴! Need to use PHP or Javascript for this and any other suggestion will be useful as well. 为此需要使用PHP或Javascript,其他建议也将很有用。

When you are on the page document.body.innerHTML will give you the page source. 在页面上时, document.body.innerHTML将为您提供页面源代码。

Edit : I didn't realize you were loading it like that. 编辑 :我不知道你正在加载那样。 See this SO question . 看到这个问题

It can be done using cURL in PHP. 可以使用PHP中的cURL来完成。

A rough implementation: 大致的实现:

$ch = curl_init('http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
preg_match('/^Set-Cookie: (.*?);/m', $data, $cookies);

var_dump($cookies);
var_dump($data);

$data will contain the entire response, so we need to parse out the cookie headers ourselves. $data将包含整个响应,因此我们需要自己解析cookie标头。

If available on your system, HttpRequest would make this easier. 如果您的系统上有HttpRequest ,它将使此操作变得更容易。

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

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