php-phantomjs icon indicating copy to clipboard operation
php-phantomjs copied to clipboard

Why php phantomjs not executd js code in response content

Open shubaivan opened this issue 5 years ago • 2 comments

I need get html with executed js for this I install phantomjs but in get content I had still old html, with old data, data should was handling by js script and should changed to span tag with some class, which script should execute. I correct understand, when I open get content I should get DOM with handled data, means tag span instead json element ? What I mssed ?

public function getContentByPhantomjs($url)
{
    $client = Client::getInstance();

    $client->getEngine()->addOption('--ssl-protocol=any');
    $client->getEngine()->addOption('--ignore-ssl-errors=true');
    $client->getEngine()->addOption('--web-security=false');

    $client->getEngine()->setPath(MY_PATH . 'bin/phantomjs');

    $request = $client->getMessageFactory()->createRequest($url, 'GET');
    $response = $client->getMessageFactory()->createResponse();

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

    return $response->getStatus() === 200 ? $response->getContent() : false;

}

but js not executed, I had not handled data in #pretty_json_id

    <div class="headline">
  
        <div id="pretty_json_id">
        <pre>                
          {"Company":"test"}
        </pre>
    </div>


<script>


    function ready() {
        element = document.getElementById('pretty_json_id').getElementsByTagName('pre');

        if (element.length > 0) {
            let parseJson = JSON.parse(element[0].innerHTML);
            let stringifyJson = JSON.stringify(parseJson, undefined, 4);
            element[0].innerHTML = syntaxHighlight(stringifyJson);
        }
    }

    function syntaxHighlight(json) {
        json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
        return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
            var cls = 'number';
            if (/^"/.test(match)) {
                if (/:$/.test(match)) {
                    cls = 'key';
                } else {
                    cls = 'string';
                }
            } else if (/true|false/.test(match)) {
                cls = 'boolean';
            } else if (/null/.test(match)) {
                cls = 'null';
            }
            return '<span class="' + cls + '">' + match + '</span>';
        });
    }

    document.addEventListener("DOMContentLoaded", ready);
</script>

In browser everythink works. But I need exist html with executed js

log information

    2020-01-28T14:49:34 [DEBUG] CookieJar - Created but will not store cookies (use option '--cookies-file=<filename>' to enable persistent cookie storage)
