1

I am using XPATH and PHP to retrieve operational status content of an external website div

   `<?php 
  $doc = new DOMDocument;
  $doc->preserveWhiteSpace = false;
  $doc->strictErrorChecking = false;
  $doc->recover = true;
  $doc->loadHTMLFile('https://status.xero.com/');
  $xpath = new DOMXPath($doc);
  $query = "//span[@class='status']";
  $entries = $xpath->query($query);
  $info = ($entries->item(0)->textContent);

  echo '<div id="sta">' . $info .'</div>'; 
  ?>`

How can I test if the text within the target div 'status' is "All Systems Operational" example (I know it's not correct!)

 if ($entries->item(0)->textContent === "All Systems Operational"){

   echo 'YES';
 } else {
   echo 'NO';
 }
SolaceBeforeDawn
  • 958
  • 1
  • 8
  • 16
  • You have a problem with the XPath, as the class may contain other things than stats (as it seems to), you need to use something like `//span[contains(concat(' ', @class, ' '), ' status ')]` to find the right element (from https://stackoverflow.com/questions/1604471/how-can-i-find-an-element-by-css-class-with-xpath). – Nigel Ren Jun 19 '21 at 05:49

1 Answers1

1

Use this XPath to test it already using XPath:

$query = "//span[contains(concat(' ', @class, ' '), ' status ') and normalize-space()='All Systems Operational']";
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19