如何在php中截取网页?

时间:2016-05-30 17:52:44

标签: php

我目前正在建立一个网站,我只希望用户能够截取网页然后下载图像。所以我需要帮助的是:如何使用PHP截取网页,然后将图像保存在我的服务器上。

我已经阅读了一些关于如何截取页面的其他教程,但我无法让它工作。

我正在使用Linux服务器(Debian 7.0)。

2 个答案:

答案 0 :(得分:1)

您可以使用grabz.it

使用PHP网站截图

  1. 免费获取Application Key and Secret
  2. 下载免费PHP Demo and Library并试一试。
  3. 然后像这样使用它:

    include("GrabzItClient.class.php");
    $grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
    $grabzIt->SetImageOptions("http://www.google.com"); 
    $grabzIt->SaveTo("google.jpg");
    

    如果您不想依赖第三方,可以使用phantomjs,即:

    phantomjs responsive-screenshot.js http://google.com
    

    备注:

    1-下载responsive-screenshot.js。检查源代码以获取可用选项。

    2-您可以通过克隆github repo来安装phantomj:

    git clone https://github.com/ariya/phantomjs.git
    

答案 1 :(得分:0)

我最近发布了一个项目,可以让PHP访问浏览器。在此处获取:https://github.com/merlinthemagic/MTS

与以前的答案一样,该项目依赖于PhantomJS。

下载并设置后,您只需使用以下代码:

$myUrl          = "http://www.google.com";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//now do a screenshot, valid formats are png, jpeg, gif.
$imageData      = $windowObj->screenshot("png");

file_put_contents("/path/to/your/file.png", $imageData);