2020-01-28T14:49:34 [DEBUG] Set  "http"  proxy to:  "" : 1080
2020-01-28T14:49:34 [DEBUG] Phantom - execute: Configuration
2020-01-28T14:49:34 [DEBUG]      0 objectName : ""
2020-01-28T14:49:34 [DEBUG]      1 cookiesFile : ""
2020-01-28T14:49:34 [DEBUG]      2 diskCacheEnabled : "true"
2020-01-28T14:49:34 [DEBUG]      3 maxDiskCacheSize : "-1"
2020-01-28T14:49:34 [DEBUG]      4 diskCachePath : ""
2020-01-28T14:49:34 [DEBUG]      5 ignoreSslErrors : "true"
2020-01-28T14:49:34 [DEBUG]      6 localUrlAccessEnabled : "true"
2020-01-28T14:49:34 [DEBUG]      7 localToRemoteUrlAccessEnabled : "false"
2020-01-28T14:49:34 [DEBUG]      8 outputEncoding : "UTF-8"
2020-01-28T14:49:34 [DEBUG]      9 proxyType : "http"
2020-01-28T14:49:34 [DEBUG]      10 proxy : ":1080"
2020-01-28T14:49:34 [DEBUG]      11 proxyAuth : ":"
2020-01-28T14:49:34 [DEBUG]      12 scriptEncoding : "UTF-8"
2020-01-28T14:49:34 [DEBUG]      13 webSecurityEnabled : "false"
2020-01-28T14:49:34 [DEBUG]      14 offlineStoragePath : ""
2020-01-28T14:49:34 [DEBUG]      15 localStoragePath : ""
2020-01-28T14:49:34 [DEBUG]      16 localStorageDefaultQuota : "-1"
2020-01-28T14:49:34 [DEBUG]      17 offlineStorageDefaultQuota : "-1"
2020-01-28T14:49:34 [DEBUG]      18 printDebugMessages : "true"
2020-01-28T14:49:34 [DEBUG]      19 javascriptCanOpenWindows : "true"
2020-01-28T14:49:34 [DEBUG]      20 javascriptCanCloseWindows : "true"
2020-01-28T14:49:34 [DEBUG]      21 sslProtocol : "any"
2020-01-28T14:49:34 [DEBUG]      22 sslCiphers : "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-RSA-RC4-SHA:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:RC4-SHA:RC4-MD5"
2020-01-28T14:49:34 [DEBUG]      23 sslCertificatesPath : ""
2020-01-28T14:49:34 [DEBUG]      24 sslClientCertificateFile : ""
2020-01-28T14:49:34 [DEBUG]      25 sslClientKeyFile : ""
2020-01-28T14:49:34 [DEBUG]      26 sslClientKeyPassphrase : ""
2020-01-28T14:49:34 [DEBUG]      27 webdriver : ":"
2020-01-28T14:49:34 [DEBUG]      28 webdriverLogFile : ""
2020-01-28T14:49:34 [DEBUG]      29 webdriverLogLevel : "INFO"
2020-01-28T14:49:34 [DEBUG]      30 webdriverSeleniumGridHub : ""
2020-01-28T14:49:34 [DEBUG] Phantom - execute: Script & Arguments
2020-01-28T14:49:34 [DEBUG]      script: "/tmp/1140ef46a6d6bfd239db"
2020-01-28T14:49:34 [DEBUG] Phantom - execute: Starting normal mode
2020-01-28T14:49:34 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:34 [DEBUG] FileSystem - _open: ":/modules/fs.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:34 [DEBUG] FileSystem - _open: ":/modules/system.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:34 [DEBUG] FileSystem - _open: ":/modules/webpage.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:34 [DEBUG] WebPage - updateLoadingProgress: 10
2020-01-28T14:49:35 [DEBUG] CookieJar - Saved "PHPSESSID=rh333emlg3vfov3u66e4lbkdkc; domain=cdb.preem.local; path=/"
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 30
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 34
2020-01-28T14:49:35 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:35 [WARNING] QNetworkDiskCache::prepare() unable to open temporary file
2020-01-28T14:49:35 [WARNING] QNetworkDiskCache::prepare() unable to open temporary file
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 37
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 40
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 43
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 100
2020-01-28T14:49:35 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function () {\n    \n    var styles = {};\n    \n    for(var property in styles) {\n        document.body.style[property] = styles[property];\n    }\n})(); })()"
2020-01-28T14:49:35 [DEBUG] WebPage - evaluateJavaScript result QVariant(Invalid)
2020-01-28T14:49:35 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:35 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function () {\n            return document.getElementsByTagName('html')[0].innerHTML;\n        })(); })()"
2020-01-28T14:49:35 [DEBUG] WebPage - evaluateJavaScript result QVariant(QString, "my html")
2020-01-28T13:49:34 [INFO] PhantomJS - Set viewport size ~ width: 1366 height: 768\n2020-01-28T14:49:35 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 10
2020-01-28T14:49:35 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 100
2020-01-28T14:49:35 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:35 [DEBUG] FileSystem - _open: ":/modules/fs.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:35 [DEBUG] FileSystem - _open: ":/modules/system.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:35 [DEBUG] FileSystem - _open: ":/modules/webpage.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 10
2020-01-28T14:49:35 [DEBUG] WebPage - setupFrame ""
2020-01-28T14:49:35 [DEBUG] FileSystem - _open: ":/modules/fs.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:35 [DEBUG] FileSystem - _open: ":/modules/system.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:35 [DEBUG] FileSystem - _open: ":/modules/webpage.js" QMap(("mode", QVariant(QString, "r")))
2020-01-28T14:49:35 [DEBUG] WebPage - updateLoadingProgress: 100
2020-01-28T14:49:35 [DEBUG] CookieJar - Purged (session) "PHPSESSID=rh333emlg3vfov3u66e4lbkdkc; domain=my.local; path=/"

shubaivan avatar Jan 28 '20 14:01 shubaivan

Have you dealt with your problem?

zlseqx avatar Feb 21 '20 09:02 zlseqx

I'm seeing this as well, and it just started recently. It's been running fine for years.

mdbollman avatar Mar 02 '20 14:03 mdbollman