Get – Quentin Oct 08 '18 at 07:38

  • I have tried it, but it just giving the empty result – BOBBY IRAWAN Oct 08 '18 at 07:42
  • which dom parser are you using? – apokryfos Oct 08 '18 at 07:46
  • php simple html dom parser – BOBBY IRAWAN Oct 08 '18 at 07:52
  • That can't get script content. It just throws it away. – apokryfos Oct 08 '18 at 07:53
  • Then what's your solution? – BOBBY IRAWAN Oct 08 '18 at 07:54
  • PHP doesn't really have a decent DOM parser, I'd use Node.js and jsdom probably – apokryfos Oct 08 '18 at 07:59
  • You can take a look at [one of my previous answers](https://stackoverflow.com/a/51987159/487813) for a possible workaround – apokryfos Oct 08 '18 at 08:01
  • You could just search for a line starting with `var key = "rkey=` – brombeer Oct 08 '18 at 08:04
  • @apokryfos I'll try it, thanks for the response. I still want to choose between the python or php actually. Node wasn't my option at all, never learn it before – BOBBY IRAWAN Oct 08 '18 at 08:09
  • @kerbholz how do I get that, when I run my code it result empty? – BOBBY IRAWAN Oct 08 '18 at 08:10
  • @apokryfos _"PHP doesn't really have a decent DOM parser"_ <- you're kidding, right? http://php.net/manual/book.dom.php – Phil Oct 09 '18 at 03:15
  • @Phil [it can't handle script contents properly](http://sandbox.onlinephpfunctions.com/code/0d2d01f9bcda5b39e7350e6e138446ee0e7ef258) and this is what OP actually wants. Wrapping scripts in `CDATA` doesn't work either but that's what CDATA is there for. So, not what I would consider a "decent parser" – apokryfos Oct 09 '18 at 07:36
  • 1 Answers1

    1

    Depending on what the rest of the markup looks like, you may be able to just use DOMDocument and XPath, then parse out the value of the var with preg_match. This example will echo the key.

    <?php
    
    $html = <<<END
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="text/javascript">
            var keysearch = {"departureLabel":"Surabaya (SUB : Juanda) Jawa Timur Indonesia","arrivalLabel":"Palangkaraya (PKY : Tjilik Riwut | Panarung) Kalimantan Tengah Indonesia","adultNum":"1","childNum":"0","infantNum":"0","departure":"SUB","arrival":"PKY","departDate":"20181115","roundTrip":0,"cabinType":-1,"departureCode":"ID-Surabaya-SUB","arrivalCode":"ID-Palangkaraya-PKY"};
    
            (function(window, _gtm, keysearch){
    
                if (window.gtmInstance){
                    var departureExp = keysearch.departureCode.split("-");
                    var arrivalExp = keysearch.arrivalCode.split("-");
    
                    gtmInstance.setFlightData({
                        'ITEM_TYPE': 'flight',
                        'FLY_OUTB_CODE': departureExp[2],
                        'FLY_OUTB_CITY': departureExp[1],
                        'FLY_OUTB_COUNTRYCODE': departureExp[0],
                        'FLY_OUTB_DATE': keysearch.departDate,
    
                        'FLY_INB_CODE': arrivalExp[2],
                        'FLY_INB_CITY': arrivalExp[1],
                        'FLY_INB_COUNTRYCODE': arrivalExp[0],
                        'FLY_INB_DATE': keysearch.returnDate,
                        'FLY_NBPAX_ADL': keysearch.adultNum,
                        'FLY_NBPAX_CHL': keysearch.childNum,
                        'FLY_NBPAX_INF': keysearch.infantNum,
                    });
    
                    gtmInstance.pushFlightSearchEvent();
                }
            }(window, gtmInstance, keysearch));
    
    
                        var key = "rkey=10fe7b6fd1f7fa1ef0f4fa538f917811dbc7f4628a791ba69962f2ed305fb72d061b67737afd843aaaeeee946f1442bb";
                    var staticRoot = 'http://sta.nusatrip.net';
    
            $(function() {
                $("#currencySelector").nusaCurrencyOptions({
                    selected: getCookie("curCode"),
                });                        
            });
        </script>   
    </head>
    <body>foo</body>
    </html>
    END;
    
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    
    $xpath = new DOMXPath($dom);
    $result = $xpath->query('//script');
    
    foreach($result as $currScriptTag)
    {
        $currScriptContent = $currScriptTag->nodeValue;
    
        $matchFound = preg_match('/var key = "(.*)"/', $currScriptContent, $matches);
    
        if($matchFound)
        {
            /*
             * $matches[0] will contain the whole line like var key = "..." 
             * $matches[1] just contains the value of the var
             */
            $key = $matches[1];
    
            echo $key.PHP_EOL;
        }
    }
    
    Rob Ruchte
    • 3,569
    • 1
    • 16
    • 18
    • WOW, it's work. Thanks a lot, still don't know about XPath. Do u have any suggest where should I learn it? Thanks @Rob Ruchte – BOBBY IRAWAN Oct 11 '18 at 07:21
    • XPath is very mature and widely used, so there is a lot of material out there, but I use this often, it's one of the most concise references I've found: https://msdn.microsoft.com/en-us/library/ms256471(v=vs.110).aspx – Rob Ruchte Oct 11 '18 at 13:48
    • Thanks again, you really help me – BOBBY IRAWAN Oct 12 '18 at 15:33