-1

I want to find data from a webpage which stores data in Java Script block and later renders the page using those data. How to fetch such data in PHP ?
I've tried DOMXPath and DomDocument, still no luck !
Below I'm posting an example of the target page.

<html>
...
<script type="text/javascript">
var showHeader = true;
var Data = {
  "packageId": "120",
  "packageTitle": "West Bengal",
  "Type": "Customizable",
   "Components": [
    {
      "destination": "Darjeeling",
      "dayNum": {
        "1": {
          "sightseeings": [
            "No Sightseeing"
          ],
          "itineraries": {
            "title": "Bagdogra / New Jalpaiguri - Darjeeling",
            "description": "<p>Welcome to darjeeling.</p>"
          }
        }
        }
      }
    ]
    };
</script>
<body>
...
</body>
</html>

I want to retrieve all those data in an associative array using PHP so I can use like $data['showHeader'] or $data['data']['packageId']

1
  • "I've tried DOMXPath and DomDocument" and yet no code provided in the question? Commented Dec 16, 2018 at 19:11

1 Answer 1

-1

How about to extract the necessary data using a regular expression and convert it to array as follows:

if (preg_match('#^var showHeader = (?P<showHeader>\w+);\s*^var Data = (?P<json>{.*?});#ms', $html, $m)) {
    $data = [
        'showHeader' => ($m['showHeader'] === 'true' || $m['showHeader'] === '1'),
        'data' => json_decode($m['json'], true)
    ];

    echo $data['data']['packageId'];
} else {
    echo 'js data not found';
}

If there are several scripts, you may want to analyze contents of all scripts:

libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHTML($html);

$data = [];
foreach ($dom->getElementsByTagName('script') as $script) {
    if (preg_match('#^var showHeader = (?P<showHeader>\w+);\s*^var Data = (?P<json>{.*?});#ms', $script->nodeValue, $m)) {
        $data['showHeader'] = ($m['showHeader'] === 'true' || $m['showHeader'] === '1');
        $data['data'] = json_decode($m['json'], true);
        break;
    }
}

if ($data) {
    echo $data['showHeader']['packageId'];
} else {
    echo 'js data not found';
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.