I'm using xPath to scrape tour dates from another website (with permission of course). Since it updates on every page load, I thought about using transients to store the data.
Unfortunately I have no experience using transients and don't get it to work. This is my code:
<?php
$html = file_get_contents('http://www.example.com');
$doc = new DOMDocument();
libxml_use_internal_errors(TRUE); // disable libxml errors
if(!empty($html)) {
$doc->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
// Get only the content needed
$termine = $xpath->query('//ul[@class="artistEvents"]/li');
if ($termine->length > 0) {
foreach ($termine as $termin) {
$date = $xpath->query("div[@class='left']/strong", $termin);
$location = $xpath->query("div[contains(@class,'right')]", $termin);
echo '<tr>';
// Date
if ($date->length > 0) {
$date = substr($date->item(0)->nodeValue, 3, 10);
$date = strftime("%d.%m.%Y", strtotime($date));
echo '<td class="live-date">' . $date . '</td>';
}
// Location
if ($location->length > 0) {
$location = substr($location->item(0)->nodeValue, 14);
$location = utf8_decode($location);
echo '<td class="live-location">' . $location . '</td>';
}
echo '</tr>';
}
}
else {
echo '<p>No dates available.</p>';
}
}
?>
Any help with how to use transients to store this query is greatly appreciated! Also, I've never used xPath before, so if my code needs any improvement (although it works) I'll be happy to know about it.
Thanks so much!