「ADAM 筆記」PHP PhantomJS

    摘自: http://jonnnnyw.github.io/php-phantomjs/

<

iframe src=\”http://jonnnnyw.github.io/php-phantomjs/\” class=\”iframe-full-content\”>

<nav class=\"navbar navbar-default visible-xs\" role=\"navigation\" id=\"global-nav\"><!-- Brand and toggle get grouped for better mobile display --><div class=\"navbar-header\">
        <button type=\"button\" class=\"navbar-toggle\" data-parent=\"#global-nav\" data-toggle=\"collapse\" data-target=\"#navbar-collapse-main\">
            <span class=\"sr-only\">Toggle navigation</span>
            <span class=\"icon-bar\"></span>
            <span class=\"icon-bar\"></span>
            <span class=\"icon-bar\"></span>
        </button>

        <button type=\"button\" class=\"navbar-toggle version-toggle\" data-parent=\"#global-nav\" data-toggle=\"collapse\" data-target=\"#navbar-collapse-version\">
            <span class=\"sr-only\">Toggle navigation</span>
            <span class=\"fa fa-terminal\"><b>4.0</b> version</span>
        </button>

        <a class=\"navbar-brand\" href=\"http://jonnnnyw.github.io/php-phantomjs/\">
            <img src=\"http://jonnnnyw.github.io/php-phantomjs/assets/media/phantomjs-small.png\" class=\"img-circle\">
            PHP PhantomJs
        </a>
    </div>

    <div class=\"panel\">
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class=\"collapse navbar-collapse\" id=\"navbar-collapse-version\">
            <ul class=\"nav navbar-nav\">
  • 4.0 version
  • 3.0 version
  •     <div class=\"panel\">
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class=\"collapse navbar-collapse\" id=\"navbar-collapse-main\">
                <ul class=\"nav navbar-nav\">
    
  • Introduction
  •                 <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/2-installation/\">Installation</a></li>
    
                    <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/\">Usage</a></li>
    
                    <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/\">Custom Scripts</a></li>
    
                    <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/5-caching/\">Caching</a></li>
    
                    <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/6-debugging/\">Debugging</a></li>
    
                    <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/7-troubleshooting/\">Troubleshooting</a></li>
    
                </ul>
    

    </nav><!-- version-menu-dropdown --><div class=\"btn-group hidden-xs\" id=\"nav-version\">
        <button type=\"button\" class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\">
            <i class=\"fa fa-terminal\"><b>4.0</b> version</i>
        </button>
        <ul class=\"dropdown-menu\" role=\"menu\">
    
  • 4.0 version
  • 3.0 version
  • <!-- nav-menu-dropdown -->
    <div class=\"btn-group hidden-xs\" id=\"nav-menu\">
        <button type=\"button\" class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\">
            <i class=\"fa fa-bars\"></i>
        </button>
        <ul class=\"dropdown-menu\" role=\"menu\">
    
  • Introduction
  •         <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/2-installation/\"><i class=\"fa fa-folder\"></i>Installation</a></li>
    
            <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/\"><i class=\"fa fa-folder\"></i>Usage</a></li>
    
            <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/\"><i class=\"fa fa-folder\"></i>Custom Scripts</a></li>
    
            <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/5-caching/\"><i class=\"fa fa-folder\"></i>Caching</a></li>
    
            <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/6-debugging/\"><i class=\"fa fa-folder\"></i>Debugging</a></li>
    
            <li><a href=\"http://jonnnnyw.github.io/php-phantomjs/4.0/7-troubleshooting/\"><i class=\"fa fa-folder\"></i>Troubleshooting</a></li>
    
            <li class=\"divider\">
            <li><a href=\"#\"><i class=\"fa fa-arrow-up\"></i>Top of Page</a></li>
        </ul>
    
    <div class=\"col-sm-3 sidebar hidden-xs\">
    
    Run PhantomJS scripts through PHP
    </div>
    
    <p id=\"version-notice\">You are currently viewing version <span>4.0</span> documentation.</p>
    
    <div class=\"col-sm-9 col-sm-offset-3\">
    

    PHP PhantomJS is a flexible PHP library to load pages through the PhantomJS headless browser and return the page response. It is handy for testing websites that demand javascript support and also supports screen captures and PDF output.

    Feature List

    Prerequisites

    PHP PhantomJS requires PHP 5.4.0 or greater to run.

    Installation

    It is recommended that you use Composer to install PHP PhantomJS. First, add the following to your project’s composer.json file:

        
        #composer.json
    
        \"scripts\": {
            \"post-install-cmd\": [
                \"PhantomInstaller\\\\Installer::installPhantomJS\"
            ],
            \"post-update-cmd\": [
                \"PhantomInstaller\\\\Installer::installPhantomJS\"
            ]
        }
        

    This will ensure the latest version of PhantomJS is installed for your system, in your bin folder. If you haven’t defined your bin folder in your composer.json, add the path:

        
        #composer.json
        
        \"config\": {
            \"bin-dir\": \"bin\"
        }

    Finally, install PHP PhantomJS from the root of your project:

        
        #bash
        
        $ composer require \"jonnyw/php-phantomjs:4.*\"

    If you would like to use another installation method or would like to see more detailed installation instructions, see the installation documentation.

    Important

    By default the PhantomJS library will look for the PhantomJS executable in the bin folder relative to where your script is running ~/bin/phantomjs. If the executable cannot be found or if the path to your PhantomJS executable differs from the default location, for example you have installed PhantomJS globally, you will need to define the path to your PhantomJS executable manually.

    $client->getEngine()->setPath(\'/path/to/phantomjs\');

    Basic Usage

    The following illustrates how to make a basic GET request and output the page content:

        <?php
    
        use JonnyW\\PhantomJs\\Client;
    
        $client = Client::getInstance();
    
        /** 
         * @see JonnyW\\PhantomJs\\Http\\Request
         **/
        $request = $client->getMessageFactory()->createRequest(\'http://jonnyw.me\', \'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();
        }
        

    Saving a screen capture to local disk:

        <?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://jonnyw.me\', \'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);
        

    Outputting a page as PDF:

        <?php
    
        use JonnyW\\PhantomJs\\Client;
    
        $client = Client::getInstance();
    
        /** 
         * @see JonnyW\\PhantomJs\\Http\\PdfRequest
         **/
        $request = $client->getMessageFactory()->createPdfRequest(\'http://jonnyw.me\', \'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);
        

    For more detailed examples see the usage section, or you can create your own custom scripts.


    <script type=\"text/javascript\" src=\"http://jonnnnyw.github.io/php-phantomjs/assets/resources/jquery/jquery.min.js\"></script><script type=\"text/javascript\" src=\"http://jonnnnyw.github.io/php-phantomjs/assets/resources/bootstrap/js/bootstrap.min.js\"></script><script type=\"text/javascript\" src=\"http://jonnnnyw.github.io/php-phantomjs/assets/js/app.js\"></script>
    

    發佈留言

    發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *