John Davidson

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

0 comments
Message:


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


My Answer:
$url = 'https://www.externalwebsite.com';
$html = file_get_contents($url);

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);

$status = $xpath->query("//div[@class='content']")->item(0)->nodeValue;

if($status == 'Operational'){
echo 'The website is operational';
} else {
echo 'The website is not operational';
}

Rate this post

3 of 5 based on 3209 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us