[jonnyw/php-phantomjs]让PHP灵活易用的操作无头浏览器PhantomJS

2022-03-05 奥古斯宏
让PHP用PhantomJS来发起请求/执行JS/截图/导出PDF

PHP PhantomJS 是一个灵活的 PHP 库,让PHP通过很简单的代码来操作 PhantomJS 无头浏览器加载页面。 让PHP加载的网页地址执行JS/页面截图/导出PDF.

他可以非常方便的实现这些功能:

  • 使用无头浏览器PhantomJS加载请求
  • 查看详细的响应数据,包括页面内容、标题、状态 代码等
  • 处理重定向
  • 查看JS控制台输出报错
  • 查看详细的PhantomJS调试信息
  • 保存页面的截图
  • 将页面导出为PDF
  • 设置浏览窗口大小
  • 为PDF导出设置页眉页脚
  • 指定截图位置x,y和宽高
  • 延迟页面渲染(等待页面加载完成)
  • 轻松构建和运行PhantomJS脚本

PHP PhantomJS 只要求 PHP 5.4.0 或以上即可。

基本用法

使用phantomjs发起一个请求非常简单:

  <?php

    use JonnyW\PhantomJs\Client;

    $client = Client::getInstance();

    /** 
     * @see JonnyW\PhantomJs\Http\Request
     **/
    $request = $client->getMessageFactory()->createRequest('http://phpreturn.com', 'GET');

    /** 
     * @see JonnyW\PhantomJs\Http\Response 
     **/
    $response = $client->getMessageFactory()->createResponse();

    // Send the request
    $client->send($request, $response);

    if($response->getStatus() === 200) {

        // Dump the requested page content
        echo $response->getContent();
    }

将页面截图并保存:

 <?php

    use JonnyW\PhantomJs\Client;

    $client = Client::getInstance();
    
    $width  = 800;
    $height = 600;
    $top    = 0;
    $left   = 0;
    
    /** 
     * @see JonnyW\PhantomJs\Http\CaptureRequest
     **/
    $request = $client->getMessageFactory()->createCaptureRequest('http://phpreturn.com', 'GET');
    $request->setOutputFile('/path/to/save/capture/file.jpg');
    $request->setViewportSize($width, $height);
    $request->setCaptureDimensions($width, $height, $top, $left);

    /** 
     * @see JonnyW\PhantomJs\Http\Response 
     **/
    $response = $client->getMessageFactory()->createResponse();

    // Send the request
    $client->send($request, $response);

将页面导出为PDF:

  <?php

    use JonnyW\PhantomJs\Client;

    $client = Client::getInstance();

    /** 
     * @see JonnyW\PhantomJs\Http\PdfRequest
     **/
    $request = $client->getMessageFactory()->createPdfRequest('http://phpreturn.com', 'GET');
    $request->setOutputFile('/path/to/save/pdf/document.pdf');
    $request->setFormat('A4');
    $request->setOrientation('landscape');
    $request->setMargin('1cm');

    /** 
     * @see JonnyW\PhantomJs\Http\Response 
     **/
    $response = $client->getMessageFactory()->createResponse();

    // Send the request
    $client->send($request, $response);

自定义一个超时时间:

版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

默认情况下每个请求超时时间为5秒,我们可以自定义一个超时时间.

    <?php
    
    ...
    
    $timeout = 10000; // 10 秒
    
    $request = $client->getMessageFactory()->createRequest('http://phpreturn.com');
    $request->setTimeout($timeout);
    
    ...

定义延迟渲染:

有时候我们希望等到页面加载完才进行其他操作,此时只要简单地设置一个延迟时间即可.

<?php

    ...
    
    $delay = 5; // 5 seconds
    
    $request = $client->getMessageFactory()->createCaptureRequest('http://phpreturn.com');
    $request->setDelay($delay);
    
    ...
    

设置等待所有请求加载完成才进行操作:

 <?php

    ...
    
    use JonnyW\PhantomJs\Client;
    
    $client = Client::getInstance();
    $client->isLazy(); // 设置所有请求完成才渲染页面 

    $request  = $client->getMessageFactory()->createRequest();
    $request->setTimeout(5000); // 设置一个超时时间

    ...
    

并且提供了一个简单地设置方法,支持phantomjs所有的配置项:

 $client = Client::getInstance();
 $client->getEngine()->addOption('--config=/path/to/config.json');


版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

最近浏览
IP用户:136.243.*.*
1 天前 DataForSeoBot
IP用户:119.236.*.*
1 天前 Chrome Windows 10
IP用户:223.98.*.*
4 天前 Firefox Windows 10
IP用户:153.165.*.*
4 天前 Chrome Windows 10
IP用户:42.236.*.*
6 天前 360Spider
IP用户:165.154.*.*
6 天前 Chrome Windows 10
IP用户:165.154.*.*
6 天前 Chrome Windows 10
IP用户:66.249.*.*
9 天前 Googlebot
IP用户:66.249.*.*
9 天前 Googlebot
IP用户:218.24.*.*
9 天前 Chrome Windows 10
IP用户:157.55.*.*
10 天前 BingBot
IP用户:220.181.*.*
11 天前 Baidu Spider
累计浏览次数:1021
评论
点击登录
phpreturn,PHP武器库,专注PHP领域的项目和资讯,收录和介绍PHP相关项目。
最近浏览 点击登录
累计浏览次数:95663
一周浏览次数:1308
今日浏览次数:93

本站所有权利归 phpreturn.com 所有

举报/反馈/投稿邮箱:phpreturn@ulthon.com

鲁ICP备19027671号-2