Skip to content
Advertisement

PHP / Xpath – if / else to check external site div content

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';
 }

Advertisement

Answer

Use this XPath to test it already using XPath:

$query = "//span[contains(concat(' ', @class, ' '), ' status ') and normalize-space()='All Systems Operational']";
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement