首页 > 解决方案 > PHP:加载 JS 事件后获取远程页面

问题描述

我正在使用一种简单的方法来加载大部分工作正常的远程网页:

$output = file_get_contents($item['URL']);
$html->loadHTML($output);

之后我可以按类型或名称或ID搜索标签,但问题是我想要的主要内容是在最后一秒由JS生成的。在浏览器中加载时,您不会注意到它,但是当尝试使用 file_get_contents 获取它时,我得到的页面是在最后一分钟 JS 运行之前存在的。

这是加载我想要的内容的部分代码,因此您可以了解我的意思,但它非常简单:我得到的页面不是“完整”页面。

<script type="text/javascript">ImageMachine.prototype.ImageMachine_Generate_Thumbnail = function (thumbnail_image, main_image, closeup_image, type_code) {
var thumbnail,
img;

我也尝试过使用 CURL,但没有运气。


                $header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,;q=0.8";
                $header[] = "Connection: keep-alive";

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $item['URL']);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header_str);
//              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_COOKIE, true); 
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
//              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_ENCODING, '');         
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_USERAGENT, $this_time);
                $output = curl_exec($ch);
                curl_close($ch);
                    
                @$html->loadHTML($output);

有没有办法得到整个东西?我希望浏览器或用户在加载页面时会看到相同的页面。

标签: php

解决方案


推荐阅读